src/Entity/User.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\Identifiable;
  4. use App\Entity\Traits\Timestampable;
  5. use App\Entity\Webmail\AccountAccess;
  6. use App\Repository\UserRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  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. #[ORM\Entity(repositoryClassUserRepository::class)]
  14. #[UniqueEntity(['email'], message'Un espace SCUF associé à cet email existe déjà')]
  15. class User implements UserInterfacePasswordAuthenticatedUserInterface\Serializable
  16. {
  17.     use Timestampable;
  18.     use Identifiable;
  19.     #[ORM\Column(type'string'length180uniquetrue)]
  20.     private $email;
  21.     #[ORM\Column(type'json')]
  22.     private $roles = [];
  23.     #[ORM\Column(type'string')]
  24.     private $password;
  25.     private ?string $plainPassword null;
  26.     #[ORM\Column(type'string'length255nullabletrue)]
  27.     private $userAgent;
  28.     #[ORM\Column(type'string'length15nullabletrue)]
  29.     private $ip;
  30.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  31.     private $lastLoginAt;
  32.     #[ORM\Column(type'string'length255nullabletrue)]
  33.     private $resetToken;
  34.     #[ORM\OneToOne(mappedBy'user'targetEntityPerson::class, cascade: ['persist''remove'])]
  35.     private $person;
  36.     #[ORM\OneToMany(mappedBy'user'targetEntityAdminSection::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  37.     private $adminSections;
  38.     #[ORM\Column(type'boolean')]
  39.     private $isTemporary false;
  40.     #[ORM\OneToMany(mappedBy'user'targetEntityAccountAccess::class)]
  41.     private Collection $webmailAccountAccesses;
  42.     public function __construct()
  43.     {
  44.         $this->adminSections = new ArrayCollection();
  45.         $this->webmailAccountAccesses = new ArrayCollection();
  46.     }
  47.     public function getEmail(): ?string
  48.     {
  49.         return $this->email;
  50.     }
  51.     public function setEmail(string $email): self
  52.     {
  53.         $this->email $email;
  54.         return $this;
  55.     }
  56.     /**
  57.      * A visual identifier that represents this user.
  58.      *
  59.      * @see UserInterface
  60.      */
  61.     public function getUserIdentifier(): string
  62.     {
  63.         return (string)$this->email;
  64.     }
  65.     /**
  66.      * @see UserInterface
  67.      */
  68.     public function getRoles(): array
  69.     {
  70.         $roles $this->roles;
  71.         // guarantee every user at least has ROLE_USER
  72.         $roles[] = 'ROLE_USER';
  73.         return array_unique($roles);
  74.     }
  75.     public function setRoles(array $roles): self
  76.     {
  77.         $this->roles $roles;
  78.         return $this;
  79.     }
  80.     public function addRole($role): self
  81.     {
  82.         if (!in_array($role$this->rolestrue)) {
  83.             $this->roles[] = $role;
  84.         }
  85.         return $this;
  86.     }
  87.     public function hasRole($role): bool
  88.     {
  89.         return in_array($role$this->rolestrue);
  90.     }
  91.     public function removeRole($role): self
  92.     {
  93.         if (($key array_search($role$this->rolestrue)) !== false) {
  94.             unset($this->roles[$key]);
  95.         }
  96.         return $this;
  97.     }
  98.     /**
  99.      * @see PasswordAuthenticatedUserInterface
  100.      */
  101.     public function getPassword(): string
  102.     {
  103.         return $this->password;
  104.     }
  105.     public function setPassword(string $password): self
  106.     {
  107.         $this->password $password;
  108.         return $this;
  109.     }
  110.     /**
  111.      * @see UserInterface
  112.      */
  113.     public function eraseCredentials()
  114.     {
  115.         $this->plainPassword null;
  116.     }
  117.     public function getPlainPassword(): ?string
  118.     {
  119.         return $this->plainPassword;
  120.     }
  121.     public function setPlainPassword(?string $plainPassword): User
  122.     {
  123.         $this->plainPassword $plainPassword;
  124.         $this->password null;
  125.         return $this;
  126.     }
  127.     public function getUserAgent(): ?string
  128.     {
  129.         return $this->userAgent;
  130.     }
  131.     public function setUserAgent(?string $userAgent): self
  132.     {
  133.         $this->userAgent $userAgent;
  134.         return $this;
  135.     }
  136.     public function getIp(): ?string
  137.     {
  138.         return $this->ip;
  139.     }
  140.     public function setIp(?string $ip): self
  141.     {
  142.         $this->ip $ip;
  143.         return $this;
  144.     }
  145.     public function getLastLoginAt(): ?\DateTimeImmutable
  146.     {
  147.         return $this->lastLoginAt;
  148.     }
  149.     public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): self
  150.     {
  151.         $this->lastLoginAt $lastLoginAt;
  152.         return $this;
  153.     }
  154.     public function getResetToken(): ?string
  155.     {
  156.         return $this->resetToken;
  157.     }
  158.     public function setResetToken(?string $resetToken): self
  159.     {
  160.         $this->resetToken $resetToken;
  161.         return $this;
  162.     }
  163.     public function getPerson(): ?Person
  164.     {
  165.         return $this->person;
  166.     }
  167.     public function setPerson(?Person $person): self
  168.     {
  169.         // unset the owning side of the relation if necessary
  170.         if ($person === null && $this->person !== null) {
  171.             $this->person->setUser(null);
  172.         }
  173.         // set the owning side of the relation if necessary
  174.         if ($person !== null && $person->getUser() !== $this) {
  175.             $person->setUser($this);
  176.         }
  177.         $this->person $person;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return Collection<int, AdminSection>
  182.      */
  183.     public function getAdminSections(): Collection
  184.     {
  185.         return $this->adminSections;
  186.     }
  187.     public function addAdminSection(AdminSection $adminSection): self
  188.     {
  189.         if (!$this->adminSections->contains($adminSection)) {
  190.             $this->adminSections[] = $adminSection;
  191.             $adminSection->setUser($this);
  192.         }
  193.         return $this;
  194.     }
  195.     public function removeAdminSection(AdminSection $adminSection): self
  196.     {
  197.         if ($this->adminSections->removeElement($adminSection)) {
  198.             // set the owning side to null (unless already changed)
  199.             if ($adminSection->getUser() === $this) {
  200.                 $adminSection->setUser(null);
  201.             }
  202.         }
  203.         return $this;
  204.     }
  205.     public function __toString(): string
  206.     {
  207.         return $this->getPerson()?->getFullName() ?? $this->getEmail();
  208.     }
  209.     public function isIsTemporary(): ?bool
  210.     {
  211.         return $this->isTemporary;
  212.     }
  213.     public function setIsTemporary(bool $isTemporary): self
  214.     {
  215.         $this->isTemporary $isTemporary;
  216.         return $this;
  217.     }
  218.     public function serialize()
  219.     {
  220.         return serialize(array(
  221.             $this->id,
  222.             $this->email,
  223.             $this->password,
  224.         ));
  225.     }
  226.     public function unserialize(string $data)
  227.     {
  228.         list(
  229.             $this->id,
  230.             $this->email,
  231.             $this->password,
  232.             ) = unserialize($data, ['allowed_classes' => false]);
  233.     }
  234.     /**
  235.      * @return Collection<int, AccountAccess>
  236.      */
  237.     public function getWebmailAccountAccesses(): Collection
  238.     {
  239.         return $this->webmailAccountAccesses;
  240.     }
  241.     public function addWebmailAccountAccess(AccountAccess $webmailAccountAccess): self
  242.     {
  243.         if (!$this->webmailAccountAccesses->contains($webmailAccountAccess)) {
  244.             $this->webmailAccountAccesses->add($webmailAccountAccess);
  245.             $webmailAccountAccess->setUser($this);
  246.         }
  247.         return $this;
  248.     }
  249.     public function removeWebmailAccountAccess(AccountAccess $webmailAccountAccess): self
  250.     {
  251.         if ($this->webmailAccountAccesses->removeElement($webmailAccountAccess)) {
  252.             // set the owning side to null (unless already changed)
  253.             if ($webmailAccountAccess->getUser() === $this) {
  254.                 $webmailAccountAccess->setUser(null);
  255.             }
  256.         }
  257.         return $this;
  258.     }
  259. }