<?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 Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
{
use \App\Traits\CommonTrt;
/**
* @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 = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastname;
/**
* @ORM\ManyToMany(targetEntity=Roles::class, inversedBy="users")
*/
private $userRoles;
/**
* @ORM\Column(type="boolean")
*/
private $isActive;
/**
* @ORM\Column(type="string", nullable=true)
*/
private string $authCode;
/**
* @ORM\OneToMany(targetEntity=Historiques::class, mappedBy="user")
*/
private $historiques;
// [...]
public function isEmailAuthEnabled(): bool
{
return true; // This can be a persisted field to switch email code authentication on/off
}
public function getEmailAuthRecipient(): string
{
return $this->email;
}
public function getEmailAuthCode(): string
{
if (null === $this->authCode) {
throw new \LogicException('The email authentication code was not set');
}
return $this->authCode;
}
public function setEmailAuthCode(string $authCode): void
{
$this->authCode = $authCode;
}
public function __construct() {
$this->userRoles = new ArrayCollection();
$this->historiques = 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() {
$roles = $this->userRoles->map(function ($role) {
return $role->getName();
})->toArray();
$roles[] = 'ROLE_USER';
return $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 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;
}
/**
* @return Collection<int, Roles>
*/
public function getUserRoles(): Collection {
return $this->userRoles;
}
public function addUserRole(Roles $userRole): self {
if (!$this->userRoles->contains($userRole)) {
$this->userRoles[] = $userRole;
}
return $this;
}
public function removeUserRole(Roles $userRole): self {
$this->userRoles->removeElement($userRole);
return $this;
}
public function getFullName() {
return "$this->firstname $this->lastname";
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function __toString()
{
return $this->getFullName();
}
/**
* @return Collection<int, Historiques>
*/
public function getHistoriques(): Collection
{
return $this->historiques;
}
public function addHistorique(Historiques $historique): self
{
if (!$this->historiques->contains($historique)) {
$this->historiques[] = $historique;
$historique->setUser($this);
}
return $this;
}
public function removeHistorique(Historiques $historique): self
{
if ($this->historiques->removeElement($historique)) {
// set the owning side to null (unless already changed)
if ($historique->getUser() === $this) {
$historique->setUser(null);
}
}
return $this;
}
}