src/Entity/User.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  14. /**
  15.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  16.  */
  17. #[ORM\Entity(repositoryClassUserRepository::class)]
  18. #[ORM\Table(name'`user`')]
  19. #[ORM\HasLifecycleCallbacks()]
  20. #[Vich\Uploadable]
  21. class User implements UserInterfacePasswordAuthenticatedUserInterface\Serializable
  22. {
  23.     public const ROLE_SUPER_ADMIN   'ROLE_SUPER_ADMIN';
  24.     public const ROLE_ADMIN         'ROLE_ADMIN';
  25.     public const ROLE_RESPONSABLE   'ROLE_RESPONSABLE';
  26.     public const ROLE_EMPLOYEE      'ROLE_EMPLOYEE';
  27.     const strEtat = [
  28.                     => 'Inactif',
  29.                     => 'Actif',
  30.                     => 'Supprimé'
  31.     ];
  32.     #[ORM\Id]
  33.     #[ORM\GeneratedValue]
  34.     #[ORM\Column(type'integer')]
  35.     private $id;
  36.     #[ORM\Column(type'string'length180uniquetrue)]
  37.     #[Assert\NotBlank()]
  38.     #[Assert\Email()]
  39.     private $email;
  40.     #[ORM\Column(type'json')]
  41.     private $roles = [];
  42.     #[ORM\Column(type'string')]
  43.     private $password;
  44.     #[Assert\Length(max:4096)]
  45.     private $plainPassword;
  46.     #[ORM\Column(type'string'length255nullabletrue)]
  47.     private $firstName;
  48.     #[ORM\Column(type'string'length255nullabletrue)]
  49.     private $lastName;
  50.     #[ORM\Column(type'string'length255nullabletrue)]
  51.     private $civility;
  52.     #[ORM\Column(type'string'length255nullabletrue)]
  53.     private $userName;
  54.     #[ORM\Column(type'string'length30nullabletrue)]
  55.     private $phoneNumber;
  56.     #[ORM\Column(type'string'length255nullabletrue)]
  57.     private $fonction;
  58.     #[ORM\Column(type'string'length255nullabletrue)]
  59.     private $adresse;
  60.     #[ORM\Column(type'string'length255nullabletrue)]
  61.     private $pays;
  62.     #[ORM\Column(type'boolean'nullabletrue)]
  63.     private $active;
  64.     #[ORM\Column(type'boolean'nullabletrue)]
  65.     private $etat 1;
  66.     #[ORM\Column(type'integer')]
  67.     private $idParent;
  68.     #[ORM\Column(type'datetime'nullabletrue)]
  69.     private $lastActivity;
  70.     #[ORM\Column(type'datetime'nullabletrue)]
  71.     private $lastConnected;
  72.     #[ORM\ManyToMany(targetEntityGroupe::class, mappedBy'listesOfEmployes')]
  73.     private $groupes;
  74.     #[ORM\OneToMany(mappedBy'responsable'targetEntityGroupe::class)]
  75.     private $responsableGroupe;
  76.     /**
  77.      * @var File|null
  78.      * @Vich\UploadableField(mapping="user_profile", fileNameProperty="imageProfile")
  79.      * @Assert\File(
  80.      *     maxSize = "2M",
  81.      *     mimeTypes = {"image/jpeg", "image/png"},
  82.      *     maxSizeMessage = "Le fichier est trop volumineux. Sa taille ne doit pas dépasser 2M",
  83.      *     mimeTypesMessage = "Veuillez télécharger une image valide!"
  84.      * )
  85.      */
  86.     private $imageProfileFile;
  87.     #[ORM\Column(type'string'length255nullabletrue)]
  88.     private $imageProfile;
  89.     #[ORM\Column(type'datetime')]
  90.     private $createdAt;
  91.     #[ORM\Column(type'datetime'nullabletrue)]
  92.     private $updatedAt;
  93.     #[ORM\Column(type'integer'nullabletrue)]
  94.     private $createdBy;
  95.     #[ORM\Column(type'integer'nullabletrue)]
  96.     private $updatedBy;
  97.     #[ORM\Column(type"boolean")]
  98.     private $isVerified false;
  99.     #[ORM\OneToMany(mappedBy'author'targetEntityComment::class)]
  100.     private $comments;
  101.     public function __construct()
  102.     {
  103.         $this->idParent 0;
  104.         $this->groupes = new ArrayCollection();
  105.         $this->responsableGroupe = new ArrayCollection();
  106.         $this->comments = new ArrayCollection();
  107.     }
  108.     public function serialize()
  109.     {
  110.         return serialize(array(
  111.             $this->id,
  112.             $this->email,
  113.             $this->firstName,
  114.             $this->lastName,
  115.             $this->password,
  116.         ));
  117.     }
  118.     public function unserialize($serialized)
  119.     {
  120.         list(
  121.             $this->id,
  122.             $this->email,
  123.             $this->firstName,
  124.             $this->lastName,
  125.             $this->password,
  126.         ) = unserialize($serialized);
  127.     }
  128.     public function getId(): ?int
  129.     {
  130.         return $this->id;
  131.     }
  132.     public function getEmail(): ?string
  133.     {
  134.         return $this->email;
  135.     }
  136.     public function setEmail(string $email): self
  137.     {
  138.         $this->email $email;
  139.         return $this;
  140.     }
  141.     /**
  142.      * A visual identifier that represents this user.
  143.      *
  144.      * @see UserInterface
  145.      */
  146.     public function getUserIdentifier(): string
  147.     {
  148.         return (string) $this->email;
  149.     }
  150.     /**
  151.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  152.      */
  153.     public function getUsername(): string
  154.     {
  155.         return (string) $this->email;
  156.     }
  157.     /**
  158.      * @see UserInterface
  159.      */
  160.     public function getRoles(): array
  161.     {
  162.         $roles $this->roles;
  163.         // guarantee every user at least has ROLE_USER
  164.         $roles[] = 'ROLE_USER';
  165.         return array_unique($roles);
  166.     }
  167.     public function setRoles(array $roles): self
  168.     {
  169.         $this->roles $roles;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @see PasswordAuthenticatedUserInterface
  174.      */
  175.     public function getPassword(): string
  176.     {
  177.         return $this->password;
  178.     }
  179.     public function setPassword(string $password): self
  180.     {
  181.         $this->password $password;
  182.         return $this;
  183.     }
  184.     public function getPlainPassword()
  185.     {
  186.         return $this->plainPassword;
  187.     }
  188.     public function setPlainPassword($password)
  189.     {
  190.         $this->plainPassword $password;
  191.     }
  192.     /**
  193.      * Returning a salt is only needed, if you are not using a modern
  194.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  195.      *
  196.      * @see UserInterface
  197.      */
  198.     public function getSalt(): ?string
  199.     {
  200.         return null;
  201.     }
  202.     /**
  203.      * @see UserInterface
  204.      */
  205.     public function eraseCredentials()
  206.     {
  207.         // If you store any temporary, sensitive data on the user, clear it here
  208.         // $this->plainPassword = null;
  209.     }
  210.     public function getFirstName(): ?string
  211.     {
  212.         return $this->firstName;
  213.     }
  214.     public function setFirstName(string $firstName): self
  215.     {
  216.         $this->firstName $firstName;
  217.         return $this;
  218.     }
  219.     public function getLastName(): ?string
  220.     {
  221.         return $this->lastName;
  222.     }
  223.     public function setLastName(string $lastName): self
  224.     {
  225.         $this->lastName $lastName;
  226.         return $this;
  227.     }
  228.     public function getCivility(): ?string
  229.     {
  230.         return $this->civility;
  231.     }
  232.     public function setCivility(?string $civility): self
  233.     {
  234.         $this->civility $civility;
  235.         return $this;
  236.     }
  237.     public function setUserName(string $userName): self
  238.     {
  239.         $this->userName $userName;
  240.         return $this;
  241.     }
  242.     public function getPhoneNumber(): ?string
  243.     {
  244.         return $this->phoneNumber;
  245.     }
  246.     public function setPhoneNumber(?string $phoneNumber): self
  247.     {
  248.         $this->phoneNumber $phoneNumber;
  249.         return $this;
  250.     }
  251.     public function getFonction(): ?string
  252.     {
  253.         return $this->fonction;
  254.     }
  255.     public function setFonction(?string $fonction): self
  256.     {
  257.         $this->fonction $fonction;
  258.         return $this;
  259.     }
  260.     public function getAdresse(): ?string
  261.     {
  262.         return $this->adresse;
  263.     }
  264.     public function setAdresse(?string $adresse): self
  265.     {
  266.         $this->adresse $adresse;
  267.         return $this;
  268.     }
  269.     public function getPays(): ?string
  270.     {
  271.         return $this->pays;
  272.     }
  273.     public function setPays(?string $pays): self
  274.     {
  275.         $this->pays $pays;
  276.         return $this;
  277.     }
  278.     public function getImageProfile(): ?string
  279.     {
  280.         return $this->imageProfile;
  281.     }
  282.     public function setImageProfile(?string $imageProfile): self
  283.     {
  284.         $this->imageProfile $imageProfile;
  285.         return $this;
  286.     }
  287.     /**
  288.      * @return null|File
  289.      */
  290.     public function getImageProfileFile(): ?File
  291.     {
  292.         return $this->imageProfileFile;
  293.     }
  294.     /**
  295.      * @param null|File $imageProfileFile
  296.      * @return self
  297.      */
  298.     public function setImageProfileFile(?File $imageProfileFile null): self
  299.     {
  300.         $this->imageProfileFile $imageProfileFile;
  301.         return $this;
  302.     }
  303.     public function getActive(): ?bool
  304.     {
  305.         return $this->active;
  306.     }
  307.     public function setActive(?bool $active): self
  308.     {
  309.         $this->active $active;
  310.         return $this;
  311.     }
  312.     public function getEtat(): ?bool
  313.     {
  314.         return $this->etat;
  315.     }
  316.     public function setEtat(?bool $etat): self
  317.     {
  318.         $this->etat $etat;
  319.         return $this;
  320.     }
  321.     public function getIdParent(): ?int
  322.     {
  323.         return $this->idParent;
  324.     }
  325.     public function setIdParent(int $idParent): self
  326.     {
  327.         $this->idParent $idParent;
  328.         return $this;
  329.     }
  330.     public function getLastActivity(): ?\DateTimeInterface
  331.     {
  332.         return $this->lastActivity;
  333.     }
  334.     public function setLastActivity(?\DateTimeInterface $lastActivity): self
  335.     {
  336.         $this->lastActivity $lastActivity;
  337.         return $this;
  338.     }
  339.     public function getLastConnected(): ?\DateTimeInterface
  340.     {
  341.         return $this->lastConnected;
  342.     }
  343.     public function setLastConnected(?\DateTimeInterface $lastConnected): self
  344.     {
  345.         $this->lastConnected $lastConnected;
  346.         return $this;
  347.     }
  348.     /**
  349.      * @return Collection|Groupe[]
  350.      */
  351.     public function getGroupes(): Collection
  352.     {
  353.         return $this->groupes;
  354.     }
  355.     public function addGroupe(Groupe $groupe): self
  356.     {
  357.         if (!$this->groupes->contains($groupe)) {
  358.             $this->groupes[] = $groupe;
  359.             $groupe->addListesOfEmploye($this);
  360.         }
  361.         return $this;
  362.     }
  363.     public function removeGroupe(Groupe $groupe): self
  364.     {
  365.         if ($this->groupes->removeElement($groupe)) {
  366.             $groupe->removeListesOfEmploye($this);
  367.         }
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return Collection|Groupe[]
  372.      */
  373.     public function getResponsableGroupe(): Collection
  374.     {
  375.         return $this->responsableGroupe;
  376.     }
  377.     public function addResponsableGroupe(Groupe $responsableGroupe): self
  378.     {
  379.         if (!$this->responsableGroupe->contains($responsableGroupe)) {
  380.             $this->responsableGroupe[] = $responsableGroupe;
  381.             $responsableGroupe->setResponsable($this);
  382.         }
  383.         return $this;
  384.     }
  385.     public function removeResponsableGroupe(Groupe $responsableGroupe): self
  386.     {
  387.         if ($this->responsableGroupe->removeElement($responsableGroupe)) {
  388.             // set the owning side to null (unless already changed)
  389.             if ($responsableGroupe->getResponsable() === $this) {
  390.                 $responsableGroupe->setResponsable(null);
  391.             }
  392.         }
  393.         return $this;
  394.     }
  395.     public function getCreatedAt(): ?\DateTimeInterface
  396.     {
  397.         return $this->createdAt;
  398.     }
  399.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  400.     {
  401.         $this->createdAt $createdAt;
  402.         return $this;
  403.     }
  404.     public function getUpdatedAt(): ?\DateTimeInterface
  405.     {
  406.         return $this->updatedAt;
  407.     }
  408.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  409.     {
  410.         $this->updatedAt $updatedAt;
  411.         return $this;
  412.     }
  413.     public function getCreatedBy(): ?int
  414.     {
  415.         return $this->createdBy;
  416.     }
  417.     public function setCreatedBy(?int $createdBy): self
  418.     {
  419.         $this->createdBy $createdBy;
  420.         return $this;
  421.     }
  422.     public function getUpdatedBy(): ?int
  423.     {
  424.         return $this->updatedBy;
  425.     }
  426.     public function setUpdatedBy(?int $updatedBy): self
  427.     {
  428.         $this->updatedBy $updatedBy;
  429.         return $this;
  430.     }
  431.     public function getFullName(): string
  432.     {
  433.         return $this->firstName.' '.$this->lastName;
  434.     }
  435.     #[ORM\PrePersist]
  436.     public function onPrePersist()
  437.     {
  438.         if(empty($this->createdAt)){
  439.             $this->createdAt = new \DateTime();
  440.         }
  441.     }
  442.     #[ORM\PreUpdate]
  443.     public function onPreUpdate()
  444.     {
  445.         $this->updatedAt    = new \DateTime();
  446.         $this->lastActivity = new \DateTime();
  447.     }
  448.     /**
  449.      * @return bool whether the user is active or not
  450.      */
  451.     public function isActiveNow()
  452.     {
  453.         $delay = new \DateTime('1 minutes ago');
  454.         return($this->getlastActivity() > $delay);
  455.     }
  456.     public function isVerified(): bool
  457.     {
  458.         return $this->isVerified;
  459.     }
  460.     public function setIsVerified(bool $isVerified): self
  461.     {
  462.         $this->isVerified $isVerified;
  463.         return $this;
  464.     }
  465.     public function getIsVerified(): bool
  466.     {
  467.         return $this->isVerified;
  468.     }
  469.     public function getStrRoles($roles): string
  470.     {
  471.         $strRoles '';
  472.         if(in_array(self::ROLE_SUPER_ADMIN$roles)){
  473.             $strRoles 'Super Admin';
  474.         }elseif(in_array(self::ROLE_RESPONSABLE$roles)){
  475.             $strRoles 'Résponsable';
  476.         }elseif(in_array(self::ROLE_EMPLOYEE$roles)){
  477.             $strRoles 'Employée';
  478.         }
  479.         return $strRoles;
  480.     }
  481.     /**
  482.      * @return Collection|Comment[]
  483.      */
  484.     public function getComments(): Collection
  485.     {
  486.         return $this->comments;
  487.     }
  488.     public function addComment(Comment $comment): self
  489.     {
  490.         if (!$this->comments->contains($comment)) {
  491.             $this->comments[] = $comment;
  492.             $comment->setAuthor($this);
  493.         }
  494.         return $this;
  495.     }
  496.     public function removeComment(Comment $comment): self
  497.     {
  498.         if ($this->comments->removeElement($comment)) {
  499.             // set the owning side to null (unless already changed)
  500.             if ($comment->getAuthor() === $this) {
  501.                 $comment->setAuthor(null);
  502.             }
  503.         }
  504.         return $this;
  505.     }
  506. }