Jump to content

How to generate/get AdminProducts token in PrestaShop 1.7.x


Recommended Posts

Hi

I am trying to develop a module to help list products in various situations. How do I generate a token for at direct product link with PS 1.7.x? I can't seem to find any examples that helps.

I can generate tokens for customers and other stuff which works - and those tokens seems to be md5, but the tokens for AdminProducts seems to be another type of hash, like "HK5Z6KMpx_EP6-ykNM2Ju1QAkOqlFmskolAe3IXt8ss". Any help anyone?

Thanks for your answer.

Link to comment
Share on other sites

  • 2 weeks later...
  • 9 months later...

Hello, can anyone provide some informations on this subject? I saw some solutions similar to

 

$tokenProducts = Tools::getAdminToken(
            'AdminProducts'.
            (int)Tab::getIdFromClassName('AdminProducts').
            (int)$this->context->employee->id
        );

 

but the code I receive is always different from the one i see in the url in my browser after the _token parameter: index.php/product/catalog?_token=DIFFERENT_CODE_HERE

 

Edit: nevermind, have a look here.

Edited by Uff182 (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 10 months later...
  • 10 months later...

I have a issue I am stuck on for 3 days now.

I created a controller in a module (modulename/src/Controller) with the new Symfony system :

class TestController extends FrameworkBundleAdminController
{
    public static function getTest(){
        return new JsonResponse("test);
    }
}

And then declared the route in routes.yml as following:

getTest:
  path: mymodule/gettest
  methods: [GET]
  defaults:
    _controller: 'Mymodule\Controller\TestController::getTest'

When I call the URI adminfolder/modules/mymodule/gettest everything works fine but ONLY if i have the token of the module list page "http://...../adminfolder/index.php/improve/modules/manage"

The aim of this controller is to send a JSON response called by AJAX. The thing is I cant NEVER find how to generate the token. I have tried

Tools::getAdminTokenLite('whatever');

with every possible thing (AdminModules, AdminModulesManage, ModuleController, etc etc etc) and I never get the same token as I see in the admin module list page and I can't find how to get it even from my back office admin module. The token itself doesn't even lpooks the same : I get tokens like ..8ee59a385ba5225cc52b.. with the Tools::getAdminTokenLite() method and the token in http://...../adminfolder/index.php/improve/modules/manage looks like LettERSOnLYWithCaps-a_CapsLowAnd1234_CAPSAND123.

The only place I can find this token is in $_SESSION as a csrf_token, but as it appears in links of admin menu tab (when you click on "modules" tab), its taken from somewhere, but where ?

Could you tell me how to get this token that I can use it to reach my endpoint from ajax please ?

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

  • 1 year later...
  • 2 months later...
  • 2 years later...

Someone could bring more light to that issue. We have 2024 - Presta 8 now and same problem still there.

Not sure but did someone forget put something in docs about it?

It is impossible to generate the same token as in URL using Tools::getAdminTokenLite and in module controller getAdminLink does not work.

How is that?

Would appreciate some clever thoughts on that.

Thx

 

 

 

Link to comment
Share on other sites

On 7/19/2019 at 2:09 PM, soduno said:

And if somebody wondering how just to generate a token, you can use:

Tools::getAdminTokenLite('AdminOrders')

 

Yeah - but it gives different token - as mentioned above before- and then that gives 'invalid token' error.

Link to comment
Share on other sites

2 minutes ago, ps8modules said:

And can you give us the code with which you are trying to open the controller?

<?php

include_once(dirname(__FILE__) . '../../../mymodule.php');




class MyModuleModuleFrontController extends ModuleFrontController
{

 public function __construct()
    {
        parent::__construct();

        $this->context = Context::getContext();
    }

    public function initContent()
    {
        parent::initContent();

        $this->setTemplate('module:mymodule/views/templates/front/added.tpl');

        if(isset($_POST['order_ID']))
        {
			 $orderID = $_POST['order_ID'];
                $order = new Order($orderID);
				$products = $order->getProducts();
			  	$currencyID = $order->id_currency;
                    $currency = new Currency($currencyID);
                    $currencyCode = $currency->iso_code;

 				$IDCart = $order->id_cart;
                    $cart = new Cart($IDCart);

				    foreach($products as $product){
						/*some stuff here */
					}

				$customer = new Customer($order->id_customer);

				/*
					adding data to ERP
				*/

			$link = $this->context->link;
			
/*that dooes not work */
			$link = $this->context->link->getAdminLink('AdminOrders', true, ['id_order' => $orderID]);
			
			$New_link = Link::getAdminLink('AdminOrders', true, ['id_order' => $orderID, 'vieworder' => '']);
/**/

/*
I am trying here but can not get the same token as in url - that leads to invalid token error
*/				

      		$productLink  = _PS_BASE_URL_.__PS_BASE_URI__."adminurl/";
            $productLink .= 'index.php/sell/orders/';
            $productLink .= $orderID.'/view';
            $productLink .= '?_token='.Tools::getAdminTokenLite('AdminOrders');

/**/


			$this->context->smarty->assign([
              
                'link' => $link,
                'token' => $token
            ]);
  
 return $this->display(__FILE__, 'added.tpl');

        //    Tools::redirect($link);

		}


}

that is mymodule/controllers/front/mycontroller.php

And action is called when button on order preview is clicked (from .tpl file):

 <form action="{Context::getContext()->link->getModuleLink('mymodule', 'mycontroller')}" method="post">
                <input type="hidden" id="order_ID" name="order_ID" aria-label="order_ID" class="form-control" value="{$order_ID}">

                <input type="submit" class="btn btn-danger btn-sm mb-3" value="Send"/>
               
</form>

All stuff is being done - but I can not get link for admin orders to redirect after insert is done

Link to comment
Share on other sites

5 minutes ago, ps8modules said:

You cannot use the link to the controller in the administration for the Front controller !!!

Hmm so quess I need to make admin controller then - not the front one..

Link to comment
Share on other sites

I will try to explain I made an module that adds a button on order summary in BO. That buttons sends order details to ERP system and redirects to order page after action is done.

Now in my module I have created the admin controller (not front controller as before)  that does stuff need to be done.
However now I am strugglin with getting the link to to acess that admin controller from mymodule.

In mymodule I am using something like this

$link = Context::getContext()->link; 
$controllerUrl = $link->getAdminLink('MyAdminControllerName');

and then I am passing that link to tpl file with form (that shows extra button on order summary).

Now I get url but it is wrong - gives "Not such page" error.

So now GetAdminLink works - but I have trouble with setting it up to get link to my admin controller.

I hope I made it clear now.

Thanks

 

Link to comment
Share on other sites

I got a question: How to pass data from form in .tpl to admin controller?
 

.tpl file:

<form action="{Context::getContext()->link->getAdminLink('OrderManagerAdmin')}" method="post">
                <input type="hidden" id="order_ID" name="order_ID" aria-label="order_ID" class="form-control" value="{$order_ID}">
                <input type="submit" class="btn btn-danger btn-sm mb-3" value="Send data"/>

</form>

and how to display $_POSt data in admin controller?

in initContent() function ? in postProcess() ?

 public function postProcess() {
        // Process submitted forms
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
}
public function initContent()
{
	parent::initContent();
    echo "<pre>";
    	print_r($_POST);
	echo "</pre>";
}

Can not manage that.

My class in controller is like:

class AdminOrderManagerController extends AdminController {

}

 

Link to comment
Share on other sites

1.

<input type="submit" class="btn btn-danger btn-sm mb-3" name="my_submit_form" value="Send data"/>

2.

class AdminOrderManagerController extends ModuleAdminController
....
....
public function initContent()
{
    $orderId = ''; /* ????????????????????????? */
    $this->context->smarty->assign(
        array(
            'order_ID' => $orderId,
            '_form_action' => $this->context->link->getAdminLink('AdminOrderManager'),
        )
    );

    $this->content = $this->module->fetch('module:mymodule/views/templates/front/added.tpl').$this->postProcess();
    parent::initContent();
}

public function postProcess() {
    if (Tools::isSubmit('my_submit_form')) {
        if (Tools::getValue('order_ID')) {
            // your function
        }
    }
}

 

Edited by ps8modules (see edit history)
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...