src/Entity/User.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Abstract\Persona;
  4. use App\Entity\GroupTicket\PublicConnection;
  5. use App\Entity\GroupTicket\School;
  6. use App\Entity\GroupTicket\Teacher;
  7. use App\Entity\GroupTicket\TransportCompany;
  8. use App\Entity\Security\UserRole;
  9. use App\Repository\UserRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\DBAL\Types\Types;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. #[ORM\Entity(repositoryClass: UserRepository::class)]
  20. #[ORM\Table('user')]
  21. #[ORM\InheritanceType('SINGLE_TABLE')]
  22. #[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
  23. #[ORM\DiscriminatorMap([
  24. 'admin' => Admin::class,
  25. 'teacher' => Teacher::class,
  26. 'transport' => TransportCompany::class,
  27. 'school' => School::class])
  28. ]
  29. #[UniqueEntity(fields: ['email'], message: 'There is already an account with this email', entityClass: User::class)]
  30. abstract class User extends Persona implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
  31. {
  32. #[ORM\Column(length: 180, unique: true)]
  33. #[Assert\NotBlank]
  34. #[Assert\Email]
  35. protected ?string $email = null;
  36. #[ORM\Column]
  37. private ?string $password = null;
  38. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  39. private ?\DateTimeInterface $passwordChangedAt = null;
  40. #[ORM\Column(type: 'boolean')]
  41. private bool $isVerified = false;
  42. #[ORM\Column(length: 20, nullable: true)]
  43. private ?string $authCode = null;
  44. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  45. private ?\DateTimeInterface $validatedAt = null;
  46. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  47. private ?\DateTimeInterface $loginValidStart = null;
  48. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  49. private ?\DateTimeInterface $loginValidEnd = null;
  50. #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
  51. private ?\DateTimeInterface $lastLogin = null;
  52. #[ORM\ManyToMany(targetEntity: UserRole::class, inversedBy: 'users')]
  53. private Collection $userRoles;
  54. #[ORM\Column(type: Types::JSON, nullable: false)]
  55. private array $roles = [];
  56. #[ORM\ManyToOne(inversedBy: 'users')]
  57. private ?Tu $tu = null;
  58. #[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: Institution::class)]
  59. private Collection $institutions;
  60. #[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: Seller::class)]
  61. private Collection $sellers;
  62. #[ORM\ManyToMany(targetEntity: PublicConnection::class, inversedBy: 'users')]
  63. private Collection $groupTicketPublicConnection;
  64. #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserPasswordHistory::class, cascade: ['persist', 'remove'])]
  65. private Collection $passwordHistory;
  66. public function __construct()
  67. {
  68. $this->userRoles = new ArrayCollection();
  69. $this->institutions = new ArrayCollection();
  70. $this->sellers = new ArrayCollection();
  71. $this->groupTicketPublicConnection = new ArrayCollection();
  72. $this->passwordHistory = new ArrayCollection();
  73. }
  74. /**
  75. * A visual identifier that represents this user.
  76. *
  77. * @see UserInterface
  78. */
  79. public function getUserIdentifier(): string
  80. {
  81. return (string) $this->email;
  82. }
  83. /**
  84. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  85. */
  86. public function getUsername(): string
  87. {
  88. return (string) $this->email;
  89. }
  90. public function getRoles(): array
  91. {
  92. $roles = $this->roles;
  93. if (!in_array('ROLE_USER', $roles, true)) {
  94. $roles[] = 'ROLE_USER';
  95. }
  96. return array_unique($roles);
  97. }
  98. public function setRoles(array $roles): self
  99. {
  100. $this->roles = $roles;
  101. return $this;
  102. }
  103. public function hasRole($role): bool
  104. {
  105. return in_array($role, $this->getRoles()) || $this->hasUserRole($role);
  106. }
  107. /**
  108. * @param string|UserRole $role
  109. * @return bool
  110. */
  111. public function hasUserRole(UserRole|string $role): bool
  112. {
  113. if ($role instanceof UserRole) {
  114. return $this->userRoles->contains($role);
  115. }
  116. if (is_string($role)) {
  117. foreach ($this->userRoles as $userRole) {
  118. if ($userRole->getIdentifier() === $role) {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * @see PasswordAuthenticatedUserInterface
  127. */
  128. public function getPassword(): string
  129. {
  130. return $this->password;
  131. }
  132. public function setPassword(string $password): self
  133. {
  134. $this->password = $password;
  135. return $this;
  136. }
  137. public function getPasswordChangedAt(): ?\DateTimeInterface
  138. {
  139. return $this->passwordChangedAt;
  140. }
  141. public function setPasswordChangedAt(?\DateTimeInterface $passwordChangedAt): static
  142. {
  143. $this->passwordChangedAt = $passwordChangedAt;
  144. return $this;
  145. }
  146. /**
  147. * Returning a salt is only needed, if you are not using a modern
  148. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  149. *
  150. * @see UserInterface
  151. */
  152. public function getSalt(): ?string
  153. {
  154. return null;
  155. }
  156. /**
  157. * @see UserInterface
  158. */
  159. public function eraseCredentials()
  160. {
  161. // If you store any temporary, sensitive data on the user, clear it here
  162. // $this->plainPassword = null;
  163. }
  164. public function isVerified(): bool
  165. {
  166. return $this->isVerified;
  167. }
  168. public function setIsVerified(bool $isVerified): self
  169. {
  170. $this->isVerified = $isVerified;
  171. return $this;
  172. }
  173. public function getAuthCode(): ?string
  174. {
  175. return $this->authCode;
  176. }
  177. public function setAuthCode(?string $authCode): static
  178. {
  179. $this->authCode = $authCode;
  180. return $this;
  181. }
  182. public function isEmailAuthEnabled(): bool
  183. {
  184. if (in_array('ROLE_TEACHER', $this->roles)) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. public function getEmailAuthRecipient(): string
  190. {
  191. return $this->email;
  192. }
  193. public function getEmailAuthCode(): string
  194. {
  195. if (null === $this->authCode) {
  196. throw new \LogicException('The email authentication code was not set');
  197. }
  198. return $this->authCode;
  199. }
  200. public function setEmailAuthCode(string $authCode): void
  201. {
  202. $this->authCode = $authCode;
  203. }
  204. public function getFullName(): string
  205. {
  206. return $this->getName();
  207. }
  208. public function getValidatedAt(): ?\DateTimeInterface
  209. {
  210. return $this->validatedAt;
  211. }
  212. public function setValidatedAt(?\DateTimeInterface $validatedAt): static
  213. {
  214. $this->validatedAt = $validatedAt;
  215. return $this;
  216. }
  217. public function getLoginValidStart(): ?\DateTimeInterface
  218. {
  219. return $this->loginValidStart;
  220. }
  221. public function setLoginValidStart(?\DateTimeInterface $loginValidStart): static
  222. {
  223. $this->loginValidStart = $loginValidStart;
  224. return $this;
  225. }
  226. public function getLoginValidEnd(): ?\DateTimeInterface
  227. {
  228. return $this->loginValidEnd;
  229. }
  230. public function setLoginValidEnd(?\DateTimeInterface $loginValidEnd): static
  231. {
  232. $this->loginValidEnd = $loginValidEnd;
  233. return $this;
  234. }
  235. public function getLastLogin(): ?\DateTimeInterface
  236. {
  237. return $this->lastLogin;
  238. }
  239. public function setLastLogin(?\DateTimeInterface $lastLogin): static
  240. {
  241. $this->lastLogin = $lastLogin;
  242. return $this;
  243. }
  244. public function isExpired(): bool
  245. {
  246. $currentDate = new \DateTime();
  247. $validStart = $this->getLoginValidStart();
  248. $validEnd = $this->getLoginValidEnd();
  249. if ($validStart !== null && $validEnd !== null) {
  250. if ($currentDate < $validStart || $currentDate > $validEnd) {
  251. return true;
  252. }
  253. } elseif ($validStart !== null && $currentDate < $validStart) {
  254. return true;
  255. } elseif ($validEnd !== null && $currentDate > $validEnd) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. public function updateEmailForDelete(): void
  261. {
  262. if(!empty($this->email)){
  263. $this->email = time().'-'.$this->email;
  264. }
  265. }
  266. public function addUserRole(UserRole $role): self
  267. {
  268. if (!$this->userRoles->contains($role)) {
  269. $this->userRoles->add($role);
  270. if(in_array($role->getIdentifier(), $this->roles) === false){
  271. $this->roles[] = $role->getIdentifier();
  272. }
  273. }
  274. return $this;
  275. }
  276. public function removeUserRole(UserRole $role): self
  277. {
  278. if ($this->userRoles->contains($role)) {
  279. $this->userRoles->removeElement($role);
  280. $this->roles = array_filter($this->roles, fn($r) => $r !== $role->getIdentifier());
  281. }
  282. return $this;
  283. }
  284. public function clearUserRoles(): self
  285. {
  286. $this->userRoles->clear();
  287. $this->roles = [];
  288. return $this;
  289. }
  290. public function getUserRoles(): Collection
  291. {
  292. return $this->userRoles;
  293. }
  294. public function getTu(): ?Tu
  295. {
  296. return $this->tu;
  297. }
  298. public function setTu(?Tu $tu): static
  299. {
  300. $this->tu = $tu;
  301. return $this;
  302. }
  303. /**
  304. * @return Collection<int, Institution>
  305. */
  306. public function getInstitutions(): Collection
  307. {
  308. return $this->institutions;
  309. }
  310. public function addInstitution(Institution $institution): static
  311. {
  312. if (!$this->institutions->contains($institution)) {
  313. $this->institutions->add($institution);
  314. $institution->setCreatedBy($this);
  315. }
  316. return $this;
  317. }
  318. public function removeInstitution(Institution $institution): static
  319. {
  320. if ($this->institutions->removeElement($institution)) {
  321. // set the owning side to null (unless already changed)
  322. if ($institution->getCreatedBy() === $this) {
  323. $institution->setCreatedBy(null);
  324. }
  325. }
  326. return $this;
  327. }
  328. /**
  329. * @return Collection<int, Seller>
  330. */
  331. public function getSellers(): Collection
  332. {
  333. return $this->sellers;
  334. }
  335. public function addSeller(Seller $seller): static
  336. {
  337. if (!$this->sellers->contains($seller)) {
  338. $this->sellers->add($seller);
  339. $seller->setCreatedBy($this);
  340. }
  341. return $this;
  342. }
  343. public function removeSeller(Seller $seller): static
  344. {
  345. if ($this->sellers->removeElement($seller)) {
  346. // set the owning side to null (unless already changed)
  347. if ($seller->getCreatedBy() === $this) {
  348. $seller->setCreatedBy(null);
  349. }
  350. }
  351. return $this;
  352. }
  353. /**
  354. * @return Collection<int, PublicConnection>
  355. */
  356. public function getGroupTicketPublicConnection(): Collection
  357. {
  358. return $this->groupTicketPublicConnection;
  359. }
  360. public function addGroupTicketPublicConnection(PublicConnection $groupTicketPublicConnection): static
  361. {
  362. if (!$this->groupTicketPublicConnection->contains($groupTicketPublicConnection)) {
  363. $this->groupTicketPublicConnection->add($groupTicketPublicConnection);
  364. }
  365. return $this;
  366. }
  367. public function removeGroupTicketPublicConnection(PublicConnection $groupTicketPublicConnection): static
  368. {
  369. $this->groupTicketPublicConnection->removeElement($groupTicketPublicConnection);
  370. return $this;
  371. }
  372. public function getPasswordHistory(): Collection
  373. {
  374. return $this->passwordHistory;
  375. }
  376. public function addPasswordHistory(UserPasswordHistory $entry): static
  377. {
  378. if (!$this->passwordHistory->contains($entry)) {
  379. $this->passwordHistory->add($entry);
  380. $entry->setUser($this);
  381. }
  382. return $this;
  383. }
  384. public function clearPasswordHistory(): static
  385. {
  386. foreach ($this->passwordHistory as $entry) {
  387. $this->passwordHistory->removeElement($entry);
  388. }
  389. return $this;
  390. }
  391. }