src/Entity/User/User.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity\User;
  3. use App\Entity\Fiche\FicheBatiment;
  4. use App\Entity\Visa\VisaExigences;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. #[ORM\Entity(repositoryClass\App\Repository\User\UserRepository::class)]
  13. #[UniqueEntity('username')]
  14. #[UniqueEntity('email')]
  15. #[ORM\Table(name'user')]
  16. class User implements UserInterface
  17. {
  18.     const ARRAY_ROLES = [
  19.         'ROLE_SUPER_ADMIN',
  20.         'ROLE_ADMIN',
  21.         'ROLE_UTILISATEUR'
  22.     ];
  23.     #[ORM\Id]
  24.     #[ORM\GeneratedValue]
  25.     #[ORM\Column(type'integer')]
  26.     private $id;
  27.     #[ORM\Column(type'string'length50uniquetrue)]
  28.     #[Assert\Length(min2max50)]
  29.     private $username;
  30.     #[ORM\Column(type'string'length150uniquetrue)]
  31.     #[Assert\Email]
  32.     #[Assert\Length(min2max150)]
  33.     private $email;
  34.     private $plainPassword;
  35.     #[ORM\Column(type'string')]
  36.     private $password;
  37.     #[ORM\Column(type'array')]
  38.     private $roles;
  39.     /**
  40.      * @var boolean
  41.      */
  42.     #[ORM\Column(type'boolean')]
  43.     protected $isEnabled false;
  44.     /**
  45.      * @var string
  46.      */
  47.     #[ORM\Column(type'string'length255nullabletrue)]
  48.     protected $nom;
  49.     /**
  50.      * @var string
  51.      */
  52.     #[ORM\Column(type'string'length255nullabletrue)]
  53.     protected $prenom;
  54.     /**
  55.      * @var string
  56.      */
  57.     #[ORM\Column(type'string'length32nullabletrue)]
  58.     protected $activationToken null;
  59.     /**
  60.      * @var bool
  61.      */
  62.     #[ORM\Column(type'boolean')]
  63.     protected $isComparaisonAll true;
  64.     /**
  65.      * @var \DateTime
  66.      */
  67.     #[ORM\Column(type'datetime')]
  68.     protected $theDateTimeCreation;
  69.     /**
  70.      * @var ArrayCollection
  71.      */
  72.     #[ORM\OneToMany(targetEntity\App\Entity\Fiche\FicheBatiment::class, mappedBy'theUser')]
  73.     protected $listFicheBatiment;
  74.     #[ORM\ManyToOne(targetEntityVisaExigences::class, inversedBy'User')]
  75.     private $visaExigences;
  76.     public static function labelizeRole($role)
  77.     {
  78.         return trim(ucwords(strtolower(str_replace(['ROLE''_'], [' '' '], $role))));
  79.     }
  80.     public function __construct()
  81.     {
  82.         $this->setTheDateTimeCreation(new \DateTime());
  83.         $this->listFicheBatiment = new ArrayCollection();
  84.     }
  85.     public function __toString()
  86.     {
  87.         return $this->getPatronyme();
  88.     }
  89.     public function getId()
  90.     {
  91.         return $this->id;
  92.     }
  93.     public function getUsername()
  94.     {
  95.         return $this->username;
  96.     }
  97.     public function setUsername(string $username): self
  98.     {
  99.         $this->username $username;
  100.         return $this;
  101.     }
  102.     public function getEmail()
  103.     {
  104.         return $this->email;
  105.     }
  106.     public function setEmail(string $email): self
  107.     {
  108.         $this->email $email;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @return mixed
  113.      */
  114.     public function getPlainPassword()
  115.     {
  116.         return $this->plainPassword;
  117.     }
  118.     /**
  119.      * @param mixed $plainPassword
  120.      */
  121.     public function setPlainPassword($plainPassword): self
  122.     {
  123.         $this->plainPassword $plainPassword;
  124.         return $this;
  125.     }
  126.     public function getPassword()
  127.     {
  128.         return $this->password;
  129.     }
  130.     public function setPassword(string $password): self
  131.     {
  132.         $this->password $password;
  133.         return $this;
  134.     }
  135.     public function getRoles()
  136.     {
  137.         $roles $this->roles;
  138.         // il est obligatoire d'avoir au moins un rôle si on est authentifié, par convention c'est ROLE_UTILISATEUR
  139.         if (empty($roles)) {
  140.             $roles[] = 'ROLE_UTILISATEUR';
  141.         }
  142.         return array_unique($roles);
  143.     }
  144.     public function addRole(string $role): self
  145.     {
  146.         if (!array_search($role$this->getRoles())) {
  147.             $this->roles[] = $role;
  148.         }
  149.         return $this;
  150.     }
  151.     public function removeRole(string $role): self
  152.     {
  153.         $index array_search($role$this->getRoles());
  154.         if ($index !== false) {
  155.             unset($this->roles[$index]);
  156.         }
  157.         return $this;
  158.     }
  159.     public function hasRole(string $role): bool
  160.     {
  161.         $isRole false;
  162.         foreach($this->getRoles() as $strRole) {
  163.             if ($strRole == $role) {
  164.                 $isRole true;
  165.                 break;
  166.             }
  167.         }
  168.         return $isRole;
  169.     }
  170.     /**
  171.      * @return bool
  172.      */
  173.     public function getIsEnabled()
  174.     {
  175.         return $this->isEnabled;
  176.     }
  177.     /**
  178.      * @param bool $isEnabled
  179.      */
  180.     public function setIsEnabled(bool $isEnabled): self
  181.     {
  182.         $this->isEnabled $isEnabled;
  183.         return $this;
  184.     }
  185.     public function getSalt(): ?string
  186.     {
  187.         return null;
  188.     }
  189.     public function eraseCredentials(): void
  190.     {
  191.     }
  192.     /**
  193.      * @return string
  194.      */
  195.     public function getNom()
  196.     {
  197.         return $this->nom;
  198.     }
  199.     /**
  200.      * @param string $nom
  201.      */
  202.     public function setNom(string $nom): self
  203.     {
  204.         $this->nom $nom;
  205.         return $this;
  206.     }
  207.     /**
  208.      * @return string
  209.      */
  210.     public function getPrenom()
  211.     {
  212.         return $this->prenom;
  213.     }
  214.     /**
  215.      * @param string $prenom
  216.      */
  217.     public function setPrenom(string $prenom): self
  218.     {
  219.         $this->prenom $prenom;
  220.         return $this;
  221.     }
  222.     /**
  223.      * @return string
  224.      */
  225.     public function getActivationToken(): ?string
  226.     {
  227.         return $this->activationToken;
  228.     }
  229.     /**
  230.      * @param string $activationToken
  231.      */
  232.     public function setActivationToken(?string $activationToken): void
  233.     {
  234.         $this->activationToken $activationToken;
  235.     }
  236.     /**
  237.      * @return bool
  238.      */
  239.     public function isComparaisonAll(): bool
  240.     {
  241.         return $this->isComparaisonAll;
  242.     }
  243.     /**
  244.      * @param bool $isComparaisonAll
  245.      */
  246.     public function setIsComparaisonAll(bool $isComparaisonAll): void
  247.     {
  248.         $this->isComparaisonAll $isComparaisonAll;
  249.     }
  250.     /**
  251.      * @return bool
  252.      */
  253.     public function getLibelleComparaisonAll(): string
  254.     {
  255.         if ($this->isComparaisonAll()) {
  256.             $libelle 'Toute la base';
  257.         } else {
  258.             $libelle 'Uniquement mes fiches';
  259.         }
  260.         return $libelle;
  261.     }
  262.     /**
  263.      * @return \DateTime
  264.      */
  265.     public function getTheDateTimeCreation(): \DateTime
  266.     {
  267.         return $this->theDateTimeCreation;
  268.     }
  269.     /**
  270.      * @param \DateTime $theDateCreation
  271.      *
  272.      * @return User
  273.      */
  274.     public function setTheDateTimeCreation(\DateTime $theDateTimeCreation): self
  275.     {
  276.         $this->theDateTimeCreation $theDateTimeCreation;
  277.         return $this;
  278.     }
  279.     public function getPatronyme()
  280.     {
  281.         return ucfirst($this->getPrenom()) . ' ' strtoupper($this->getNom());
  282.     }
  283.     public function getPatronymeAndRole()
  284.     {
  285.         return $this->getPatronyme() . ' (' $this->getRolePrincipal() . ')';
  286.     }
  287.     public function setRolePrincipal($role)
  288.     {
  289.         $this->roles = array();
  290.         $this->addRole($role);
  291.         return $this;
  292.     }
  293.     public function getRolePrincipal()
  294.     {
  295.         if (count($this->getRoles()) > 0) {
  296.             return $this->getRoles()[0];
  297.         } else {
  298.             return null;
  299.         }
  300.     }
  301.     public function getLibelleRolePrincipal()
  302.     {
  303.         return self::labelizeRole($this->getRolePrincipal());
  304.     }
  305.     /**
  306.      * @return ArrayCollection
  307.      */
  308.     public function getListFicheBatiment()
  309.     {
  310.         return $this->listFicheBatiment;
  311.     }
  312.     public function getVisaExigences(): ?VisaExigences
  313.     {
  314.         return $this->visaExigences;
  315.     }
  316.     public function setVisaExigences(?VisaExigences $visaExigences): self
  317.     {
  318.         $this->visaExigences $visaExigences;
  319.         return $this;
  320.     }
  321.     public function setRoles(array $roles): self
  322.     {
  323.         $this->roles $roles;
  324.         return $this;
  325.     }
  326.     public function isIsEnabled(): ?bool
  327.     {
  328.         return $this->isEnabled;
  329.     }
  330.     public function isIsComparaisonAll(): ?bool
  331.     {
  332.         return $this->isComparaisonAll;
  333.     }
  334.     public function addListFicheBatiment(FicheBatiment $listFicheBatiment): self
  335.     {
  336.         if (!$this->listFicheBatiment->contains($listFicheBatiment)) {
  337.             $this->listFicheBatiment->add($listFicheBatiment);
  338.             $listFicheBatiment->setTheUser($this);
  339.         }
  340.         return $this;
  341.     }
  342.     public function removeListFicheBatiment(FicheBatiment $listFicheBatiment): self
  343.     {
  344.         if ($this->listFicheBatiment->removeElement($listFicheBatiment)) {
  345.             // set the owning side to null (unless already changed)
  346.             if ($listFicheBatiment->getTheUser() === $this) {
  347.                 $listFicheBatiment->setTheUser(null);
  348.             }
  349.         }
  350.         return $this;
  351.     }
  352. }