Jump to content

How to set up permission for user programmatically


Recommended Posts

On 12/20/2018 at 11:38 AM, JBW said:

Check the class Employee.php in folder /classes - this should offer all methods needed to manage Employees.

mmmm, I can see only the method for checking if a user can access a tab:

   public function can($action, $tab)
    {
        $access = Profile::getProfileAccess($this->id_profile, Tab::getIdFromClassName($tab));

        return ($access[$action] == '1');
    }

I think it refers to ps_access table where I see a row for each menù permission

looking at the DB I've found the ps_module_access that has permissions for users on modules.

 

Now I'm looking for a function to set properly the access for a user to my module, during the installation script.

Any ideas?

Link to comment
Share on other sites

  • 2 weeks later...
On 12/21/2018 at 9:00 PM, JBW said:

If you inspect Employee class you will find that a profile_id which is a value from class Profile. Looking further you will also find class Access to define the access right on module level.

I don't understand how to  set permission for a certain user for using  my module during the installation process of the module

Link to comment
Share on other sites

Are you struggeling with the development part or with the permission concept overall? Whats the specific issue, how far did you get so far, what did you try?

In general a user has a profile assigned. This profil has several permissions assigned including authorizations for actions on modules. By default only SuperAdmin profile has module authorizations.

Link to comment
Share on other sites

10 hours ago, JBW said:

Are you struggeling with the development part or with the permission concept overall? Whats the specific issue, how far did you get so far, what did you try?

In general a user has a profile assigned. This profil has several permissions assigned including authorizations for actions on modules. By default only SuperAdmin profile has module authorizations.

ok, I was confusing.
I'm a Magento developer and I've been assigned a task for a Prestashop project that I have not seen yet, but I'm trying to inform myself. Now I'll explain what I should do:
I have a module that communicates with an external web service. I saw that the permissions on resources on PrestaShop, for web service, are assigned through an API key (I was confused because on Magento the permissions for web services are assigned by users).
Now I have to give read permissions on this apikey on everything and in writing only on the orders
is it clearer now?

Link to comment
Share on other sites

ok, I was wrong I just needed to give permission to the webservice (advanced parameters -> webservice). I was able to do it using the WebserviceKey class that has the method
setPermissionForAccount, and passing it a parameter containing the permissions extracted from a json file.
Here is my install function:

public function install()
    {

        $keyId = $this->getKeyId();
        $wsKey = new WebserviceKey($keyId ? $keyId : null);
        $wsKey->active = true;
        $wsKey->description = self::KEY_DESCRIPTION_MARKER;

        do {
            $wsKey->key = $this->generateKey(self::KEY_LENGTH);
        } while (WebserviceKey::keyExists($wsKey->key));

        $wsKey->add();

        $permissions_file = "../modules/".$this->name."/data/permissions.json";
        $jsonPermissions = file_get_contents($permissions_file); //TODO controlli sul file
        $permissions_to_set = json_decode($jsonPermissions,true);
        $wsKey->setPermissionForAccount($wsKey->id,$permissions_to_set);

        $configuration = new stdClass();
        $configuration->soap_key = $wsKey->key;

        Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration));

        return parent::install() &&
            $this->registerHook('actionAdminControllerSetMedia') &&
            $this->registerHook('displayBackOfficeHeader');
    }

 

in the permissions.json we have:

{
 "messages": {
    "GET": "on"
  },
  "order_carriers": {
    "GET": "on",
    "PUT": "on",
    "POST": "on"
  }
}

So the installation works fine, I'm trying to update my module via zip upload, in this case Prestashop updates the module to the correct version, but does not set the permissions of the web service, perhaps because the install method is not executed during an upgrade?

Edited by poel (see edit history)
Link to comment
Share on other sites

  • 11 months later...
On 1/11/2019 at 12:37 PM, poel said:

ok, I was wrong I just needed to give permission to the webservice (advanced parameters -> webservice). I was able to do it using the WebserviceKey class that has the method
setPermissionForAccount, and passing it a parameter containing the permissions extracted from a json file.
Here is my install function:


public function install()
    {

        $keyId = $this->getKeyId();
        $wsKey = new WebserviceKey($keyId ? $keyId : null);
        $wsKey->active = true;
        $wsKey->description = self::KEY_DESCRIPTION_MARKER;

        do {
            $wsKey->key = $this->generateKey(self::KEY_LENGTH);
        } while (WebserviceKey::keyExists($wsKey->key));

        $wsKey->add();

        $permissions_file = "../modules/".$this->name."/data/permissions.json";
        $jsonPermissions = file_get_contents($permissions_file); //TODO controlli sul file
        $permissions_to_set = json_decode($jsonPermissions,true);
        $wsKey->setPermissionForAccount($wsKey->id,$permissions_to_set);

        $configuration = new stdClass();
        $configuration->soap_key = $wsKey->key;

        Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration));

        return parent::install() &&
            $this->registerHook('actionAdminControllerSetMedia') &&
            $this->registerHook('displayBackOfficeHeader');
    }

 

in the permissions.json we have:


{
 "messages": {
    "GET": "on"
  },
  "order_carriers": {
    "GET": "on",
    "PUT": "on",
    "POST": "on"
  }
}

So the installation works fine, I'm trying to update my module via zip upload, in this case Prestashop updates the module to the correct version, but does not set the permissions of the web service, perhaps because the install method is not executed during an upgrade?

is the code missing somthing i have tried it but it dosn't seems to be working ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...