您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

334 行
7.5KB

  1. <?php
  2. namespace Lc\SovBundle\Model\User;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Lc\SovBundle\Doctrine\EntityInterface;
  7. use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
  8. use Lc\SovBundle\Doctrine\Extension\DevAliasTrait;
  9. use Lc\SovBundle\Doctrine\Extension\TimestampableTrait;
  10. use Lc\SovBundle\Model\Ticket\TicketInterface;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Lc\SovBundle\Model\User\UserInterface as SovUserInterface;
  14. #[ORM\MappedSuperclass]
  15. abstract class UserModel implements EntityInterface, UserInterface, SovUserInterface, DevAliasInterface, PasswordAuthenticatedUserInterface
  16. {
  17. use DevAliasTrait;
  18. use TimestampableTrait;
  19. #[ORM\Column(type: 'string', length: 180, unique: true)]
  20. protected $email;
  21. #[ORM\Column(type: 'json')]
  22. protected $roles = [];
  23. /**
  24. * @var string The hashed password
  25. */
  26. #[ORM\Column(type: 'string')]
  27. protected $password;
  28. #[ORM\Column(type: "string", length: 255, nullable: true)]
  29. protected $lastname;
  30. #[ORM\Column(type: "string", length: 255, nullable: true)]
  31. protected $firstname;
  32. #[ORM\Column(type: 'string', length: 20, nullable: true)]
  33. protected $phone;
  34. #[ORM\Column(type: 'boolean', nullable: true)]
  35. protected $gender;
  36. #[ORM\Column(type: 'date', nullable: true)]
  37. protected $birthdate;
  38. #[ORM\Column(type: 'boolean')]
  39. protected $isVerified = false;
  40. #[ORM\ManyToMany(targetEntity: 'Lc\SovBundle\Model\User\GroupUserInterface', inversedBy: 'users')]
  41. protected $groupUsers;
  42. #[ORM\OneToMany(targetEntity: 'Lc\SovBundle\Model\Ticket\TicketInterface', mappedBy: 'user', cascade: ['remove'])]
  43. protected $tickets;
  44. #[ORM\Column(type: 'array', nullable: true)]
  45. protected $ticketTypesNotification = [];
  46. #[ORM\Column(type: 'datetime', nullable: true)]
  47. protected $lastLogin;
  48. public function __construct()
  49. {
  50. $this->tickets = new ArrayCollection();
  51. }
  52. public function getUserIdentifier(): string
  53. {
  54. return (string) $this->email;
  55. }
  56. public function __toString()
  57. {
  58. return '#' . $this->getId() . ' ' . strtoupper($this->getLastname()) . ' ' . $this->getFirstname(
  59. ) . ' (' . $this->getEmail() . ')';
  60. }
  61. public function getEmail(): ?string
  62. {
  63. return $this->email;
  64. }
  65. public function setEmail(string $email): self
  66. {
  67. $this->email = $email;
  68. return $this;
  69. }
  70. /**
  71. * A visual identifier that represents this user.
  72. *
  73. * @see UserInterface
  74. */
  75. public function getUsername(): string
  76. {
  77. return (string)$this->email;
  78. }
  79. public function getGender(): ?bool
  80. {
  81. return $this->gender;
  82. }
  83. public function setGender(?bool $gender): self
  84. {
  85. $this->gender = $gender;
  86. return $this;
  87. }
  88. public function getBirthdate(): ?\DateTimeInterface
  89. {
  90. return $this->birthdate;
  91. }
  92. public function setBirthdate(?\DateTimeInterface $birthdate): self
  93. {
  94. $this->birthdate = $birthdate;
  95. return $this;
  96. }
  97. /**
  98. * @see UserInterface
  99. */
  100. public function getRoles(): array
  101. {
  102. $roles = $this->roles;
  103. // guarantee every user at least has ROLE_USER
  104. $roles[] = 'ROLE_USER';
  105. return array_unique($roles);
  106. }
  107. public function setRoles(array $roles): self
  108. {
  109. $this->roles = $roles;
  110. return $this;
  111. }
  112. public function hasRole($role)
  113. {
  114. return in_array(strtoupper($role), $this->getRoles(), true);
  115. }
  116. /**
  117. * @see UserInterface
  118. */
  119. public function getPassword(): string
  120. {
  121. return (string)$this->password;
  122. }
  123. public function setPassword(string $password): self
  124. {
  125. $this->password = $password;
  126. return $this;
  127. }
  128. //TODO vérifier si on peut déplacer da Solver
  129. public function generatePassword($length = 8): string
  130. {
  131. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  132. $count = mb_strlen($chars);
  133. for ($i = 0, $password = ''; $i < $length; $i++) {
  134. $index = rand(0, $count - 1);
  135. $password .= mb_substr($chars, $index, 1);
  136. }
  137. return $password;
  138. }
  139. /**
  140. * @see UserIn
  141. */
  142. public function getSalt()
  143. {
  144. // not needed when using the "bcrypt" algorithm in security.yaml
  145. }
  146. /**
  147. * @see UserInterface
  148. */
  149. public function eraseCredentials()
  150. {
  151. // If you store any temporary, sensitive data on the user, clear it here
  152. // $this->plainPassword = null;
  153. }
  154. public function getLastname(): ?string
  155. {
  156. return $this->lastname;
  157. }
  158. public function setLastname(?string $lastname): self
  159. {
  160. $this->lastname = $lastname;
  161. return $this;
  162. }
  163. public function getFirstname(): ?string
  164. {
  165. return $this->firstname;
  166. }
  167. public function setFirstname(?string $firstname): self
  168. {
  169. $this->firstname = $firstname;
  170. return $this;
  171. }
  172. public function getPhone(): ?string
  173. {
  174. return $this->phone;
  175. }
  176. public function setPhone(?string $phone): self
  177. {
  178. $this->phone = $phone;
  179. return $this;
  180. }
  181. public function isVerified(): bool
  182. {
  183. return $this->isVerified;
  184. }
  185. public function setIsVerified(bool $isVerified): self
  186. {
  187. $this->isVerified = $isVerified;
  188. return $this;
  189. }
  190. /**
  191. * @return Collection|GroupUserInterface[]
  192. */
  193. public function getGroupUsers(): Collection
  194. {
  195. return $this->groupUsers;
  196. }
  197. public function addGroupUser(GroupUserInterface $groupUser): self
  198. {
  199. if (!$this->groupUsers->contains($groupUser)) {
  200. $this->groupUsers[] = $groupUser;
  201. $groupUser->addUser($this);
  202. }
  203. return $this;
  204. }
  205. public function removeGroupUser(GroupUserInterface $groupUser): self
  206. {
  207. if ($this->groupUsers->contains($groupUser)) {
  208. $this->groupUsers->removeElement($groupUser);
  209. $groupUser->removeUser($this);
  210. }
  211. return $this;
  212. }
  213. /**
  214. * @return Collection|TicketInterface[]
  215. */
  216. public function getTickets(): Collection
  217. {
  218. return $this->tickets;
  219. }
  220. public function addTicket(TicketInterface $ticket): self
  221. {
  222. if (!$this->tickets->contains($ticket)) {
  223. $this->tickets[] = $ticket;
  224. $ticket->setUser($this);
  225. }
  226. return $this;
  227. }
  228. public function removeTicket(TicketInterface $ticket): self
  229. {
  230. if ($this->tickets->contains($ticket)) {
  231. $this->tickets->removeElement($ticket);
  232. // set the owning side to null (unless already changed)
  233. if ($ticket->getUser() === $this) {
  234. $ticket->setUser(null);
  235. }
  236. }
  237. return $this;
  238. }
  239. public function getTicketTypesNotification(): ?array
  240. {
  241. return $this->ticketTypesNotification;
  242. }
  243. public function setTicketTypesNotification(?array $ticketTypesNotification): self
  244. {
  245. $this->ticketTypesNotification = $ticketTypesNotification;
  246. return $this;
  247. }
  248. public function getLastLogin()
  249. {
  250. return $this->lastLogin;
  251. }
  252. public function setLastLogin(\DateTime $time = null)
  253. {
  254. $this->lastLogin = $time;
  255. return $this;
  256. }
  257. }