No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

336 líneas
7.0KB

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