You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 satır
4.3KB

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