User.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Tree\Fixture;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
  6. * @ORM\Table(name="user")
  7. */
  8. class User extends Role {
  9. const PASSWORD_SALT = 'dfJko$~346958rg!DFT]AEtzserf9giq)3/TAeg;aDFa43';
  10. /**
  11. * @ORM\Column(name="email", type="string", unique=true)
  12. * @var string
  13. */
  14. private $email;
  15. /**
  16. * @ORM\Column(name="password_hash", type="string", length=32)
  17. * @var string
  18. */
  19. private $passwordHash;
  20. /**
  21. * @ORM\Column(name="activation_code", type="string", length=12)
  22. * @var string
  23. */
  24. private $activationCode;
  25. /**
  26. * @param string $email
  27. * @param string $password
  28. */
  29. public function __construct($email, $password) {
  30. parent::__construct();
  31. $this
  32. ->setEmail($email)
  33. ->setPassword($password);
  34. }
  35. public function init() {
  36. $this->setActivationCode($this->generateString(12));
  37. }
  38. /**
  39. * Generates a random password
  40. *
  41. * @param int $length
  42. * @return string
  43. */
  44. public function generateString($length = 8) {
  45. $length = (int) $length;
  46. if ($length < 0) {
  47. throw new \Exception("Invalid password length '$length'");
  48. }
  49. $set = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  50. $num = strlen($set);
  51. $ret = '';
  52. for ($i = 0; $i < $length; $i++) {
  53. $ret .= $set[rand(0, $num - 1)];
  54. }
  55. return $ret;
  56. }
  57. /**
  58. * Generates a password hash
  59. *
  60. * @param string $password
  61. * @return string
  62. */
  63. public function generatePasswordHash($password) {
  64. return md5($password . self::PASSWORD_SALT);
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getEmail() {
  70. return $this->email;
  71. }
  72. /**
  73. * @param string $email
  74. * @return User
  75. */
  76. public function setEmail($email) {
  77. $this->email = $email;
  78. $this->setRoleId($email);
  79. return $this;
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getPasswordHash() {
  85. return $this->passwordHash;
  86. }
  87. /**
  88. * @param string $password
  89. * @return User
  90. */
  91. public function setPassword($password) {
  92. $this->passwordHash = $this->generatePasswordHash(trim($password));
  93. return $this;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function getActivationCode() {
  99. return $this->activationCode;
  100. }
  101. /**
  102. * @param string $activationCode
  103. * @return User
  104. */
  105. public function setActivationCode($activationCode) {
  106. $this->activationCode = $activationCode;
  107. return $this;
  108. }
  109. }