src/Entity/User.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use DateTimeImmutable;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Repository\UserRepository;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. #[ORM\Entity(repositoryClassUserRepository::class)]
  12. #[ORM\Table(name'`user`')]
  13. class User implements UserInterfacePasswordAuthenticatedUserInterface
  14. {
  15.     #[ORM\Id]
  16.     #[ORM\GeneratedValue]
  17.     #[ORM\Column]
  18.     private ?int $id null;
  19.     #[ORM\Column(length180uniquetrue)]
  20.     private ?string $email null;
  21.     #[ORM\Column]
  22.     private array $roles = [];
  23.     /**
  24.      * @var string The hashed password
  25.      */
  26.     #[ORM\Column]
  27.     private ?string $password null;
  28.     private ?string $plainPassword null;
  29.     private ?string $name null;
  30.     #[ORM\Column]
  31.     private ?bool $firstLogin null;
  32.     #[ORM\Column]
  33.     private ?bool $active null;
  34.     #[ORM\Column(typeTypes::DATETIME_IMMUTABLEnullabletrue)]
  35.     private ?\DateTimeImmutable $expirationPassword null;
  36.     #[ORM\OneToMany(mappedBy'user'targetEntityPasswordHistory::class, orphanRemovaltrue)]
  37.     private Collection $passwordHistories;
  38.     #[ORM\OneToOne(inversedBy'user'cascade: ['persist''remove'])]
  39.     private ?CompanyMember $companyMember null;
  40.     #[ORM\OneToOne(inversedBy'user'cascade: ['persist''remove'])]
  41.     private ?Accompanist $accompanist null;
  42.     #[ORM\OneToMany(mappedBy'author'targetEntityEvaluation::class)]
  43.     private Collection $evaluations;
  44.     #[ORM\Column(nullabletrue)]
  45.     private ?\DateTimeImmutable $lastVisit null;
  46.     #[ORM\Column(nullabletrue)]
  47.     private ?\DateTimeImmutable $lastLogin null;
  48.     #[ORM\OneToMany(mappedBy'stakeholder'targetEntityObjective::class)]
  49.     private Collection $objectives;
  50.     public function __construct()
  51.     {
  52.         $this->firstLogin true;
  53.         $this->active false;
  54.         $this->passwordHistories = new ArrayCollection();
  55.         $this->evaluations = new ArrayCollection();
  56.         $this->objectives = new ArrayCollection();
  57.     }
  58.     public function __toString()
  59.     {
  60.         if ($this->companyMember) {
  61.             $name $this->companyMember->getFirstname() . ' ' $this->companyMember->getLastname();
  62.         } else {
  63.             $name $this->accompanist->getFirstname() . ' ' substr($this->accompanist->getLastname(), 01) . '.';
  64.         }
  65.         return $name;
  66.     }
  67.     public function getName()
  68.     {
  69.         if ($this->companyMember) {
  70.             $name $this->companyMember->getFirstname() . ' ' $this->companyMember->getLastname();
  71.         } else {
  72.             $name $this->accompanist->getFirstname() . ' ' substr($this->accompanist->getLastname(), 01) . '.';
  73.         }
  74.         return $name;
  75.     }
  76.     public function getId(): ?int
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function getEmail(): ?string
  81.     {
  82.         return $this->email;
  83.     }
  84.     public function setEmail(string $email): self
  85.     {
  86.         $this->email $email;
  87.         return $this;
  88.     }
  89.     /**
  90.      * A visual identifier that represents this user.
  91.      *
  92.      * @see UserInterface
  93.      */
  94.     public function getUserIdentifier(): string
  95.     {
  96.         return (string) $this->email;
  97.     }
  98.     /**
  99.      * @see UserInterface
  100.      */
  101.     public function getRoles(): array
  102.     {
  103.         $roles $this->roles;
  104.         // guarantee every user at least has ROLE_USER
  105.         $roles[] = 'ROLE_USER';
  106.         return array_unique($roles);
  107.     }
  108.     public function setRoles(array $roles): self
  109.     {
  110.         $this->roles $roles;
  111.         return $this;
  112.     }
  113.     /**
  114.      * @see PasswordAuthenticatedUserInterface
  115.      */
  116.     public function getPassword(): string
  117.     {
  118.         return $this->password;
  119.     }
  120.     public function setPassword(string $password): self
  121.     {
  122.         $this->password $password;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @see UserInterface
  127.      */
  128.     public function eraseCredentials()
  129.     {
  130.         // If you store any temporary, sensitive data on the user, clear it here
  131.         // $this->plainPassword = null;
  132.     }
  133.     /**
  134.      * Get the value of plainPassword
  135.      */
  136.     public function getPlainPassword()
  137.     {
  138.         return $this->plainPassword;
  139.     }
  140.     /**
  141.      * Set the value of plainPassword
  142.      *
  143.      * @return  self
  144.      */
  145.     public function setPlainPassword($plainPassword)
  146.     {
  147.         $this->plainPassword $plainPassword;
  148.         return $this;
  149.     }
  150.     public function isFirstLogin(): ?bool
  151.     {
  152.         return $this->firstLogin;
  153.     }
  154.     public function setFirstLogin(bool $firstLogin): self
  155.     {
  156.         $this->firstLogin $firstLogin;
  157.         return $this;
  158.     }
  159.     public function isActive(): ?bool
  160.     {
  161.         return $this->active;
  162.     }
  163.     public function setActive(bool $active): self
  164.     {
  165.         $this->active $active;
  166.         return $this;
  167.     }
  168.     public function getExpirationPassword(): ?\DateTimeImmutable
  169.     {
  170.         return $this->expirationPassword;
  171.     }
  172.     public function setExpirationPassword(\DateTimeImmutable $expirationPassword): self
  173.     {
  174.         $this->expirationPassword $expirationPassword;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @return Collection<int, PasswordHistory>
  179.      */
  180.     public function getPasswordHistories(): Collection
  181.     {
  182.         return $this->passwordHistories;
  183.     }
  184.     public function addPasswordHistory(PasswordHistory $passwordHistory): self
  185.     {
  186.         if (!$this->passwordHistories->contains($passwordHistory)) {
  187.             $this->passwordHistories->add($passwordHistory);
  188.             $passwordHistory->setUser($this);
  189.         }
  190.         return $this;
  191.     }
  192.     public function removePasswordHistory(PasswordHistory $passwordHistory): self
  193.     {
  194.         if ($this->passwordHistories->removeElement($passwordHistory)) {
  195.             // set the owning side to null (unless already changed)
  196.             if ($passwordHistory->getUser() === $this) {
  197.                 $passwordHistory->setUser(null);
  198.             }
  199.         }
  200.         return $this;
  201.     }
  202.     public function getCompanyMember(): ?CompanyMember
  203.     {
  204.         return $this->companyMember;
  205.     }
  206.     public function setCompanyMember(?CompanyMember $companyMember): self
  207.     {
  208.         $this->companyMember $companyMember;
  209.         return $this;
  210.     }
  211.     public function getAccompanist(): ?Accompanist
  212.     {
  213.         return $this->accompanist;
  214.     }
  215.     public function setAccompanist(?Accompanist $accompanist): self
  216.     {
  217.         $this->accompanist $accompanist;
  218.         return $this;
  219.     }
  220.     /**
  221.      * @return Collection<int, Evaluation>
  222.      */
  223.     public function getEvaluations(): Collection
  224.     {
  225.         return $this->evaluations;
  226.     }
  227.     public function addEvaluation(Evaluation $evaluation): self
  228.     {
  229.         if (!$this->evaluations->contains($evaluation)) {
  230.             $this->evaluations->add($evaluation);
  231.             $evaluation->setAuthor($this);
  232.         }
  233.         return $this;
  234.     }
  235.     public function removeEvaluation(Evaluation $evaluation): self
  236.     {
  237.         if ($this->evaluations->removeElement($evaluation)) {
  238.             // set the owning side to null (unless already changed)
  239.             if ($evaluation->getAuthor() === $this) {
  240.                 $evaluation->setAuthor(null);
  241.             }
  242.         }
  243.         return $this;
  244.     }
  245.     public function isAllowed($granted$company null): ?bool
  246.     {
  247.         if (in_array('ROLE_ADMIN'$this->roles)) {
  248.             return true;
  249.         }
  250.         if ($granted == "company") {
  251.             if (in_array('ROLE_BILBEA'$this->roles)) {
  252.                 return true;
  253.             } elseif ($this->companyMember->getCompany() == $company) {
  254.                 return true;
  255.             }
  256.         } elseif ($granted == "accompanist") {
  257.             if (in_array('ROLE_BILBEA'$this->roles)) {
  258.                 return true;
  259.             }
  260.         } elseif ($granted == "admin") {
  261.             if (in_array('ROLE_ADMIN'$this->roles)) {
  262.                 return true;
  263.             }
  264.         }
  265.         return false;
  266.     }
  267.     public function isAccountActive(): ?bool
  268.     {
  269.         if ($this->companyMember) {
  270.             return $this->companyMember->isActive();
  271.         }
  272.         if ($this->accompanist) {
  273.             return $this->accompanist->isActive();
  274.         }
  275.     }
  276.     public function getLastVisit(): ?\DateTimeImmutable
  277.     {
  278.         return $this->lastVisit;
  279.     }
  280.     public function setLastVisit(?\DateTimeImmutable $lastVisit): self
  281.     {
  282.         $this->lastVisit $lastVisit;
  283.         return $this;
  284.     }
  285.     public function getLastLogin(): ?\DateTimeImmutable
  286.     {
  287.         return $this->lastLogin;
  288.     }
  289.     public function setLastLogin(?\DateTimeImmutable $lastLogin): self
  290.     {
  291.         $this->lastLogin $lastLogin;
  292.         return $this;
  293.     }
  294.     public function hasMemberShip(): ?bool
  295.     {
  296.         if (in_array('ROLE_ADMIN'$this->roles) || in_array('ROLE_BILBEA'$this->roles)) {
  297.             return true;
  298.         }
  299.         $now = new DateTimeImmutable();
  300.         $memberships array_reverse($this->companyMember->getCompany()->getMemberships()->toArray());
  301.         if (count($memberships) > 0) {
  302.             $this->getCompanyMember()->getCompany()->setLastMembership($memberships[0]);
  303.         }
  304.         foreach (array_reverse($this->companyMember->getCompany()->getMemberships()->toArray()) as $key => $membership) {
  305.             if ($membership->isActive() && $membership->getStartedAt() < $now && $membership->getEndAt() > $now) {
  306.                 return true;
  307.             }
  308.         }
  309.         return false;
  310.     }
  311.     /**
  312.      * @return Collection<int, Objective>
  313.      */
  314.     public function getObjectives(): Collection
  315.     {
  316.         return $this->objectives;
  317.     }
  318.     public function addObjective(Objective $objective): self
  319.     {
  320.         if (!$this->objectives->contains($objective)) {
  321.             $this->objectives->add($objective);
  322.             $objective->setStakeholder($this);
  323.         }
  324.         return $this;
  325.     }
  326.     public function removeObjective(Objective $objective): self
  327.     {
  328.         if ($this->objectives->removeElement($objective)) {
  329.             // set the owning side to null (unless already changed)
  330.             if ($objective->getStakeholder() === $this) {
  331.                 $objective->setStakeholder(null);
  332.             }
  333.         }
  334.         return $this;
  335.     }
  336. }