<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Ignore;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'Un compte existe déjà avec cet email.')]
class User implements UserInterface
{
const ROLES = [
'GLOBAL' => [
'ROLE_USER' => [
'level' => 0,
'label' => 'Utilisateur',
'icon' => 'user'
],
'ROLE_ADMIN' => [
'level' => 98,
'label' => 'Administrateur',
'icon' => 'tool'
],
'ROLE_SUPER_ADMIN' => [
'level' => 99,
'label' => 'Super-administrateur',
'icon' => 'key'
]
],
//Other roles groups by categories
/* Example
'CATEGORY' => [
'ROLE_CATEGORY_VIEW' => [
'level' => 0,
'label' => 'Category - View',
'icon' => 'user'
],
'ROLE_CATEGORY_EDIT' => [
'level' => 0,
'label' => 'Category - Edit',
'icon' => 'user'
]
] ,*/
];
const STATUS_INACTIVE = 'Inactif';
const STATUS_ACTIVE = 'Actif';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180)]
private $email;
#[ORM\Column(type: 'string', length: 55)]
private $firstname;
#[ORM\Column(type: 'string', length: 55)]
private $lastname;
#[ORM\Column(type: 'string', length: 20)]
private $status;
#[ORM\Column(type: 'json')]
private $roles = [];
#[ORM\Column(type: 'string', length: 255)]
private $password;
#[ORM\ManyToOne(targetEntity: File::class, fetch: "EAGER")]
#[ORM\JoinColumn(nullable: true)]
private $avatar;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Notification::class)]
private $notifications;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ReponseUser::class, orphanRemoval: true)]
private $reponseUsers;
#[ORM\Column(type: 'string', length: 55, nullable: true)]
private $entrepriseName;
#[ORM\Column(type: 'string', length: 55, nullable: true)]
private $entrepriseSiteWeb;
#[ORM\Column(type: 'text', nullable: true)]
private $entrepriseDescription;
#[ORM\Column(type: 'text', nullable: true)]
private $entrepriseAdresse;
#[ORM\Column(type: 'string', length: 55, nullable: true)]
private $entrepriseRCS;
#[ORM\Column(type: 'string', length: 55, nullable: true)]
private $entrepriseFormeJuridique;
#[ORM\Column(type: 'boolean')]
private $archive;
public function __construct()
{
$this->reponseUsers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUserIdentifier(): string
{
return $this->email;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
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;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
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 UserInterface
*/
public function getPassword(): string
{
return (string) $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 getUniqRole(): string
{
$uniqRole = 'ROLE_USER';
foreach ($this->getRoles() as $role) {
if (isset(self::ROLES['GLOBAL'][$role])) if (self::ROLES['GLOBAL'][$role]['level'] > self::ROLES['GLOBAL'][$uniqRole]['level']) $uniqRole = $role;
}
return $uniqRole;
}
public function getUniqRoleLabel(): string
{
return self::ROLES['GLOBAL'][$this->getUniqRole()]['label'];
}
/**
* Get the value of status
*/
public function getStatus()
{
return $this->status == null ? self::STATUS_INACTIVE : $this->status;
}
/**
* Set the value of status
*
* @return self
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get the value of avatar
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* Set the value of avatar
*
* @return self
*/
public function setAvatar($avatar)
{
$this->avatar = $avatar;
return $this;
}
/**
* @return Collection|Notification[]
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
public function addNotification(Notification $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUser($this);
}
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection<int, ReponseUser>
*/
public function getReponseUsers(): Collection
{
return $this->reponseUsers;
}
public function addReponseUser(ReponseUser $reponseUser): self
{
if (!$this->reponseUsers->contains($reponseUser)) {
$this->reponseUsers[] = $reponseUser;
$reponseUser->setUser($this);
}
return $this;
}
public function removeReponseUser(ReponseUser $reponseUser): self
{
if ($this->reponseUsers->removeElement($reponseUser)) {
// set the owning side to null (unless already changed)
if ($reponseUser->getUser() === $this) {
$reponseUser->setUser(null);
}
}
return $this;
}
/**
* Get the value of entrepriseName
*/
public function getEntrepriseName()
{
return $this->entrepriseName;
}
/**
* Set the value of entrepriseName
*
* @return self
*/
public function setEntrepriseName($entrepriseName)
{
$this->entrepriseName = $entrepriseName;
return $this;
}
/**
* Get the value of entrepriseSiteWeb
*/
public function getEntrepriseSiteWeb()
{
return $this->entrepriseSiteWeb;
}
/**
* Set the value of entrepriseSiteWeb
*
* @return self
*/
public function setEntrepriseSiteWeb($entrepriseSiteWeb)
{
$this->entrepriseSiteWeb = $entrepriseSiteWeb;
return $this;
}
/**
* Get the value of entrepriseDescription
*/
public function getEntrepriseDescription()
{
return $this->entrepriseDescription;
}
/**
* Set the value of entrepriseDescription
*
* @return self
*/
public function setEntrepriseDescription($entrepriseDescription)
{
$this->entrepriseDescription = $entrepriseDescription;
return $this;
}
/**
* Get the value of entrepriseAdresse
*/
public function getEntrepriseAdresse()
{
return $this->entrepriseAdresse;
}
/**
* Set the value of entrepriseAdresse
*
* @return self
*/
public function setEntrepriseAdresse($entrepriseAdresse)
{
$this->entrepriseAdresse = $entrepriseAdresse;
return $this;
}
/**
* Get the value of entrepriseRCS
*/
public function getEntrepriseRCS()
{
return $this->entrepriseRCS;
}
/**
* Set the value of entrepriseRCS
*
* @return self
*/
public function setEntrepriseRCS($entrepriseRCS)
{
$this->entrepriseRCS = $entrepriseRCS;
return $this;
}
/**
* Get the value of entrepriseFormeJuridique
*/
public function getEntrepriseFormeJuridique()
{
return $this->entrepriseFormeJuridique;
}
/**
* Set the value of entrepriseFormeJuridique
*
* @return self
*/
public function setEntrepriseFormeJuridique($entrepriseFormeJuridique)
{
$this->entrepriseFormeJuridique = $entrepriseFormeJuridique;
return $this;
}
public function isArchive(): ?bool
{
return $this->archive;
}
public function setArchive(bool $archive): self
{
$this->archive = $archive;
return $this;
}
}