User.php 2.4 KB

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