<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[ORM\HasLifecycleCallbacks()]
#[Vich\Uploadable]
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable
{
public const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_RESPONSABLE = 'ROLE_RESPONSABLE';
public const ROLE_EMPLOYEE = 'ROLE_EMPLOYEE';
const strEtat = [
0 => 'Inactif',
1 => 'Actif',
2 => 'Supprimé'
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Assert\NotBlank()]
#[Assert\Email()]
private $email;
#[ORM\Column(type: 'json')]
private $roles = [];
#[ORM\Column(type: 'string')]
private $password;
#[Assert\Length(max:4096)]
private $plainPassword;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $firstName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $lastName;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $civility;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $userName;
#[ORM\Column(type: 'string', length: 30, nullable: true)]
private $phoneNumber;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $fonction;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $adresse;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $pays;
#[ORM\Column(type: 'boolean', nullable: true)]
private $active;
#[ORM\Column(type: 'boolean', nullable: true)]
private $etat = 1;
#[ORM\Column(type: 'integer')]
private $idParent;
#[ORM\Column(type: 'datetime', nullable: true)]
private $lastActivity;
#[ORM\Column(type: 'datetime', nullable: true)]
private $lastConnected;
#[ORM\ManyToMany(targetEntity: Groupe::class, mappedBy: 'listesOfEmployes')]
private $groupes;
#[ORM\OneToMany(mappedBy: 'responsable', targetEntity: Groupe::class)]
private $responsableGroupe;
/**
* @var File|null
* @Vich\UploadableField(mapping="user_profile", fileNameProperty="imageProfile")
* @Assert\File(
* maxSize = "2M",
* mimeTypes = {"image/jpeg", "image/png"},
* maxSizeMessage = "Le fichier est trop volumineux. Sa taille ne doit pas dépasser 2M",
* mimeTypesMessage = "Veuillez télécharger une image valide!"
* )
*/
private $imageProfileFile;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $imageProfile;
#[ORM\Column(type: 'datetime')]
private $createdAt;
#[ORM\Column(type: 'datetime', nullable: true)]
private $updatedAt;
#[ORM\Column(type: 'integer', nullable: true)]
private $createdBy;
#[ORM\Column(type: 'integer', nullable: true)]
private $updatedBy;
#[ORM\Column(type: "boolean")]
private $isVerified = false;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Comment::class)]
private $comments;
public function __construct()
{
$this->idParent = 0;
$this->groupes = new ArrayCollection();
$this->responsableGroupe = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->firstName,
$this->lastName,
$this->password,
));
}
public function unserialize($serialized)
{
list(
$this->id,
$this->email,
$this->firstName,
$this->lastName,
$this->password,
) = unserialize($serialized);
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($password)
{
$this->plainPassword = $password;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getCivility(): ?string
{
return $this->civility;
}
public function setCivility(?string $civility): self
{
$this->civility = $civility;
return $this;
}
public function setUserName(string $userName): self
{
$this->userName = $userName;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getFonction(): ?string
{
return $this->fonction;
}
public function setFonction(?string $fonction): self
{
$this->fonction = $fonction;
return $this;
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(?string $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getPays(): ?string
{
return $this->pays;
}
public function setPays(?string $pays): self
{
$this->pays = $pays;
return $this;
}
public function getImageProfile(): ?string
{
return $this->imageProfile;
}
public function setImageProfile(?string $imageProfile): self
{
$this->imageProfile = $imageProfile;
return $this;
}
/**
* @return null|File
*/
public function getImageProfileFile(): ?File
{
return $this->imageProfileFile;
}
/**
* @param null|File $imageProfileFile
* @return self
*/
public function setImageProfileFile(?File $imageProfileFile = null): self
{
$this->imageProfileFile = $imageProfileFile;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
public function getEtat(): ?bool
{
return $this->etat;
}
public function setEtat(?bool $etat): self
{
$this->etat = $etat;
return $this;
}
public function getIdParent(): ?int
{
return $this->idParent;
}
public function setIdParent(int $idParent): self
{
$this->idParent = $idParent;
return $this;
}
public function getLastActivity(): ?\DateTimeInterface
{
return $this->lastActivity;
}
public function setLastActivity(?\DateTimeInterface $lastActivity): self
{
$this->lastActivity = $lastActivity;
return $this;
}
public function getLastConnected(): ?\DateTimeInterface
{
return $this->lastConnected;
}
public function setLastConnected(?\DateTimeInterface $lastConnected): self
{
$this->lastConnected = $lastConnected;
return $this;
}
/**
* @return Collection|Groupe[]
*/
public function getGroupes(): Collection
{
return $this->groupes;
}
public function addGroupe(Groupe $groupe): self
{
if (!$this->groupes->contains($groupe)) {
$this->groupes[] = $groupe;
$groupe->addListesOfEmploye($this);
}
return $this;
}
public function removeGroupe(Groupe $groupe): self
{
if ($this->groupes->removeElement($groupe)) {
$groupe->removeListesOfEmploye($this);
}
return $this;
}
/**
* @return Collection|Groupe[]
*/
public function getResponsableGroupe(): Collection
{
return $this->responsableGroupe;
}
public function addResponsableGroupe(Groupe $responsableGroupe): self
{
if (!$this->responsableGroupe->contains($responsableGroupe)) {
$this->responsableGroupe[] = $responsableGroupe;
$responsableGroupe->setResponsable($this);
}
return $this;
}
public function removeResponsableGroupe(Groupe $responsableGroupe): self
{
if ($this->responsableGroupe->removeElement($responsableGroupe)) {
// set the owning side to null (unless already changed)
if ($responsableGroupe->getResponsable() === $this) {
$responsableGroupe->setResponsable(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedBy(): ?int
{
return $this->createdBy;
}
public function setCreatedBy(?int $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getUpdatedBy(): ?int
{
return $this->updatedBy;
}
public function setUpdatedBy(?int $updatedBy): self
{
$this->updatedBy = $updatedBy;
return $this;
}
public function getFullName(): string
{
return $this->firstName.' '.$this->lastName;
}
#[ORM\PrePersist]
public function onPrePersist()
{
if(empty($this->createdAt)){
$this->createdAt = new \DateTime();
}
}
#[ORM\PreUpdate]
public function onPreUpdate()
{
$this->updatedAt = new \DateTime();
$this->lastActivity = new \DateTime();
}
/**
* @return bool whether the user is active or not
*/
public function isActiveNow()
{
$delay = new \DateTime('1 minutes ago');
return($this->getlastActivity() > $delay);
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getIsVerified(): bool
{
return $this->isVerified;
}
public function getStrRoles($roles): string
{
$strRoles = '';
if(in_array(self::ROLE_SUPER_ADMIN, $roles)){
$strRoles = 'Super Admin';
}elseif(in_array(self::ROLE_RESPONSABLE, $roles)){
$strRoles = 'Résponsable';
}elseif(in_array(self::ROLE_EMPLOYEE, $roles)){
$strRoles = 'Employée';
}
return $strRoles;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setAuthor($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getAuthor() === $this) {
$comment->setAuthor(null);
}
}
return $this;
}
}