Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

355 Zeilen
7.4KB

  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. /**
  70. * @ORM\Column(type="datetime", nullable=true)
  71. */
  72. protected $lastLogin;
  73. public function __construct()
  74. {
  75. $this->tickets = new ArrayCollection();
  76. }
  77. public function __toString()
  78. {
  79. return '#' . $this->getId() . ' ' . strtoupper($this->getLastname()) . ' ' . $this->getFirstname(
  80. ) . ' (' . $this->getEmail() . ')';
  81. }
  82. public function getEmail(): ?string
  83. {
  84. return $this->email;
  85. }
  86. public function setEmail(string $email): self
  87. {
  88. $this->email = $email;
  89. return $this;
  90. }
  91. /**
  92. * A visual identifier that represents this user.
  93. *
  94. * @see UserInterface
  95. */
  96. public function getUsername(): string
  97. {
  98. return (string)$this->email;
  99. }
  100. public function getGender(): ?bool
  101. {
  102. return $this->gender;
  103. }
  104. public function setGender(?bool $gender): self
  105. {
  106. $this->gender = $gender;
  107. return $this;
  108. }
  109. public function getBirthdate(): ?\DateTimeInterface
  110. {
  111. return $this->birthdate;
  112. }
  113. public function setBirthdate(?\DateTimeInterface $birthdate): self
  114. {
  115. $this->birthdate = $birthdate;
  116. return $this;
  117. }
  118. /**
  119. * @see UserInterface
  120. */
  121. public function getRoles(): array
  122. {
  123. $roles = $this->roles;
  124. // guarantee every user at least has ROLE_USER
  125. $roles[] = 'ROLE_USER';
  126. return array_unique($roles);
  127. }
  128. public function setRoles(array $roles): self
  129. {
  130. $this->roles = $roles;
  131. return $this;
  132. }
  133. public function hasRole($role)
  134. {
  135. return in_array(strtoupper($role), $this->getRoles(), true);
  136. }
  137. /**
  138. * @see UserInterface
  139. */
  140. public function getPassword(): string
  141. {
  142. return (string)$this->password;
  143. }
  144. public function setPassword(string $password): self
  145. {
  146. $this->password = $password;
  147. return $this;
  148. }
  149. //TODO vérifier si on peut déplacer da Solver
  150. public function generatePassword($length = 8): string
  151. {
  152. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  153. $count = mb_strlen($chars);
  154. for ($i = 0, $password = ''; $i < $length; $i++) {
  155. $index = rand(0, $count - 1);
  156. $password .= mb_substr($chars, $index, 1);
  157. }
  158. return $password;
  159. }
  160. /**
  161. * @see UserIn
  162. */
  163. public function getSalt()
  164. {
  165. // not needed when using the "bcrypt" algorithm in security.yaml
  166. }
  167. /**
  168. * @see UserInterface
  169. */
  170. public function eraseCredentials()
  171. {
  172. // If you store any temporary, sensitive data on the user, clear it here
  173. // $this->plainPassword = null;
  174. }
  175. public function getLastname(): ?string
  176. {
  177. return $this->lastname;
  178. }
  179. public function setLastname(?string $lastname): self
  180. {
  181. $this->lastname = $lastname;
  182. return $this;
  183. }
  184. public function getFirstname(): ?string
  185. {
  186. return $this->firstname;
  187. }
  188. public function setFirstname(?string $firstname): self
  189. {
  190. $this->firstname = $firstname;
  191. return $this;
  192. }
  193. public function getPhone(): ?string
  194. {
  195. return $this->phone;
  196. }
  197. public function setPhone(?string $phone): self
  198. {
  199. $this->phone = $phone;
  200. return $this;
  201. }
  202. public function isVerified(): bool
  203. {
  204. return $this->isVerified;
  205. }
  206. public function setIsVerified(bool $isVerified): self
  207. {
  208. $this->isVerified = $isVerified;
  209. return $this;
  210. }
  211. /**
  212. * @return Collection|GroupUserInterface[]
  213. */
  214. public function getGroupUsers(): Collection
  215. {
  216. return $this->groupUsers;
  217. }
  218. public function addGroupUser(GroupUserInterface $groupUser): self
  219. {
  220. if (!$this->groupUsers->contains($groupUser)) {
  221. $this->groupUsers[] = $groupUser;
  222. $groupUser->addUser($this);
  223. }
  224. return $this;
  225. }
  226. public function removeGroupUser(GroupUserInterface $groupUser): self
  227. {
  228. if ($this->groupUsers->contains($groupUser)) {
  229. $this->groupUsers->removeElement($groupUser);
  230. $groupUser->removeUser($this);
  231. }
  232. return $this;
  233. }
  234. /**
  235. * @return Collection|TicketInterface[]
  236. */
  237. public function getTickets(): Collection
  238. {
  239. return $this->tickets;
  240. }
  241. public function addTicket(TicketInterface $ticket): self
  242. {
  243. if (!$this->tickets->contains($ticket)) {
  244. $this->tickets[] = $ticket;
  245. $ticket->setUser($this);
  246. }
  247. return $this;
  248. }
  249. public function removeTicket(TicketInterface $ticket): self
  250. {
  251. if ($this->tickets->contains($ticket)) {
  252. $this->tickets->removeElement($ticket);
  253. // set the owning side to null (unless already changed)
  254. if ($ticket->getUser() === $this) {
  255. $ticket->setUser(null);
  256. }
  257. }
  258. return $this;
  259. }
  260. public function getTicketTypesNotification(): ?array
  261. {
  262. return $this->ticketTypesNotification;
  263. }
  264. public function setTicketTypesNotification(?array $ticketTypesNotification): self
  265. {
  266. $this->ticketTypesNotification = $ticketTypesNotification;
  267. return $this;
  268. }
  269. public function getLastLogin()
  270. {
  271. return $this->lastLogin;
  272. }
  273. public function setLastLogin(\DateTime $time = null)
  274. {
  275. $this->lastLogin = $time;
  276. return $this;
  277. }
  278. }