src/Entity/User.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\ActiveTrait;
  4. use App\Entity\Traits\DeleteTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Gedmo\Timestampable\Traits\TimestampableEntity;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @Gedmo\Loggable
  16. */
  17. #[UniqueEntity(fields: ['username'], errorPath: 'username', message: 'A user has already be created with that username.')]
  18. #[UniqueEntity(fields: ['email'], errorPath: 'email', message: 'The email has already be used by another user. If this user was previously deleted - ask your developer to recover his account')]
  19. #[ORM\Entity(repositoryClass: 'UserRepository')]
  20. #[ORM\Table(name: 'users')]
  21. class User implements UserInterface, PasswordAuthenticatedUserInterface, \Stringable
  22. {
  23. use ActiveTrait;
  24. use DeleteTrait;
  25. use TimestampableEntity;
  26. #[ORM\Id]
  27. #[ORM\GeneratedValue(strategy: 'AUTO')]
  28. #[ORM\Column(type: 'integer')]
  29. private ?int $id = null;
  30. #[Assert\NotBlank]
  31. #[ORM\Column(type: 'string', length: 25, unique: true)]
  32. private ?string $username = null;
  33. #[Assert\Length(max: 4096)]
  34. private ?string $plainPassword = null;
  35. #[ORM\Column(type: 'string', length: 64)]
  36. private ?string $password = null;
  37. #[Assert\NotBlank]
  38. #[Assert\Email]
  39. #[ORM\Column(type: 'string', length: 60, unique: true)]
  40. private ?string $email = null;
  41. #[ORM\Column(type: 'array')]
  42. private ?array $roles = null;
  43. #[ORM\Column(name: 'name', type: 'string', nullable: true)]
  44. private ?string $name = null;
  45. #[ORM\Column(name: 'profile_image', type: 'string', nullable: true)]
  46. private ?string $profile_image = null;
  47. #[ORM\OneToMany(targetEntity: \App\Entity\Page::class, mappedBy: 'updatedBy')]
  48. private ?Collection $pagesupdated = null;
  49. #[ORM\OneToMany(targetEntity: \App\Entity\Menu::class, mappedBy: 'updatedBy')]
  50. private ?Collection $menuupdated = null;
  51. #[ORM\Column(name: 'emailresetkey', type: 'string', nullable: true)]
  52. private ?string $emailresetkey = null;
  53. #[ORM\OneToMany(mappedBy: 'updatedBy', targetEntity: News::class, orphanRemoval: true)]
  54. private array|\Doctrine\Common\Collections\Collection $news;
  55. public function __construct()
  56. {
  57. $pagesupdated = new ArrayCollection();
  58. $menuupdated = new ArrayCollection();
  59. // may not be needed, see section on salt below
  60. // $this->salt = md5(uniqid(null, true));
  61. $this->news = new ArrayCollection();
  62. }
  63. public function __toString(): string
  64. {
  65. return (string) $this->username;
  66. }
  67. public function getUsername(): ?string
  68. {
  69. return $this->username;
  70. }
  71. public function getSalt(): ?string
  72. {
  73. // you *may* need a real salt depending on your encoder
  74. // see section on salt below
  75. return null;
  76. }
  77. public function getPassword(): ?string
  78. {
  79. return $this->password;
  80. }
  81. public function eraseCredentials(): void {}
  82. public function serialize(): string
  83. {
  84. return serialize([
  85. $this->id,
  86. $this->username,
  87. $this->password,
  88. $this->active,
  89. // see section on salt below
  90. // $this->salt,
  91. ]);
  92. }
  93. public function unserialize($serialized): void
  94. {
  95. [$this->id, $this->username, $this->password, $this->active] = unserialize($serialized);
  96. }
  97. public function getId(): int
  98. {
  99. return $this->id;
  100. }
  101. public function setUsername($username): self
  102. {
  103. $this->username = $username;
  104. return $this;
  105. }
  106. public function setPassword($password): self
  107. {
  108. $this->password = $password;
  109. return $this;
  110. }
  111. public function setEmail($email): self
  112. {
  113. $this->email = $email;
  114. return $this;
  115. }
  116. public function getEmail(): ?string
  117. {
  118. return $this->email;
  119. }
  120. public function isAccountNonExpired(): bool
  121. {
  122. return true;
  123. }
  124. public function isAccountNonLocked(): bool
  125. {
  126. return true;
  127. }
  128. public function isCredentialsNonExpired(): bool
  129. {
  130. return true;
  131. }
  132. public function isEnabled(): bool
  133. {
  134. return $this->isActive();
  135. }
  136. public function getRoles(): ?array
  137. {
  138. return $this->roles;
  139. }
  140. public function getPlainPassword(): ?string
  141. {
  142. return $this->plainPassword;
  143. }
  144. public function setPlainPassword($password): void
  145. {
  146. $this->plainPassword = $password;
  147. }
  148. public function setRoles($roles): self
  149. {
  150. $this->roles = $roles;
  151. return $this;
  152. }
  153. public function addRole($role): self
  154. {
  155. $role = strtoupper((string) $role);
  156. if (!in_array($role, $this->roles, true)) {
  157. $this->roles[] = $role;
  158. }
  159. return $this;
  160. }
  161. public function addPagesupdated(Page $pagesupdated): self
  162. {
  163. $this->pagesupdated[] = $pagesupdated;
  164. return $this;
  165. }
  166. public function removePagesupdated(Page $pagesupdated): void
  167. {
  168. $this->pagesupdated->removeElement($pagesupdated);
  169. }
  170. public function getPagesupdated(): ?Collection
  171. {
  172. return $this->pagesupdated;
  173. }
  174. public function getMenuupdated(): ?Collection
  175. {
  176. return $this->menuupdated;
  177. }
  178. public function setName($name): self
  179. {
  180. $this->name = $name;
  181. return $this;
  182. }
  183. public function getName(): ?string
  184. {
  185. return $this->name;
  186. }
  187. public function getFilePath(): string
  188. {
  189. return 'userfiles/images/user';
  190. }
  191. public function setProfileImage($profileImage): self
  192. {
  193. $this->profile_image = $profileImage;
  194. return $this;
  195. }
  196. public function getProfileImagePath(): string
  197. {
  198. if ('' == $this->profile_image) {
  199. return '/takeflight/user-placeholder.png';
  200. }
  201. return 'userfiles/images/user/'.$this->profile_image;
  202. }
  203. public function getProfileImage(): ?string
  204. {
  205. return $this->profile_image;
  206. }
  207. public function setEmailresetkey($emailresetkey): self
  208. {
  209. $this->emailresetkey = $emailresetkey;
  210. return $this;
  211. }
  212. public function getEmailresetkey(): ?string
  213. {
  214. return $this->emailresetkey;
  215. }
  216. public function getUserIdentifier()
  217. {
  218. return $this->username;
  219. }
  220. /**
  221. * @return Collection<int, News>
  222. */
  223. public function getNews(): Collection
  224. {
  225. return $this->news;
  226. }
  227. public function addNews(News $news): self
  228. {
  229. if (!$this->news->contains($news)) {
  230. $this->news[] = $news;
  231. $news->setUpdatedBy($this);
  232. }
  233. return $this;
  234. }
  235. public function removeNews(News $news): self
  236. {
  237. if ($this->news->removeElement($news)) {
  238. // set the owning side to null (unless already changed)
  239. if ($news->getUpdatedBy() === $this) {
  240. $news->setUpdatedBy(null);
  241. }
  242. }
  243. return $this;
  244. }
  245. }