src/Entity/User/User.php line 21

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