User.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Entity;
  3. class User extends ModelBase
  4. {
  5. public function install()
  6. {
  7. $dbPrefix = $this->getDbPrefix();
  8. $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}user` (
  9. `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  10. `email` VARCHAR(128) NOT NULL UNIQUE,
  11. `password` VARCHAR(64),
  12. `sexe` ENUM('MALE', 'FEMALE') NULL,
  13. `optin` BOOLEAN DEFAULT FALSE,
  14. `register` DATETIME NOT NULL,
  15. `lastConnect` DATETIME NOT NULL,
  16. `lastVisit` DATETIME NOT NULL,
  17. `registerIp` VARCHAR(42) NOT NULL,
  18. `lastConnectIp` VARCHAR(42) NOT NULL,
  19. `lastVisitIp` VARCHAR(42) NOT NULL
  20. )");
  21. if ($result === false)
  22. throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
  23. return true;
  24. }
  25. public function __construct($id = null)
  26. {
  27. $this->register = $this->lastVisit = $this->lastConnect = new \DateTime();
  28. $this->registerIp = $this->lastVisitIp = $this->lastConnectIp = $_SERVER["REMOTE_ADDR"];
  29. parent::__construct($id);
  30. }
  31. public function checkPassword($value)
  32. {
  33. return password_verify($value, $this->password);
  34. }
  35. public function setPassword($value)
  36. {
  37. parent::__set("password", password_hash($value, PASSWORD_BCRYPT));
  38. }
  39. public function __set($key, $value)
  40. {
  41. if ($key == "password")
  42. return $this->setPassword($value);
  43. return parent::__set($key, $value);
  44. }
  45. }