Jump to content

creating a module that activate API with specific rights and finally get the key


Recommended Posts

Dear Community,

I was trying to create a module that activate the API and set the rights then it generate the key of this created API still i didn't found any documentation that matchs my needs. this need to be done from the module not manually from prestashop ( back-end code ).

thanks

PS: i am new to prestashop please help 

Share this post


Link to post
Share on other sites
On 1/9/2020 at 10:57 AM, kimokimo said:

Dear Community,

I was trying to create a module that activate the API and set the rights then it generate the key of this created API still i didn't found any documentation that matchs my needs. this need to be done from the module not manually from prestashop ( back-end code ).

thanks

PS: i am new to prestashop please help 

Hi,

You have to save  the  API  KEY  in  a configuration  value  and  call  it where you need it in your  module.

 

Edited by ndiaga (see edit history)
  • Confused 1

Share this post


Link to post
Share on other sites
On 1/13/2020 at 5:29 PM, ndiaga said:

Hi,

You have  save  the  API  KEY  in  a configuration  value  and  call  it where you need it in your  module.

 

could you please go further in details ? how do i save the API KEY in a configuraiton value ? 

Share this post


Link to post
Share on other sites
2 hours ago, kimokimo said:

could you please go further in details ? how do i save the API KEY in a configuraiton value ? 

Can you please share your code? 

Share this post


Link to post
Share on other sites
1 hour ago, ndiaga said:

Can you please share your code? 

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');
    }

i was trying this code that i found in this post https://www.prestashop.com/forums/topic/947964-how-to-set-up-permission-for-user-programmatically/?_fromLogin=1

but unfortunatly it dosn't seems to work its missing some parts i guess

 

 

Share this post


Link to post
Share on other sites

Just  you did not  put  the configuration  value in the right place.

Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration));   //this  should go inside the condition

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


//  do   this :
        return (parent::install() &&
            $this->registerHook('actionAdminControllerSetMedia') &&
            $this->registerHook('displayBackOfficeHeader') &&
            Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration))
            );

 

Share this post


Link to post
Share on other sites
24 minutes ago, ndiaga said:

Just  you did not  put  the configuration  value in the right place.


Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration));   //this  should go inside the condition

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


//  do   this :
        return (parent::install() &&
            $this->registerHook('actionAdminControllerSetMedia') &&
            $this->registerHook('displayBackOfficeHeader') &&
            Configuration::updateValue('XXX_CONFIGURATION', json_encode($configuration))
            );

 

what about $keyId = $this->getKeyId(); ? is this predifined in prestashop ?

Share this post


Link to post
Share on other sites
42 minutes ago, kimokimo said:

what about $keyId = $this->getKeyId(); ? is this predifined in prestashop ?

No,  the  PrestaShop  function  to save  a configuration  value is  :  Configuration::updateValue('Variable_Namae',   'Value t save  here' )   

Share this post


Link to post
Share on other sites
3 hours ago, ndiaga said:

No,  the  PrestaShop  function  to save  a configuration  value is  :  Configuration::updateValue('Variable_Namae',   'Value t save  here' )   

i have tried changing what you propsed but unlucky. the problem isn't solved because the system dosn't recognize the key ID

$keyId = $this->getKeyId();

can you please try running the code.

Share this post


Link to post
Share on other sites
1 minute ago, kimokimo said:

i have tried changing what you propsed but unlucky. the problem isn't solved because the system dosn't recognize the key ID


$keyId = $this->getKeyId();

can you please try running the code.

Running it where?

 

Share this post


Link to post
Share on other sites
17 minutes ago, kimokimo said:

try to run the code on your machine !

From what  I read in your code you don't even need to save   data in the configuration.

You can  just  call  the  json_encode($configuration)       where you want  it to be .

 

Share this post


Link to post
Share on other sites
Just now, ndiaga said:

From what  I read in your code you don't even need to save   data in the configuration.

You can  just  call  the  json_encode($configuration)       where you want  it to be .

 

i tried to forget about that part and leave only the rest of the code, but that didn't make it work. what i come to is that the problem is come to the first instruction wich is the one i mentioned earlier 

$keyId = $this->getKeyId();

do i have by deafault the $this->getKeyId() funciton in the install() function ? or in module ? or that i need to impliment some code to get it working ?

Share this post


Link to post
Share on other sites
2 minutes ago, kimokimo said:

i tried to forget about that part and leave only the rest of the code, but that didn't make it work. what i come to is that the problem is come to the first instruction wich is the one i mentioned earlier 


$keyId = $this->getKeyId();

do i have by deafault the $this->getKeyId() funciton in the install() function ? or in module ? or that i need to impliment some code to get it working ?

I see you just  copy  from here?

 

Share this post


Link to post
Share on other sites
1 minute ago, ndiaga said:

I see you just  copy  from here?

 

yes, that's what i have done. i am trying to create a Module that activate the API by code like it will be automated. then i just found that code seems to be talking about what i wanna do so i just copy paste in install() function but that dosn't seems to be working.

Share this post


Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More