<?php
namespace App\Entity\User;
use App\Entity\Project\Project;
use App\Entity\Request\RequestComment;
use App\Entity\Request\RequestForm;
use App\Entity\Request\RequestHistory;
use App\Repository\User\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const ROLE_SUPER_ADMIN ='ROLE_SUPER_ADMIN';
const ROLE_ADMIN = 'ROLE_ADMIN';
const ROLE_PM = 'ROLE_PM';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column(nullable: true)]
private ?string $password = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $deletedAt = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $firstName = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $lastName = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $phone = null;
#[ORM\Column(nullable: true)]
private ?bool $isEnable = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: RequestHistory::class)]
private Collection $requestHistories;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Project::class)]
private Collection $projects;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: RequestComment::class)]
private Collection $requestComments;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: RequestForm::class)]
private Collection $requestForms;
#[ORM\Column(length: 255, nullable: true)]
private ?string $role = null;
public function __construct()
{
$this->requestHistories = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->requestComments = new ArrayCollection();
$this->requestForms = new ArrayCollection();
}
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;
}
/**
* 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 getDeletedAt(): ?\DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
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 getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function isIsEnable(): ?bool
{
return $this->isEnable;
}
public function setIsEnable(?bool $isEnable): self
{
$this->isEnable = $isEnable;
return $this;
}
/**
* @return Collection<int, RequestHistory>
*/
public function getRequestHistories(): Collection
{
return $this->requestHistories;
}
public function addRequestHistory(RequestHistory $requestHistory): self
{
if (!$this->requestHistories->contains($requestHistory)) {
$this->requestHistories->add($requestHistory);
$requestHistory->setUser($this);
}
return $this;
}
public function removeRequestHistory(RequestHistory $requestHistory): self
{
if ($this->requestHistories->removeElement($requestHistory)) {
// set the owning side to null (unless already changed)
if ($requestHistory->getUser() === $this) {
$requestHistory->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Project>
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): static
{
if (!$this->projects->contains($project)) {
$this->projects->add($project);
$project->setUser($this);
}
return $this;
}
public function removeProject(Project $project): static
{
if ($this->projects->removeElement($project)) {
// set the owning side to null (unless already changed)
if ($project->getUser() === $this) {
$project->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, RequestComment>
*/
public function getRequestComments(): Collection
{
return $this->requestComments;
}
public function addRequestComment(RequestComment $requestComment): static
{
if (!$this->requestComments->contains($requestComment)) {
$this->requestComments->add($requestComment);
$requestComment->setUser($this);
}
return $this;
}
public function removeRequestComment(RequestComment $requestComment): static
{
if ($this->requestComments->removeElement($requestComment)) {
// set the owning side to null (unless already changed)
if ($requestComment->getUser() === $this) {
$requestComment->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, RequestForm>
*/
public function getRequestForms(): Collection
{
return $this->requestForms;
}
public function addRequestForm(RequestForm $requestForm): static
{
if (!$this->requestForms->contains($requestForm)) {
$this->requestForms->add($requestForm);
$requestForm->setUser($this);
}
return $this;
}
public function removeRequestForm(RequestForm $requestForm): static
{
if ($this->requestForms->removeElement($requestForm)) {
// set the owning side to null (unless already changed)
if ($requestForm->getUser() === $this) {
$requestForm->setUser(null);
}
}
return $this;
}
public function getRole(): ?string
{
return $this->roles[0] ?? null; // Retourne le premier rôle
}
public function setRole(?string $role): self
{
$this->roles = [$role]; // Met à jour les rôles comme tableau
return $this;
}
}