Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

179 lines
3.5KB

  1. <?php
  2. namespace Lc\SovBundle\Model\User;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Lc\SovBundle\Doctrine\EntityInterface;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. /**
  7. * @ORM\MappedSuperclass()
  8. */
  9. abstract class User implements EntityInterface, UserInterface
  10. {
  11. /**
  12. * @ORM\Column(type="string", length=180, unique=true)
  13. */
  14. protected $email;
  15. /**
  16. * @ORM\Column(type="json")
  17. */
  18. protected $roles = [];
  19. /**
  20. * @var string The hashed password
  21. * @ORM\Column(type="string")
  22. */
  23. protected $password;
  24. /**
  25. * @ORM\Column(type="string", length=255, nullable=true)
  26. */
  27. protected $lastname;
  28. /**
  29. * @ORM\Column(type="string", length=255, nullable=true)
  30. */
  31. protected $firstname;
  32. /**
  33. * @ORM\Column(type="boolean")
  34. */
  35. protected $isVerified = false;
  36. public function getEmail(): ?string
  37. {
  38. return $this->email;
  39. }
  40. public function setEmail(string $email): self
  41. {
  42. $this->email = $email;
  43. return $this;
  44. }
  45. /**
  46. * A visual identifier that represents this user.
  47. *
  48. * @see UserInterface
  49. */
  50. public function getUsername(): string
  51. {
  52. return (string)$this->email;
  53. }
  54. /**
  55. * @see UserInterface
  56. */
  57. public function getRoles(): array
  58. {
  59. $roles = $this->roles;
  60. // guarantee every user at least has ROLE_USER
  61. $roles[] = 'ROLE_USER';
  62. return array_unique($roles);
  63. }
  64. public function setRoles(array $roles): self
  65. {
  66. $this->roles = $roles;
  67. return $this;
  68. }
  69. public function hasRole($role)
  70. {
  71. return in_array(strtoupper($role), $this->getRoles(), true);
  72. }
  73. /**
  74. * @see UserInterface
  75. */
  76. public function getPassword(): string
  77. {
  78. return (string)$this->password;
  79. }
  80. public function setPassword(string $password): self
  81. {
  82. $this->password = $password;
  83. return $this;
  84. }
  85. public function generatePassword($length = 8): string
  86. {
  87. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  88. $count = mb_strlen($chars);
  89. for ($i = 0, $password = ''; $i < $length; $i++) {
  90. $index = rand(0, $count - 1);
  91. $password .= mb_substr($chars, $index, 1);
  92. }
  93. return $password;
  94. }
  95. /**
  96. * @see UserIn
  97. */
  98. public function getSalt()
  99. {
  100. // not needed when using the "bcrypt" algorithm in security.yaml
  101. }
  102. /**
  103. * @see UserInterface
  104. */
  105. public function eraseCredentials()
  106. {
  107. // If you store any temporary, sensitive data on the user, clear it here
  108. // $this->plainPassword = null;
  109. }
  110. public function getLastname(): ?string
  111. {
  112. return $this->lastname;
  113. }
  114. public function setLastname(?string $lastname): self
  115. {
  116. $this->lastname = $lastname;
  117. return $this;
  118. }
  119. public function getFirstname(): ?string
  120. {
  121. return $this->firstname;
  122. }
  123. public function setFirstname(?string $firstname): self
  124. {
  125. $this->firstname = $firstname;
  126. return $this;
  127. }
  128. public function getName(): ?string
  129. {
  130. return $this->getFirstname().' '.strtoupper($this->getLastname());
  131. }
  132. public function isVerified(): bool
  133. {
  134. return $this->isVerified;
  135. }
  136. public function setIsVerified(bool $isVerified): self
  137. {
  138. $this->isVerified = $isVerified;
  139. return $this;
  140. }
  141. }