Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

340 lines
7.1KB

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