Search code examples
phpprestashoplogoutprestashop-1.7backoffice

How to log out an admin user (employee) from back office in Prestashop 1.7?


I am developing a module for PrestaShop and I need to log out a user (employee) from the back office. I see in the back end code that they do it by using

// Find employee
$this->context->employee = new Employee();
$this->context->employee->getByEmail($email, $passwd);
$this->context->employee->logout();

But I can't use it in my module scope since I would need to intercept the credentials from back office login form to create a proper link to the employee, but I can't find a way how. Are there any ways to get these credentials or to log out the user in another way?

P.S. Intercepting user credentials before he logs in and blocking him from logging in until certain point would also satisfy my needs, but it sounds much more complicated and I also can't find a way to do this.


Solution

  • This code is enough to disconnect an employee:
    $this->context->employee->logout();

    But, as you can see it's in the context, which mean the code should be executed from the employee you want to disconnect because the session is saved in the local cookie of the employee, in other words, you can't disconnect remotely to an employee, but you can use the hook displayBackOfficeHeader which is called in every browsing page and then disconnect the employee, eg:

    public function hookDisplayBackOfficeHeader()
    {
        // My validation to disconnect the employee I want
        if ($id_employee == 1) {
            $this->context->employee->logout();
            Tools::redirectAdmin('index.php');
        }
    }