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: Page::class, mappedBy: 'updatedBy')]
  48. private ?Collection $pagesupdated = null;
  49. #[ORM\OneToMany(targetEntity: 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|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. {
  83. }
  84. public function serialize(): string
  85. {
  86. return serialize([
  87. $this->id,
  88. $this->username,
  89. $this->password,
  90. $this->active,
  91. // see section on salt below
  92. // $this->salt,
  93. ]);
  94. }
  95. public function unserialize($serialized): void
  96. {
  97. [$this->id, $this->username, $this->password, $this->active] = unserialize($serialized);
  98. }
  99. public function getId(): int
  100. {
  101. return $this->id;
  102. }
  103. public function setUsername($username): self
  104. {
  105. $this->username = $username;
  106. return $this;
  107. }
  108. public function setPassword($password): self
  109. {
  110. $this->password = $password;
  111. return $this;
  112. }
  113. public function setEmail($email): self
  114. {
  115. $this->email = $email;
  116. return $this;
  117. }
  118. public function getEmail(): ?string
  119. {
  120. return $this->email;
  121. }
  122. public function isAccountNonExpired(): bool
  123. {
  124. return true;
  125. }
  126. public function isAccountNonLocked(): bool
  127. {
  128. return true;
  129. }
  130. public function isCredentialsNonExpired(): bool
  131. {
  132. return true;
  133. }
  134. public function isEnabled(): bool
  135. {
  136. return $this->isActive();
  137. }
  138. public function getRoles(): ?array
  139. {
  140. return $this->roles;
  141. }
  142. public function getPlainPassword(): ?string
  143. {
  144. return $this->plainPassword;
  145. }
  146. public function setPlainPassword($password): void
  147. {
  148. $this->plainPassword = $password;
  149. }
  150. public function setRoles($roles): self
  151. {
  152. $this->roles = $roles;
  153. return $this;
  154. }
  155. public function addRole($role): self
  156. {
  157. $role = strtoupper((string) $role);
  158. if (!in_array($role, $this->roles, true)) {
  159. $this->roles[] = $role;
  160. }
  161. return $this;
  162. }
  163. public function addPagesupdated(Page $pagesupdated): self
  164. {
  165. $this->pagesupdated[] = $pagesupdated;
  166. return $this;
  167. }
  168. public function removePagesupdated(Page $pagesupdated): void
  169. {
  170. $this->pagesupdated->removeElement($pagesupdated);
  171. }
  172. public function getPagesupdated(): ?Collection
  173. {
  174. return $this->pagesupdated;
  175. }
  176. public function getMenuupdated(): ?Collection
  177. {
  178. return $this->menuupdated;
  179. }
  180. public function setName($name): self
  181. {
  182. $this->name = $name;
  183. return $this;
  184. }
  185. public function getName(): ?string
  186. {
  187. return $this->name;
  188. }
  189. public function getFilePath(): string
  190. {
  191. return 'userfiles/images/user';
  192. }
  193. public function setProfileImage($profileImage): self
  194. {
  195. $this->profile_image = $profileImage;
  196. return $this;
  197. }
  198. public function getProfileImagePath(): string
  199. {
  200. if ('' == $this->profile_image) {
  201. return '/takeflight/user-placeholder.png';
  202. }
  203. return 'userfiles/images/user/'.$this->profile_image;
  204. }
  205. public function getProfileImage(): ?string
  206. {
  207. return $this->profile_image;
  208. }
  209. public function setEmailresetkey($emailresetkey): self
  210. {
  211. $this->emailresetkey = $emailresetkey;
  212. return $this;
  213. }
  214. public function getEmailresetkey(): ?string
  215. {
  216. return $this->emailresetkey;
  217. }
  218. public function getUserIdentifier()
  219. {
  220. return $this->username;
  221. }
  222. /**
  223. * @return Collection<int, News>
  224. */
  225. public function getNews(): Collection
  226. {
  227. return $this->news;
  228. }
  229. public function addNews(News $news): self
  230. {
  231. if (!$this->news->contains($news)) {
  232. $this->news[] = $news;
  233. $news->setUpdatedBy($this);
  234. }
  235. return $this;
  236. }
  237. public function removeNews(News $news): self
  238. {
  239. if ($this->news->removeElement($news)) {
  240. // set the owning side to null (unless already changed)
  241. if ($news->getUpdatedBy() === $this) {
  242. $news->setUpdatedBy(null);
  243. }
  244. }
  245. return $this;
  246. }
  247. }