Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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