User.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. #[ORM\Entity(repositoryClass: UserRepository::class)]
  12. #[ORM\Table(name: '`user`')]
  13. #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
  14. #[UniqueEntity('email')]
  15. class User implements UserInterface, PasswordAuthenticatedUserInterface
  16. {
  17. #[ORM\Id]
  18. #[ORM\GeneratedValue]
  19. #[ORM\Column]
  20. private ?int $id = null;
  21. #[ORM\Column(length: 180)]
  22. #[Assert\Email]
  23. private ?string $email = null;
  24. /**
  25. * @var list<string> The user roles
  26. */
  27. #[ORM\Column]
  28. private array $roles = [];
  29. /**
  30. * @var string The hashed password
  31. */
  32. #[ORM\Column]
  33. private ?string $password = null;
  34. /**
  35. * @var Collection<int, CommonList>
  36. */
  37. #[ORM\OneToMany(targetEntity: CommonList::class, mappedBy: 'addedBy')]
  38. private Collection $commonLists;
  39. /**
  40. * @var Collection<int, UserList>
  41. */
  42. #[ORM\OneToMany(targetEntity: UserList::class, mappedBy: 'viewer', orphanRemoval: true)]
  43. private Collection $userLists;
  44. #[ORM\Column(length: 255, nullable: true)]
  45. private ?string $name = null;
  46. public function __construct()
  47. {
  48. $this->commonLists = new ArrayCollection();
  49. $this->userLists = new ArrayCollection();
  50. }
  51. public function getId(): ?int
  52. {
  53. return $this->id;
  54. }
  55. public function getEmail(): ?string
  56. {
  57. return $this->email;
  58. }
  59. public function setEmail(string $email): static
  60. {
  61. $this->email = $email;
  62. return $this;
  63. }
  64. /**
  65. * A visual identifier that represents this user.
  66. *
  67. * @see UserInterface
  68. */
  69. public function getUserIdentifier(): string
  70. {
  71. return (string) $this->email;
  72. }
  73. /**
  74. * @see UserInterface
  75. *
  76. * @return list<string>
  77. */
  78. public function getRoles(): array
  79. {
  80. $roles = $this->roles;
  81. // guarantee every user at least has ROLE_USER
  82. $roles[] = 'ROLE_USER';
  83. return array_unique($roles);
  84. }
  85. /**
  86. * @param list<string> $roles
  87. */
  88. public function setRoles(array $roles): static
  89. {
  90. $this->roles = $roles;
  91. return $this;
  92. }
  93. /**
  94. * @see PasswordAuthenticatedUserInterface
  95. */
  96. public function getPassword(): ?string
  97. {
  98. return $this->password;
  99. }
  100. public function setPassword(string $password): static
  101. {
  102. $this->password = $password;
  103. return $this;
  104. }
  105. /**
  106. * @see UserInterface
  107. */
  108. public function eraseCredentials(): void
  109. {
  110. // If you store any temporary, sensitive data on the user, clear it here
  111. // $this->plainPassword = null;
  112. }
  113. /**
  114. * @return Collection<int, CommonList>
  115. */
  116. public function getCommonLists(): Collection
  117. {
  118. return $this->commonLists;
  119. }
  120. public function addCommonList(CommonList $commonList): static
  121. {
  122. if (!$this->commonLists->contains($commonList)) {
  123. $this->commonLists->add($commonList);
  124. $commonList->setAddedBy($this);
  125. }
  126. return $this;
  127. }
  128. public function removeCommonList(CommonList $commonList): static
  129. {
  130. if ($this->commonLists->removeElement($commonList)) {
  131. // set the owning side to null (unless already changed)
  132. if ($commonList->getAddedBy() === $this) {
  133. $commonList->setAddedBy(null);
  134. }
  135. }
  136. return $this;
  137. }
  138. /**
  139. * @return Collection<int, UserList>
  140. */
  141. public function getUserLists(): Collection
  142. {
  143. return $this->userLists;
  144. }
  145. public function addUserList(UserList $userList): static
  146. {
  147. if (!$this->userLists->contains($userList)) {
  148. $this->userLists->add($userList);
  149. $userList->setViewer($this);
  150. }
  151. return $this;
  152. }
  153. public function removeUserList(UserList $userList): static
  154. {
  155. if ($this->userLists->removeElement($userList)) {
  156. // set the owning side to null (unless already changed)
  157. if ($userList->getViewer() === $this) {
  158. $userList->setViewer(null);
  159. }
  160. }
  161. return $this;
  162. }
  163. public function getName(): ?string
  164. {
  165. return $this->name;
  166. }
  167. public function setName(?string $name): static
  168. {
  169. $this->name = $name;
  170. return $this;
  171. }
  172. public function getPseudo(): string
  173. {
  174. return $this->getName() ?? $this->getEmail();
  175. }
  176. }