<?php
namespace App\Entity;
use App\Entity\Common\RoleInterface;
use App\Entity\Traits\BaseDateAtTrait;
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;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity('email')]
class User implements UserInterface, RoleInterface, PasswordAuthenticatedUserInterface
{
use BaseDateAtTrait;
// The list of civility
public const CIVILITY_MISSIS = 1;
public const CIVILITY_MISTER = 2;
// Full list
public const CIVILITY_LIST = [
self::CIVILITY_MISSIS => 'app.user.label.missis',
self::CIVILITY_MISTER => 'app.user.label.mister',
];
public const BINDINGS = [
'[[USER.FULLNAME]]',
'[[USER.NAME]]',
'[[USER.EMAIL]]',
'[[USER.STATUS]]',
'[[USER.UNIVERSITY]]',
'[[USER.ADMIN.REVIEW]]',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
private $username;
#[ORM\Column(type: 'json')]
private $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column(type: 'string')]
private $password;
#[Assert\NotBlank(groups: ['ResetPassword', 'Registration', 'ChangePassword', 'AccountChangePassword'])]
#[Assert\Length(min: 8, max: 4096, minMessage: 'app.user.password.length', groups: ['Profile', 'ResetPassword', 'Registration', 'ChangePassword', 'AccountChangePassword'])]
#[Assert\Regex(pattern: '/^([^0-9]*|[^A-Z]*|[a-zA-Z0-9]*)$/', message: 'app.user.password.rule', match: false, groups: ['Profile', 'ResetPassword', 'Registration', 'ChangePassword', 'AccountChangePassword'])]
protected $plainPassword;
#[ORM\Column(type: 'smallint', nullable: true)]
private $civility;
#[ORM\Column(type: 'string', length: 255)]
private $firstName;
#[ORM\Column(type: 'string', length: 255)]
private $lastName;
#[ORM\Column(type: 'string', length: 255, unique: true)]
private $email;
#[ORM\Column(type: 'string', length: 255, unique: true)]
private $uuid;
#[ORM\Column(type: 'datetime', nullable: true)]
private $lastLogin;
#[ORM\OneToMany(targetEntity: PersonalResource::class, mappedBy: 'user', cascade: ['remove'])]
private $personalResources;
#[ORM\OneToMany(targetEntity: UserProject::class, mappedBy: 'user', cascade: ['remove'])]
private $userProjects;
#[ORM\OneToMany(targetEntity: UserWorkroom::class, mappedBy: 'user', cascade: ['remove'])]
private $userWorkrooms;
#[ORM\OneToMany(targetEntity: ReadingCard::class, mappedBy: 'user')]
private $readingCards;
#[ORM\OneToMany(targetEntity: ReadingCard::class, mappedBy: 'lastUser')]
private $lastUserReadingCards;
#[ORM\ManyToOne(targetEntity: University::class, inversedBy: 'users')]
private $university;
#[ORM\OneToOne(targetEntity: UserProfile::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
private $userProfile;
#[ORM\OneToMany(targetEntity: PersonalFolder::class, mappedBy: 'user', cascade: ['remove'])]
private $personalFolders;
#[ORM\OneToOne(targetEntity: ResetPasswordToken::class, mappedBy: 'User', cascade: ['persist','remove'])]
private $resetPasswordToken;
#[ORM\Column(type: 'boolean', options: ['default' => 0])]
private $isActive;
#[ORM\ManyToOne(targetEntity: Statut::class)]
private $statut;
#[ORM\OneToMany(targetEntity: Notification::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
private $notifications;
#[ORM\Column(type: 'string', length: 500, nullable: true)]
private $adminReview;
#[ORM\Column(type: 'boolean', options: ['default' => 0])]
private $isDisabled;
#[ORM\OneToMany(targetEntity: SectionRevision::class, mappedBy: 'user', cascade: ['persist'])]
private $sectionRevisions;
#[ORM\OneToMany(targetEntity: WorkroomRevision::class, mappedBy: 'user')]
private $workroomRevisions;
#[ORM\OneToMany(targetEntity: UserWorkroomArena::class, mappedBy: 'user', orphanRemoval: true, cascade: ['remove'])]
private $userWorkroomArenas;
#[ORM\OneToMany(targetEntity: SearchQuery::class, mappedBy: 'user', orphanRemoval: true, cascade: ['remove'])]
private $searchQueries;
#[ORM\OneToMany(targetEntity: History::class, mappedBy: 'user', orphanRemoval: true, cascade: ['remove'])]
private $histories;
#[ORM\OneToMany(targetEntity: HistoryEvent::class, mappedBy: 'User', orphanRemoval: true, cascade: ['remove'])]
private $historyEvents;
#[ORM\ManyToOne(inversedBy: 'users')]
private ?Organization $organization = null;
#[ORM\Column(type: 'string', length: 180, nullable: true, unique: true)]
private $zoteroUserId;
#[ORM\Column(type: 'string', length: 255, nullable: true, unique: true)]
private $zoteroApiKey;
/** Clé API Semantic Scholar par utilisateur (https://www.semanticscholar.org/product/api). */
#[ORM\Column(type: 'string', length: 255, nullable: true, name: 'semantic_scholar_api_key')]
private ?string $semanticScholarApiKey = null;
#[ORM\Column(nullable: true)]
private ?string $twoFactorCode = null;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?\DateTimeInterface $twoFactorExpiresAt = null;
#[ORM\Column(type: 'boolean')]
private bool $isTwoFactorEnabled = false;
/**
* User constructor.
*/
public function __construct()
{
$this->personalResources = new ArrayCollection();
$this->userProjects = new ArrayCollection();
$this->userWorkrooms = new ArrayCollection();
$this->readingCards = new ArrayCollection();
$this->personalFolders = new ArrayCollection();
$this->isActive = $this->isDisabled = false;
$this->notifications = new ArrayCollection();
$this->workroomRevisions = new ArrayCollection();
$this->userWorkroomArenas = new ArrayCollection();
$this->searchQueries = new ArrayCollection();
$this->histories = new ArrayCollection();
$this->historyEvents = new ArrayCollection();
}
/**
* @return string
*/
public function __toString()
{
return $this->firstName.' '.$this->lastName;
}
public function getBindings(): array
{
return [
'USER.FULLNAME' => $this->getFullName(),
'USER.NAME' => strtoupper($this->getLastName()),
'USER.EMAIL' => $this->getEmail(),
'USER.STATUS' => $this->getStatut(),
'USER.UNIVERSITY' => $this->getOrganization(),
'USER.ORGANIZATION' => $this->getOrganization(),
'USER.ADMIN.REVIEW' => $this->getAdminReview()
];
}
public function getFullName(): string
{
return $this->firstName.' '.strtoupper($this->lastName);
}
public function activate()
{
$this->setIsActive(true);
$this->addRole(RoleInterface::ROLE_USER);
}
/**
* Gets triggered only on insert.
* Set created date.
*/
#[ORM\PrePersist]
public function onPrePersist()
{
$this->generateUuid();
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function isItAdmin(): bool
{
if (!empty(array_intersect(RoleInterface::ROLE_ADMINS, $this->roles))) {
return true;
}
return false;
}
public function hasRoles(array $roles): bool
{
if (!empty(array_intersect($roles, $this->roles))) {
return true;
}
return false;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->username;
}
/**
* @return $this
*/
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @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 addRole(string $role): User
{
if (!in_array($role, $this->roles)) {
$this->roles[] = $role;
}
return $this;
}
/**
* @return $this
*/
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function hasRole(string $role): bool
{
return false !== array_search($role, $this->getRoles());
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
/**
* @return $this
*/
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()
{
$this->plainPassword = null;
}
public function getCivility(): ?int
{
return $this->civility;
}
/**
* @return $this
*/
public function setCivility(?int $civility): self
{
$this->civility = $civility;
return $this;
}
/**
* Provide the name of civility bases on key value.
*/
public function getCivilityName(): string
{
return self::CIVILITY_LIST[$this->civility] ?? '';
}
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @return $this
*/
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @return $this
*/
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
/**
* @return $this
*/
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getUuid(): ?string
{
return $this->uuid;
}
/**
* @return $this
*/
public function setUuid(string $uuid): self
{
$this->uuid = $uuid;
return $this;
}
/**
* @return $this
*
* @throws \Exception
*/
public function generateUuid(): self
{
$this->uuid = bin2hex(random_bytes(16));
return $this;
}
public function getLastLogin(): ?\DateTimeInterface
{
return $this->lastLogin;
}
/**
* @return $this
*/
public function setLastLogin(?\DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* @return Collection|PersonalResource[]
*/
public function getPersonalResources(): Collection
{
return $this->personalResources;
}
/**
* @return $this
*/
public function addPersonalResource(PersonalResource $personalResource): self
{
if (!$this->personalResources->contains($personalResource)) {
$this->personalResources[] = $personalResource;
$personalResource->setUser($this);
}
return $this;
}
/**
* @return $this
*/
public function removePersonalResource(PersonalResource $personalResource): self
{
if ($this->personalResources->removeElement($personalResource)) {
// set the owning side to null (unless already changed)
if ($personalResource->getUser() === $this) {
$personalResource->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserProject[]
*/
public function getUserProjects(): Collection
{
return $this->userProjects;
}
public function addUserProject(UserProject $userProject): self
{
if (!$this->userProjects->contains($userProject)) {
$this->userProjects[] = $userProject;
$userProject->setUser($this);
}
return $this;
}
public function removeUserProject(UserProject $userProject): self
{
if ($this->userProjects->removeElement($userProject)) {
// set the owning side to null (unless already changed)
if ($userProject->getUser() === $this) {
$userProject->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserWorkroom[]
*/
public function getUserWorkrooms(): Collection
{
return $this->userWorkrooms;
}
public function addUserWorkroom(UserWorkroom $userWorkroom): self
{
if (!$this->userWorkrooms->contains($userWorkroom)) {
$this->userWorkrooms[] = $userWorkroom;
$userWorkroom->setUser($this);
}
return $this;
}
public function removeUserWorkroom(UserWorkroom $userWorkroom): self
{
if ($this->userWorkrooms->removeElement($userWorkroom)) {
// set the owning side to null (unless already changed)
if ($userWorkroom->getUser() === $this) {
$userWorkroom->setUser(null);
}
}
return $this;
}
/**
* @return Collection|ReadingCard[]
*/
public function getReadingCards(): Collection
{
return $this->readingCards;
}
public function addReadingCard(ReadingCard $readingCard): self
{
if (!$this->readingCards->contains($readingCard)) {
$this->readingCards[] = $readingCard;
$readingCard->setUser($this);
}
return $this;
}
public function removeReadingCard(ReadingCard $readingCard): self
{
if ($this->readingCards->removeElement($readingCard)) {
// set the owning side to null (unless already changed)
if ($readingCard->getUser() === $this) {
$readingCard->setUser(null);
}
}
return $this;
}
public function getUniversity(): ?University
{
return $this->university;
}
public function setUniversity(?University $university): self
{
$this->university = $university;
return $this;
}
public function getUserProfile(): ?UserProfile
{
return $this->userProfile;
}
public function setUserProfile(UserProfile $userProfile): self
{
// set the owning side of the relation if necessary
if ($userProfile->getUser() !== $this) {
$userProfile->setUser($this);
}
$this->userProfile = $userProfile;
return $this;
}
/**
* @return Collection|PersonalFolder[]
*/
public function getPersonalFolders(): Collection
{
return $this->personalFolders;
}
public function addPersonalFolder(PersonalFolder $personalFolder): self
{
if (!$this->personalFolders->contains($personalFolder)) {
$this->personalFolders[] = $personalFolder;
$personalFolder->setUser($this);
}
return $this;
}
public function removePersonalFolder(PersonalFolder $personalFolder): self
{
if ($this->personalFolders->removeElement($personalFolder)) {
// set the owning side to null (unless already changed)
if ($personalFolder->getUser() === $this) {
$personalFolder->setUser(null);
}
}
return $this;
}
public function getResetPasswordToken(): ?ResetPasswordToken
{
return $this->resetPasswordToken;
}
public function setResetPasswordToken(ResetPasswordToken $resetPasswordToken): self
{
// set the owning side of the relation if necessary
if ($resetPasswordToken->getUser() !== $this) {
$resetPasswordToken->setUser($this);
}
$this->resetPasswordToken = $resetPasswordToken;
return $this;
}
public function isActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getStatut(): ?Statut
{
return $this->statut;
}
public function setStatut(?Statut $statut): self
{
$this->statut = $statut;
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 removeNotification(Notification $notification): self
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
public function getAdminReview(): ?string
{
return $this->adminReview;
}
public function setAdminReview(?string $adminReview): self
{
$this->adminReview = $adminReview;
return $this;
}
public function getIsDisabled(): ?bool
{
return $this->isDisabled;
}
public function setIsDisabled(bool $isDisabled): self
{
$this->isDisabled = $isDisabled;
return $this;
}
/**
* @return Collection|SectionRevision[]
*/
public function getSectionRevisions(): Collection
{
return $this->sectionRevisions;
}
/**
* @return $this
*/
public function addSectionRevision(SectionRevision $sectionRevision): self
{
if (!$this->sectionRevisions->contains($sectionRevision)) {
$this->sectionRevisions[] = $sectionRevision;
$sectionRevision->setUser($this);
}
return $this;
}
/**
* @return $this
*/
public function removeSectionRevision(SectionRevision $sectionRevision): self
{
if ($this->sectionRevisions->removeElement($sectionRevision)) {
// set the owning side to null (unless already changed)
if ($sectionRevision->getUser() === $this) {
$sectionRevision->setUser(null);
}
}
return $this;
}
/**
* @return Collection|WorkroomRevision[]
*/
public function getWorkroomRevisions(): Collection
{
return $this->workroomRevisions;
}
public function addWorkroomRevision(WorkroomRevision $workroomRevision): self
{
if (!$this->workroomRevisions->contains($workroomRevision)) {
$this->workroomRevisions[] = $workroomRevision;
$workroomRevision->setUser($this);
}
return $this;
}
public function removeWorkroomRevision(WorkroomRevision $workroomRevision): self
{
if ($this->workroomRevisions->removeElement($workroomRevision)) {
// set the owning side to null (unless already changed)
if ($workroomRevision->getUser() === $this) {
$workroomRevision->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserWorkroomArena[]
*/
public function getUserWorkroomArenas(): Collection
{
return $this->userWorkroomArenas;
}
public function addUserWorkroomArena(UserWorkroomArena $userWorkroomArena): self
{
if (!$this->userWorkroomArenas->contains($userWorkroomArena)) {
$this->userWorkroomArenas[] = $userWorkroomArena;
$userWorkroomArena->setUser($this);
}
return $this;
}
public function removeUserWorkroomArena(UserWorkroomArena $userWorkroomArena): self
{
if ($this->userWorkroomArenas->removeElement($userWorkroomArena)) {
// set the owning side to null (unless already changed)
if ($userWorkroomArena->getUser() === $this) {
$userWorkroomArena->setUser(null);
}
}
return $this;
}
/**
* @return Collection|SearchQuery[]
*/
public function getSearchQueries(): Collection
{
return $this->searchQueries;
}
public function addSearchQuery(SearchQuery $searchQuery): self
{
if (!$this->searchQueries->contains($searchQuery)) {
$this->searchQueries[] = $searchQuery;
$searchQuery->setUser($this);
}
return $this;
}
public function removeSearchQuery(SearchQuery $searchQuery): self
{
if ($this->searchQueries->removeElement($searchQuery)) {
// set the owning side to null (unless already changed)
if ($searchQuery->getUser() === $this) {
$searchQuery->setUser(null);
}
}
return $this;
}
/**
* @return Collection|History[]
*/
public function getHistories(): Collection
{
return $this->histories;
}
public function addHistory(History $history): self
{
if (!$this->histories->contains($history)) {
$this->histories[] = $history;
$history->setUser($this);
}
return $this;
}
public function removeHistory(History $history): self
{
if ($this->histories->removeElement($history)) {
// set the owning side to null (unless already changed)
if ($history->getUser() === $this) {
$history->setUser(null);
}
}
return $this;
}
/**
* @return Collection|HistoryEvent[]
*/
public function getHistoryEvents(): Collection
{
return $this->historyEvents;
}
public function addHistoryEvent(HistoryEvent $historyEvent): self
{
if (!$this->historyEvents->contains($historyEvent)) {
$this->historyEvents[] = $historyEvent;
$historyEvent->setUser($this);
}
return $this;
}
public function removeHistoryEvent(HistoryEvent $historyEvent): self
{
if ($this->historyEvents->removeElement($historyEvent)) {
// set the owning side to null (unless already changed)
if ($historyEvent->getUser() === $this) {
$historyEvent->setUser(null);
}
}
return $this;
}
public function getUserIdentifier(): string
{
return $this->getUsername();
}
public function getOrganization(): ?Organization
{
return $this->organization;
}
public function setOrganization(?Organization $organization): static
{
$this->organization = $organization;
return $this;
}
public function setZoteroApiKey(?string $zoteroApiKey): self
{
$this->zoteroApiKey = $zoteroApiKey;
return $this;
}
public function getZoteroApiKey(): ?string
{
return $this->zoteroApiKey;
}
public function setZoteroUserId(?string $zoteroUserId): self
{
$this->zoteroUserId = $zoteroUserId;
return $this;
}
public function getEmailDomain(): ?string
{
if (1 === preg_match('/.*\@(.+)$/i', $this->email, $matches) && count($matches) > 1) {
return $matches[1];
}
return null;
}
public function getZoteroUserId(): ?string
{
return $this->zoteroUserId;
}
public function getSemanticScholarApiKey(): ?string
{
return $this->semanticScholarApiKey;
}
public function setSemanticScholarApiKey(?string $semanticScholarApiKey): self
{
$this->semanticScholarApiKey = $semanticScholarApiKey;
return $this;
}
public function isTwoFactorEnabled(): bool
{
return $this->isTwoFactorEnabled;
}
public function setIsTwoFactorEnabled(bool $enabled): static
{
$this->isTwoFactorEnabled = $enabled;
return $this;
}
public function getTwoFactorCode(): ?string
{
return $this->twoFactorCode;
}
public function setTwoFactorCode(?string $code): static
{
$this->twoFactorCode = $code;
return $this;
}
public function getTwoFactorExpiresAt(): ?\DateTimeInterface
{
return $this->twoFactorExpiresAt;
}
public function setTwoFactorExpiresAt(?\DateTimeInterface $expiresAt): static
{
$this->twoFactorExpiresAt = $expiresAt;
return $this;
}
}