Admin.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Entity;
  3. class Admin extends ModelBase
  4. {
  5. /**
  6. * Role -> 0 : no access
  7. * -> 1 : read access
  8. * -> 2 : +write access
  9. * -> 3 : +delete /disable access
  10. *
  11. * [0] -> admin (add user, change site configuration)
  12. * [1] -> cms (add / modify / delete pages)
  13. * [2] -> products
  14. * [3] -> orders
  15. * [4] -> translations
  16. *
  17. * '*' Is the site admin, have all rights and no one can change this role
  18. **/
  19. public function install()
  20. {
  21. $dbPrefix = $this->getDbPrefix();
  22. $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}admin` (
  23. `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  24. `email` VARCHAR(128) NOT NULL UNIQUE,
  25. `password` VARCHAR(64),
  26. `role` VARCHAR(8) NOT NULL DEFAULT '00000',
  27. `lastConnect` DATETIME NOT NULL,
  28. `lastConnectIp` VARCHAR(42) NOT NULL
  29. )");
  30. if ($result === false)
  31. throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
  32. return true;
  33. }
  34. public function __construct($id = null)
  35. {
  36. $this->lastConnect = new \DateTime();
  37. $this->lastConnectIp = \Tools\Context::getContext()->ip;
  38. parent::__construct($id);
  39. }
  40. public function checkPassword($value)
  41. {
  42. return password_verify($value, $this->password);
  43. }
  44. public function setPassword($value)
  45. {
  46. parent::__set("password", password_hash($value, PASSWORD_BCRYPT));
  47. }
  48. public function __set($key, $value)
  49. {
  50. if ($key == "password")
  51. return $this->setPassword($value);
  52. return parent::__set($key, $value);
  53. }
  54. }