src/Entity/User.php line 28

  1. <?php
  2. /*
  3.  *  @author Guerby Duval <info@tranzaksyon.com>
  4.  *  @link https://tranzaksyon.com
  5.  *  @copyright You are not allowed to remove this author "Guerby Duval <info@tranzaksyon.com>", the link "https://tranzaksyon.com" neither this copyright.
  6.  */
  7. namespace App\Entity;
  8. use App\Interfaces\OwnerCompanyInterface;
  9. use App\Repository\UserRepository;
  10. use App\RoleUtils;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints\Count;
  19. use Symfony\Component\Validator\Constraints\Email;
  20. use Symfony\Component\Validator\Constraints\Length;
  21. use Symfony\Component\Validator\Constraints\NotBlank;
  22. #[ORM\Entity(repositoryClassUserRepository::class)]
  23. #[UniqueEntity('email')]
  24. #[UniqueEntity('username')]
  25. class User implements UserInterfacePasswordAuthenticatedUserInterfaceOwnerCompanyInterface
  26. {
  27.     #[ORM\Id]
  28.     #[ORM\GeneratedValue]
  29.     #[ORM\Column]
  30.     #[Groups(['user_out'])]
  31.     private ?int $id null;
  32.     #[ORM\Column(length180uniquetrue)]
  33.     #[NotBlank]
  34.     #[Groups(['user_out'])]
  35.     private ?string $username null;
  36.     #[ORM\Column]
  37.     #[NotBlank(groups: ['employee-create'])]
  38.     #[Count(min1groups: ['employee-create'])]
  39.     #[Groups(['user_out'])]
  40.     private array $roles = [];
  41.     /**
  42.      * @var string The hashed password
  43.      */
  44.     #[ORM\Column]
  45.     #[NotBlank(groups: ["new-user"])]
  46.     #[Length(min8groups: ["new-user"])]
  47.     private ?string $password null;
  48.     #[ORM\Column(length80uniquetrue)]
  49.     #[Email]
  50.     #[NotBlank]
  51.     #[Groups(['user_out'])]
  52.     private ?string $email null;
  53.     #[ORM\Column(length100)]
  54.     #[NotBlank]
  55.     #[Groups(['user_out'])]
  56.     private ?string $name null;
  57.     #[ORM\Column(length100)]
  58.     #[NotBlank]
  59.     #[Groups(['user_out'])]
  60.     private ?string $lastname null;
  61.     #[ORM\OneToMany(mappedBy'user'targetEntitySriInfo::class, orphanRemovaltrue)]
  62.     private Collection $sriInfos;
  63.     #[ORM\OneToMany(mappedBy'user'targetEntityInvoice::class)]
  64.     private Collection $invoices;
  65.     #[ORM\ManyToOne(targetEntityself::class, inversedBy'disabledUsers')]
  66.     #[ORM\JoinColumn(onDelete'SET NULL')]
  67.     private ?self $disabledBy null;
  68.     #[ORM\OneToMany(mappedBy'disabledBy'targetEntityself::class)]
  69.     private Collection $disabledUsers;
  70.     #[ORM\OneToMany(mappedBy'disabledBy'targetEntitySriInfo::class)]
  71.     private Collection $disableCompanies;
  72.     #[ORM\Column(length2000nullabletrue)]
  73.     private ?string $resetPasswordKey null;
  74.     #[ORM\OneToMany(mappedBy'owner'targetEntityCompany::class, orphanRemovaltrue)]
  75.     private Collection $companies;
  76.     public function __construct()
  77.     {
  78.         $this->sriInfos = new ArrayCollection();
  79.         $this->invoices = new ArrayCollection();
  80.         $this->disabledUsers = new ArrayCollection();
  81.         $this->disableCompanies = new ArrayCollection();
  82.         $this->companies = new ArrayCollection();
  83.     }
  84.     public function getId(): ?int
  85.     {
  86.         return $this->id;
  87.     }
  88.     public function getUsername(): ?string
  89.     {
  90.         return $this->username;
  91.     }
  92.     public function setUsername(string $username): self
  93.     {
  94.         $this->username $username;
  95.         return $this;
  96.     }
  97.     /**
  98.      * A visual identifier that represents this user.
  99.      *
  100.      * @see UserInterface
  101.      */
  102.     public function getUserIdentifier(): string
  103.     {
  104.         return (string)$this->username;
  105.     }
  106.     /**
  107.      * @see UserInterface
  108.      */
  109.     public function getRoles(): array
  110.     {
  111.         $roles $this->roles;
  112.         // guarantee every user at least has ROLE_USER
  113.         $roles[] = 'ROLE_USER';
  114.         return array_unique($roles);
  115.     }
  116.     public function setRoles(array $roles): self
  117.     {
  118.         $this->roles $roles;
  119.         return $this;
  120.     }
  121.     /**
  122.      * @see PasswordAuthenticatedUserInterface
  123.      */
  124.     public function getPassword(): string
  125.     {
  126.         return $this->password;
  127.     }
  128.     public function setPassword(string $password): self
  129.     {
  130.         $this->password $password;
  131.         return $this;
  132.     }
  133.     /**
  134.      * @see UserInterface
  135.      */
  136.     public function eraseCredentials()
  137.     {
  138.         // If you store any temporary, sensitive data on the user, clear it here
  139.         // $this->plainPassword = null;
  140.     }
  141.     public function getEmail(): ?string
  142.     {
  143.         return $this->email;
  144.     }
  145.     public function setEmail(string $email): self
  146.     {
  147.         $this->email $email;
  148.         return $this;
  149.     }
  150.     public function getName(): ?string
  151.     {
  152.         return $this->name;
  153.     }
  154.     public function setName(string $name): self
  155.     {
  156.         $this->name $name;
  157.         return $this;
  158.     }
  159.     public function getLastname(): ?string
  160.     {
  161.         return $this->lastname;
  162.     }
  163.     public function setLastname(string $lastname): self
  164.     {
  165.         $this->lastname $lastname;
  166.         return $this;
  167.     }
  168.     public function canonicalRoles()
  169.     {
  170.         return RoleUtils::canonicalizeRoles($this->getRoles());
  171.     }
  172.     /**
  173.      * @return Collection<int, SriInfo>
  174.      */
  175.     public function getSriInfos(): Collection
  176.     {
  177.         return $this->sriInfos;
  178.     }
  179.     public function addSriInfo(SriInfo $sriInfo): self
  180.     {
  181.         if (!$this->sriInfos->contains($sriInfo)) {
  182.             $this->sriInfos->add($sriInfo);
  183.             $sriInfo->setUser($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeSriInfo(SriInfo $sriInfo): self
  188.     {
  189.         if ($this->sriInfos->removeElement($sriInfo)) {
  190.             // set the owning side to null (unless already changed)
  191.             if ($sriInfo->getUser() === $this) {
  192.                 $sriInfo->setUser(null);
  193.             }
  194.         }
  195.         return $this;
  196.     }
  197.     /**
  198.      * @return Collection<int, Invoice>
  199.      */
  200.     public function getInvoices(): Collection
  201.     {
  202.         return $this->invoices;
  203.     }
  204.     public function addInvoice(Invoice $invoice): self
  205.     {
  206.         if (!$this->invoices->contains($invoice)) {
  207.             $this->invoices->add($invoice);
  208.             $invoice->setUser($this);
  209.         }
  210.         return $this;
  211.     }
  212.     public function removeInvoice(Invoice $invoice): self
  213.     {
  214.         if ($this->invoices->removeElement($invoice)) {
  215.             // set the owning side to null (unless already changed)
  216.             if ($invoice->getUser() === $this) {
  217.                 $invoice->setUser(null);
  218.             }
  219.         }
  220.         return $this;
  221.     }
  222.     public function getDisabledBy(): ?self
  223.     {
  224.         return $this->disabledBy;
  225.     }
  226.     public function setDisabledBy(?self $disabledBy): self
  227.     {
  228.         $this->disabledBy $disabledBy;
  229.         return $this;
  230.     }
  231.     /**
  232.      * @return Collection<int, self>
  233.      */
  234.     public function getDisabledUsers(): Collection
  235.     {
  236.         return $this->disabledUsers;
  237.     }
  238.     public function addDisabledUser(self $disabledUser): self
  239.     {
  240.         if (!$this->disabledUsers->contains($disabledUser)) {
  241.             $this->disabledUsers->add($disabledUser);
  242.             $disabledUser->setDisabledBy($this);
  243.         }
  244.         return $this;
  245.     }
  246.     public function removeDisabledUser(self $disabledUser): self
  247.     {
  248.         if ($this->disabledUsers->removeElement($disabledUser)) {
  249.             // set the owning side to null (unless already changed)
  250.             if ($disabledUser->getDisabledBy() === $this) {
  251.                 $disabledUser->setDisabledBy(null);
  252.             }
  253.         }
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return Collection<int, SriInfo>
  258.      */
  259.     public function getDisableCompanies(): Collection
  260.     {
  261.         return $this->disableCompanies;
  262.     }
  263.     public function addDisableCompany(SriInfo $disableCompany): self
  264.     {
  265.         if (!$this->disableCompanies->contains($disableCompany)) {
  266.             $this->disableCompanies->add($disableCompany);
  267.             $disableCompany->setDisabledBy($this);
  268.         }
  269.         return $this;
  270.     }
  271.     public function removeDisableCompany(SriInfo $disableCompany): self
  272.     {
  273.         if ($this->disableCompanies->removeElement($disableCompany)) {
  274.             // set the owning side to null (unless already changed)
  275.             if ($disableCompany->getDisabledBy() === $this) {
  276.                 $disableCompany->setDisabledBy(null);
  277.             }
  278.         }
  279.         return $this;
  280.     }
  281.     public function getResetPasswordKey(): ?string
  282.     {
  283.         return $this->resetPasswordKey;
  284.     }
  285.     public function setResetPasswordKey(?string $resetPasswordKey): self
  286.     {
  287.         $this->resetPasswordKey $resetPasswordKey;
  288.         return $this;
  289.     }
  290.     public function getSriInfo(): ?SriInfo
  291.     {
  292.         if ($this->sriInfos->count() === 0) {
  293.             return null;
  294.         }
  295.         return $this->sriInfos[0];
  296.     }
  297.     /**
  298.      * @return Collection<int, Company>
  299.      */
  300.     public function getCompanies(): Collection
  301.     {
  302.         return $this->companies;
  303.     }
  304.     public function addCompany(Company $company): static
  305.     {
  306.         if (!$this->companies->contains($company)) {
  307.             $this->companies->add($company);
  308.             $company->setOwner($this);
  309.         }
  310.         return $this;
  311.     }
  312.     public function removeCompany(Company $company): static
  313.     {
  314.         if ($this->companies->removeElement($company)) {
  315.             // set the owning side to null (unless already changed)
  316.             if ($company->getOwner() === $this) {
  317.                 $company->setOwner(null);
  318.             }
  319.         }
  320.         return $this;
  321.     }
  322. }