<?php
namespace App\Entity;
use App\Entity\Traits\Identifiable;
use App\Entity\Traits\Timestampable;
use App\Entity\Webmail\AccountAccess;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
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;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(['email'], message: 'Un espace SCUF associé à cet email existe déjà')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable
{
use Timestampable;
use Identifiable;
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $email;
#[ORM\Column(type: 'json')]
private $roles = [];
#[ORM\Column(type: 'string')]
private $password;
private ?string $plainPassword = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $userAgent;
#[ORM\Column(type: 'string', length: 15, nullable: true)]
private $ip;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private $lastLoginAt;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $resetToken;
#[ORM\OneToOne(mappedBy: 'user', targetEntity: Person::class, cascade: ['persist', 'remove'])]
private $person;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: AdminSection::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private $adminSections;
#[ORM\Column(type: 'boolean')]
private $isTemporary = false;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: AccountAccess::class)]
private Collection $webmailAccountAccesses;
public function __construct()
{
$this->adminSections = new ArrayCollection();
$this->webmailAccountAccesses = new ArrayCollection();
}
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;
}
public function addRole($role): self
{
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
public function hasRole($role): bool
{
return in_array($role, $this->roles, true);
}
public function removeRole($role): self
{
if (($key = array_search($role, $this->roles, true)) !== false) {
unset($this->roles[$key]);
}
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
$this->plainPassword = null;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): User
{
$this->plainPassword = $plainPassword;
$this->password = null;
return $this;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(?string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(?string $ip): self
{
$this->ip = $ip;
return $this;
}
public function getLastLoginAt(): ?\DateTimeImmutable
{
return $this->lastLoginAt;
}
public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): self
{
$this->lastLoginAt = $lastLoginAt;
return $this;
}
public function getResetToken(): ?string
{
return $this->resetToken;
}
public function setResetToken(?string $resetToken): self
{
$this->resetToken = $resetToken;
return $this;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
// unset the owning side of the relation if necessary
if ($person === null && $this->person !== null) {
$this->person->setUser(null);
}
// set the owning side of the relation if necessary
if ($person !== null && $person->getUser() !== $this) {
$person->setUser($this);
}
$this->person = $person;
return $this;
}
/**
* @return Collection<int, AdminSection>
*/
public function getAdminSections(): Collection
{
return $this->adminSections;
}
public function addAdminSection(AdminSection $adminSection): self
{
if (!$this->adminSections->contains($adminSection)) {
$this->adminSections[] = $adminSection;
$adminSection->setUser($this);
}
return $this;
}
public function removeAdminSection(AdminSection $adminSection): self
{
if ($this->adminSections->removeElement($adminSection)) {
// set the owning side to null (unless already changed)
if ($adminSection->getUser() === $this) {
$adminSection->setUser(null);
}
}
return $this;
}
public function __toString(): string
{
return $this->getPerson()?->getFullName() ?? $this->getEmail();
}
public function isIsTemporary(): ?bool
{
return $this->isTemporary;
}
public function setIsTemporary(bool $isTemporary): self
{
$this->isTemporary = $isTemporary;
return $this;
}
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->password,
));
}
public function unserialize(string $data)
{
list(
$this->id,
$this->email,
$this->password,
) = unserialize($data, ['allowed_classes' => false]);
}
/**
* @return Collection<int, AccountAccess>
*/
public function getWebmailAccountAccesses(): Collection
{
return $this->webmailAccountAccesses;
}
public function addWebmailAccountAccess(AccountAccess $webmailAccountAccess): self
{
if (!$this->webmailAccountAccesses->contains($webmailAccountAccess)) {
$this->webmailAccountAccesses->add($webmailAccountAccess);
$webmailAccountAccess->setUser($this);
}
return $this;
}
public function removeWebmailAccountAccess(AccountAccess $webmailAccountAccess): self
{
if ($this->webmailAccountAccesses->removeElement($webmailAccountAccess)) {
// set the owning side to null (unless already changed)
if ($webmailAccountAccess->getUser() === $this) {
$webmailAccountAccess->setUser(null);
}
}
return $this;
}
}