<?php
namespace App\Entity\Gestiform\Users;
use App\Entity\AbstractEntity;
use App\Entity\Common\Adresse;
use App\Entity\Common\Upload;
use App\Enums\RolesEnum;
use App\Repository\Gestiform\Users\UserRepository;
use App\Trait\ApiGestiformEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\InheritanceType;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity("email", message="cette adresse email est déja prise")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({
* "user" = "App\Entity\Gestiform\Users\User",
* "employe" = "App\Entity\Gestiform\Users\Employe",
* "apprenant" = "App\Entity\Gestiform\Users\Apprenant",
* "formateur" = "App\Entity\Gestiform\Users\Formateur",
* "personne" = "App\Entity\Gestiform\Users\Personne",
* })
*/
class User extends AbstractEntity implements UserInterface, PasswordAuthenticatedUserInterface
{
use ApiGestiformEntity;
/**
* @ORM\Column(type="string", length=255, nullable=false)
* @Assert\Email()
*/
protected string $email;
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
protected ?string $password = null;
/**
* @ORM\Column(type="json")
*/
protected array $roles = [];
/**
* @ORM\Column(type="boolean")
*/
protected bool $enabled = true;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected ?bool $status = null;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $rgpd = false;
/**
* @ORM\ManyToOne(targetEntity=Adresse::class, cascade={"all"})
* @ORM\JoinColumn(nullable=true)
*/
protected ?Adresse $adresse = null;
/**
* @ORM\OneToOne(targetEntity=Upload::class, cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
*/
protected ?Upload $photo = null;
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(?bool $status): self
{
$this->status = $status;
return $this;
}
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = RolesEnum::ROLE_MEMBRE->value;
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function addRole(string $role): self
{
if (!in_array($role, $this->getRoles())) {
$this->roles = array_merge($this->roles, [$role]);
}
return $this;
}
public function removeRoles(): self
{
$this->roles = [];
return $this;
}
public function removeRole(string $role): self
{
$this->roles = array_diff($this->roles, [$role]);
return $this;
}
public function hasRole(string $role): bool
{
return in_array($role, $this->getRoles());
}
/**
* @param string[][] $rolesListsToCheck
* @return bool
* check with logic OR between args and logic AND between same array
*/
public function hasRoles(array ...$rolesListsToCheck): bool
{
$roles = new ArrayCollection($this->getRoles());
foreach ($rolesListsToCheck as $rolesToCheck) {
if ($roles->filter(fn(string $role) => in_array($role, $rolesToCheck))->count() !== 0) {
return true;
}
}
return false;
}
public function getUserIdentifier(): string
{
return $this->email;
}
public function getSalt(): ?string
{
return null;
}
public function getUsername(): string
{
return $this->email;
}
public function eraseCredentials()
{
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function isEnabled(): ?bool
{
return $this->enabled;
}
public function getAdresse(): ?Adresse
{
return $this->adresse;
}
public function setAdresse(?Adresse $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getPhoto(): ?Upload
{
return $this->photo;
}
public function setPhoto(?Upload $photo): self
{
$this->photo=$photo;
return $this;
}
public function isRgpd(): ?bool
{
return $this->rgpd;
}
public function setRgpd(?bool $rgpd): User
{
$this->rgpd = $rgpd;
return $this;
}
public function isStatus(): ?bool
{
return $this->status;
}
}