Reading list Switch to dark mode

    Create webservice resource & assign permission to resource using code in PrestaShop 1.7

    Updated 21 March 2024

    In this blog, We are going to learn how we can create webservice resource & assign permission to this resource using code in PrestaShop 1.7.

    Sometimes we don’t have access to webservices tab inside Advanced Parameters > WebService. So we can manage webservice related things using code. Let’s understand things using code.

    Step 1:

    As we know if we want to add a new webservice resource in PrestaShop 1.7 then we need to register a hook.

    $this->registerHook('addWebserviceResources');

    Step 2:

    After this, We need to register a resource using this hook.

    /**
    * Add a specific_management resource
    * This hook is only in PS17
    *
    * @param array $resources
    * @return void
    */
    public function hookAddWebserviceResources($params)
    {
        $params['resources']['RESOURCE_NAME'] = array('description' => 'RESOURCE_DESCRIPTION');
    
        return $params['resources'];
    }

    If the resource is related to specific management then follow the below code.

    Searching for an experienced
    Prestashop Company ?
    Find out More
    public function hookAddWebserviceResources($params)
    {
        $params['resources']['RESOURCE_NAME'] = array('description' => 'RESOURCE_DESCRIPTION', 'specific_management' => true);
    
        return $params['resources'];
    }

    Step 3:

    Now we need to add some custom code in your install function to manage things at run time while installing the module.

    public function install()
    {
        if (!parent::install()
            || !$this->registerHook('addWebserviceResources')
        ) {
            return false;
        }
    
        Configuration::updateValue('PS_WEBSERVICE', 1); // Code added to enable prestashop webservice
        $apiAccess = new WebserviceKey();
        $apiAccess->key = $this->generateWebserviceKey(32); // Function to generate 32 character alphanumeric ws key
        $apiAcesss->save();
    
        $account_id = $apiAccess->id;
    
        $permissions = array(
            'RESOURCE_NAME' => ['GET' => 1, 'POST' => 1, 'PUT' => 1, 'DELETE' => 1, 'HEAD' => 1]
        );
    
        $response = WebserviceKey::setPermissionForAccount($apiAccess->id, $permissions);
    
        return true;
    }
    
    // Custom function to generate 32 character alphanumeric ws key
    
    function generateWebserviceKey($length) {
        $key = '';
        $keys = array_merge(range(0, 9), range('a', 'z'));
    
        for ($i = 0; $i < $length; $i++) {
            $key .= $keys[array_rand($keys)];
        }
    
        return strtoupper($key);
    }

    Now you will see that webservice key & permission are automatically created when we install the module.

    That’s all about this blog. Hope it will help you.

    If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home