For some people the way of logging in to BackOffice in PrestaShop might be quite uncomfortable, because everytime they have to type (long!) email address (instead login).
This tutorial shows the necessary changes to make login via username to BackOffice possible.
Firstly, you need to store somewhere your new login
field. I think the best place for it is adding a new column in the ps_employee table. Go to your phpMyAdmin and execute this SQL command.
ALTER TABLE ps_employee ADD COLUMN login VARCHAR(100)
Then, find in that table a row with person, who you want to set login and add it.
Now it’s time to edit a login form.
Find:
Replace with:
Edit: /controllers/admin/AdminLoginController.phpNext, let’s modify AdminLogin Controller. You have to add there our new field called
loginfield
and remove email address validation.Find:
$email = trim(Tools::getValue('email')); if (empty($email)) $this->errors[] = Tools::displayError('Email is empty.'); elseif (!Validate::isEmail($email)) $this->errors[] = Tools::displayError('Invalid email address.');Replace with:
$login = trim(Tools::getValue('loginfield')); if (empty($login)) $this->errors[] = Tools::displayError('Login is empty.');There is also a need to replace the function which checks user credentials. Create call to a new function
getByLogin
.Find:
getByEmail($email, $passwd);Replace with:
getByLogin($login, $passwd);Edit: /classes/Employee.phpNow, extend your Employee class by adding a new function
getByLogin
.Find:
public function getByEmail($email, $passwd = null) { if (!Validate::isEmail($email) || ($passwd != null && !Validate::isPasswd($passwd))) die(Tools::displayError()); $result = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'employee` WHERE `active` = 1 AND `email` = \''.pSQL($email).'\' '.($passwd !== null ? 'AND `passwd` = \''.Tools::encrypt($passwd).'\'' : '')); if (!$result) return false; $this->id = $result['id_employee']; $this->id_profile = $result['id_profile']; foreach ($result as $key => $value) if (property_exists($this, $key)) $this->{$key} = $value; return $this; }Add after:
public function getByLogin($login, $passwd = null) { if (($passwd != null && !Validate::isPasswd($passwd))) die(Tools::displayError()); $result = Db::getInstance()->getRow(' SELECT * FROM `'._DB_PREFIX_.'employee` WHERE `active` = 1 AND `login` = \''.pSQL($login).'\' '.($passwd !== null ? 'AND `passwd` = \''.Tools::encrypt($passwd).'\'' : '')); if (!$result) return false; $this->id = $result['id_employee']; $this->id_profile = $result['id_profile']; foreach ($result as $key => $value) if (property_exists($this, $key)) $this->{$key} = $value; return $this; }Edit: /js/admin/login.jsThe last thing to do is modifying the required field in JavaScript.
Find:
"email":{ "email": true,Replace with:
"loginfield":{ "loginfield": true,Set overrides
Last and very important thing is to move all Your changes to
override/
directory.
1. Copy and paste files:
Source Destination controllers/admin/AdminLoginController.php override/controllers/admin/AdminLoginController.php classes/Employee.php override/classes/Employee.php 2. Replace in DESTINATION files:
–class AdminLoginControllerCore extends AdminController
replace withclass AdminLoginController extends AdminLoginControllerCore
–class EmployeeCore extends ObjectModel
replace withclass Employee extends EmployeeCore
3. Purge cache:
Remove/cache/class_index.php
file.