<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
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)]
#[ORM\Table(name: 'app_user')]
#[UniqueEntity(fields: ['email'], message: 'User.Form.ExistingEmail')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
public const ROLE_CLIENT_MANAGER = 'ROLE_CLIENT_MANAGER';
public const ROLE_CLIENT = 'ROLE_CLIENT';
public const ROLE_EMPLOYEE = 'ROLE_EMPLOYEE';
public const ROLES_AVAILABLES = [
self::ROLE_SUPER_ADMIN => 'Super Admin',
'ROLE_ADMIN' => 'Fayat - administateur',
'ROLE_USER' => 'Utilisateur',
self::ROLE_EMPLOYEE => 'Fayat - Utilisateur',
self::ROLE_CLIENT => 'Utilisateur Société (externe)',
self::ROLE_CLIENT_MANAGER => 'Responsable Société (externe)',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $email;
#[ORM\Column(type: 'json')]
private $roles = [];
#[ORM\Column(type: 'string', nullable: true)]
private $password;
#[Assert\NotBlank(groups: ['create'])]
#[Assert\Length(
min: 6,
max: 142,
groups: ['create']
)]
private ?string $plainPassword = null;
#[ORM\Column(type: 'boolean')]
private ?bool $isVerified = false;
#[ORM\Column(length: 20, nullable: true)]
private ?string $telephone = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeInterface $emailSentAt = null;
#[ORM\Column]
private ?bool $forceChangePassword = true;
#[ORM\Column]
private ?bool $enabledAlert = true;
#[ORM\Column(nullable: true)]
private ?bool $enabled = null;
#[ORM\Column(length: 75, nullable: true)]
private ?string $givenName = null;
#[ORM\Column(length: 75, nullable: true)]
private ?string $familyName = null;
#[ORM\ManyToOne(inversedBy: 'employees')]
private ?Company $company = null;
#[ORM\Column(nullable: true)]
private ?array $informationForModeration = null;
public function __toString(): string
{
return $this->email;
}
public function getDisplayName(): string
{
return $this->familyName ? strtoupper($this->familyName).' '.ucfirst($this->givenName) : $this->email;
}
public function isManager(): bool
{
return in_array(self::ROLE_CLIENT_MANAGER, $this->roles);
}
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;
}
/**
* @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(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(string $password): void
{
$this->plainPassword = $password;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getTelephone(): ?string
{
return $this->telephone;
}
public function setTelephone(?string $telephone): self
{
$this->telephone = $telephone;
return $this;
}
public function getEmailSentAt(): ?\DateTimeInterface
{
return $this->emailSentAt;
}
public function setEmailSentAt(?\DateTimeInterface $emailSentAt): self
{
$this->emailSentAt = $emailSentAt;
return $this;
}
public function isForceChangePassword(): ?bool
{
return $this->forceChangePassword;
}
public function setForceChangePassword(bool $forceChangePassword): self
{
$this->forceChangePassword = $forceChangePassword;
return $this;
}
public function isEnabledAlert(): ?bool
{
return $this->enabledAlert;
}
public function setEnabledAlert(bool $enabledAlert): self
{
$this->enabledAlert = $enabledAlert;
return $this;
}
public function isEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(?bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getGivenName(): ?string
{
return $this->givenName;
}
public function setGivenName(?string $givenName): self
{
$this->givenName = $givenName;
return $this;
}
public function getFamilyName(): ?string
{
return $this->familyName;
}
public function setFamilyName(?string $familyName): self
{
$this->familyName = $familyName;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getLocale(): string
{
return $this->getCompany()?->getLocale() ?? 'fr';
}
public function getInformationForModeration(): ?array
{
return $this->informationForModeration;
}
public function setInformationForModeration(?array $informationForModeration): self
{
$this->informationForModeration = $informationForModeration;
return $this;
}
public function isPremium(): bool
{
return $this->getCompany()?->isPremium() ?? false;
}
}