You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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