<?php
/**
* @package BLUUE
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTime;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use App\DoctrineExtensions\IsActiveEntity;
use App\Entity\Traits\OpensslServiceEntity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* formats={"json"},
* security="is_granted('ROLE_USER')",
* normalizationContext= {"groups"= {"get"}},
* denormalizationContext={"groups"={"write"}},
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_READ_ONLY')"
* },
* "post"={
* "security"="is_granted('ROLE_SUPER_ADMIN')"
* }
* },
* itemOperations={
* "get",
* "put"={
* "security"="is_granted('ROLE_SUPER_ADMIN')"
* },
* "patch"={
* "security"="is_granted('ROLE_SUPER_ADMIN')"
* },
* "delete"={
* "security"="is_granted('ROLE_SUPER_ADMIN')"
* }
* }
* )
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="user", indexes={
* @ORM\Index(name="name", columns={"firstname", "lastname"}),
* @ORM\Index(name="is_active", columns={"is_active"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @UniqueEntity(
* fields={"email", "deletedAt"},
* errorPath="email",
* message="This email is already in use.",
* ignoreNull=false
* )
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsActiveEntity;
use OpensslServiceEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @Groups({"get", "userSearch"})
*/
private ?Uuid $id = null;
/**
* @ORM\Column(type="string", length=32)
* @Groups({"get", "write"})
*/
private string $firstname;
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"get", "write"})
*/
private ?string $lastname = null;
/**
* @ORM\Column(type="string", length=128)
* @Assert\Email
* @Groups({"get", "write"})
*/
private string $email;
/**
* @ORM\Column(type="string", length=32, nullable=true)
* @Groups({"get", "write"})
*/
private ?string $mobilePhone;
/**
* @var ?string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private ?string $password = null;
/**
* @ORM\Column(type="string", length=40, nullable=true)
*/
private ?string $token;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $token_validity;
/**
* @ORM\Column(type="string", length=128, nullable=true)
* @Groups({"get"})
*/
private ?string $loginName = null;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private ?string $apiPin = null;
/**
* @ORM\Column(type="string", length=40, nullable=true)
*/
private ?string $apiToken = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?DateTime $apiTokenValidity;
/**
* @ORM\Column(type="json", nullable=true)
* @Groups({"get"})
*/
private array $config = [];
/**
* @ORM\ManyToOne(targetEntity=Profile::class, inversedBy="users")
* @Groups({"get"})
*/
private ?Profile $profile;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"get"})
*/
private ?string $emailSignature = null;
/**
* @ORM\OneToMany(targetEntity=UserLoginLog::class, mappedBy="user", fetch="EXTRA_LAZY")
*/
private Collection $userLoginLogs;
/**
* @ORM\OneToMany(targetEntity=Notification::class, mappedBy="user", fetch="EXTRA_LAZY")
*/
private Collection $notifications;
/**
* @ORM\OneToMany(targetEntity=UserContext::class, mappedBy="user", fetch="EXTRA_LAZY")
*/
private Collection $userContexts;
/**
* @ORM\ManyToOne(targetEntity=Language::class, inversedBy="users")
* @Groups({"get"})
* #[ApiProperty(readableLink: false, writableLink: false)]
*/
private ?Language $language = null;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"get"})
*/
private ?string $note = null;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $isSolo = false;
/**
* @ORM\OneToMany(targetEntity=MailServer::class, mappedBy="user", fetch="EXTRA_LAZY")
*/
private Collection $mail_servers;
/**
* @ORM\OneToMany(targetEntity=TeamUser::class, mappedBy="user", fetch="EXTRA_LAZY")
*/
private Collection $teamUsers;
/**
* @ORM\OneToMany(targetEntity=UsersEstablishments::class, mappedBy="user")
*/
private ?Collection $usersEstablishments;
public function __construct()
{
$this->userLoginLogs = new ArrayCollection();
$this->notifications = new ArrayCollection();
$this->userContexts = new ArrayCollection();
$this->mail_servers = new ArrayCollection();
$this->teamUsers = new ArrayCollection();
$this->usersEstablishments = new ArrayCollection();
}
/**
* @Groups({"get"})
* @return string
*/
public function getUserIdentifier(): string
{
return $this->email;
}
/**
* @Groups({"get"})
* @return string
* @deprecated since symfony 5.3
*/
public function getUsername(): string
{
return $this->getUserIdentifier();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return string|null
*/
public function getFirstname(): ?string
{
return $this->firstname;
}
/**
* @return string|null
*/
public function getLastname(): ?string
{
return $this->lastname;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @return string|null
*/
public function getMobilePhone(): ?string
{
return $this->mobilePhone;
}
/**
* @return string|null
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* @return string|null
*/
public function getToken(): ?string
{
return $this->token;
}
/**
* @return DateTime|null
*/
public function getTokenValidity(): ?DateTime
{
return $this->token_validity;
}
/**
* @return string|null
*/
public function getLoginName(): ?string
{
return $this->loginName;
}
/**
* @return string|null
*/
public function getApiPin(): ?string
{
$apiPin = $this->apiPin;
$apiPin = $apiPin ? $this->openssl->decrypt($apiPin) : null;
return $apiPin;
}
/**
* @return string|null
*/
public function getApiToken(): ?string
{
return $this->apiToken;
}
/**
* @return DateTime|null
*/
public function getApiTokenValidity(): ?DateTime
{
return $this->apiTokenValidity;
}
/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}
/**
* @param string $firstname
* @return $this
*/
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
/**
* @param string|null $lastname
* @return $this
*/
public function setLastname(?string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
/**
* @param string $email
* @return $this
*/
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @param string|null $mobilePhone
* @return $this
*/
public function setMobilePhone(?string $mobilePhone): self
{
$this->mobilePhone = $mobilePhone;
return $this;
}
/**
* @param string|null $password
* @return $this
*/
public function setPassword(?string $password = null): self
{
$this->password = $password;
return $this;
}
/**
* @param string|null $token
* @return $this
*/
public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}
/**
* @param DateTime|null $token_validity
* @return $this
*/
public function setTokenValidity(?DateTime $token_validity): self
{
$this->token_validity = $token_validity;
return $this;
}
/**
* @param string|null $loginName
* @return $this
*/
public function setLoginName(?string $loginName): self
{
$this->loginName = $loginName;
return $this;
}
/**
* @param string|null $apiPin
* @return $this
*/
public function setApiPin(?string $apiPin): self
{
$apiPin = $apiPin ? $this->openssl->encrypt($apiPin) : null;
$this->apiPin = $apiPin;
return $this;
}
/**
* @param string|null $apiToken
* @return $this
*/
public function setApiToken(?string $apiToken): self
{
$this->apiToken = $apiToken;
return $this;
}
/**
* @param DateTime|null $apiTokenValidity
* @return $this
*/
public function setApiTokenValidity(?DateTime $apiTokenValidity): self
{
$this->apiTokenValidity = $apiTokenValidity;
return $this;
}
/**
* @param array $config
* @return $this
*/
public function setConfig(array $config): self
{
$this->config = $config;
return $this;
}
/**
* @Groups({"get"})
* @return array
*/
public function getRoles(): array
{
if ($this->profile) {
return $this->profile->getRoles();
}
return [];
}
/**
* @return Profile|null
*/
public function getProfile(): ?Profile
{
return $this->profile;
}
/**
* @param Profile|null $profile
* @return $this
*/
public function setProfile(?Profile $profile): self
{
$this->profile = $profile;
return $this;
}
/**
* @return string|null
*/
public function getEmailSignature(): ?string
{
return $this->emailSignature;
}
/**
* @param string|null $emailSignature
* @return User
*/
public function setEmailSignature(?string $emailSignature): User
{
$this->emailSignature = $emailSignature;
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;
}
/**
* @return Collection|UserLoginLog[]
*/
public function getUserLoginLogs(): Collection
{
return $this->userLoginLogs;
}
/**
* @param UserLoginLog $userLoginLog
* @return $this
*/
public function addUserLoginLog(UserLoginLog $userLoginLog): self
{
if (!$this->userLoginLogs->contains($userLoginLog)) {
$this->userLoginLogs[] = $userLoginLog;
$userLoginLog->setUser($this);
}
return $this;
}
/**
* @param UserLoginLog $userLoginLog
* @return $this
*/
public function removeUserLoginLog(UserLoginLog $userLoginLog): self
{
if ($this->userLoginLogs->removeElement($userLoginLog)) {
if ($userLoginLog->getUser() === $this) {
$userLoginLog->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Notification[]
*/
public function getNotifications(): Collection
{
return $this->notifications;
}
/**
* @param Notification $notification
* @return $this
*/
public function addNotification(Notification $notification): self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUser($this);
}
return $this;
}
/**
* @param Notification $notification
* @return $this
*/
public function removeNotification(Notification $notification): self
{
if ($this->notifications->removeElement($notification)) {
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserContext[]
*/
public function getUserContexts(): Collection
{
return $this->userContexts;
}
/**
* @Groups({"get"})
* @return string
*/
public function getLocale(): string
{
return 'en';
}
/**
* @return Language|null
*/
public function getLanguage(): ?Language
{
return $this->language;
}
/**
* @param Language|null $language
* @return $this
*/
public function setLanguage(?Language $language): self
{
$this->language = $language;
return $this;
}
/**
* @return string|null
*/
public function getNote(): ?string
{
$note = $this->note;
if ($note) {
$note = $this->openssl->decrypt($note);
}
return $note;
}
/**
* @param string|null $note
* @return $this
*/
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
/**
* @return bool|null
*/
public function getIsSolo(): ?bool
{
return $this->isSolo;
}
/**
* @param bool $isSolo
* @return $this
*/
public function setIsSolo(bool $isSolo): self
{
$this->isSolo = $isSolo;
return $this;
}
/**
* @Groups({"get", "userSearch"})
* @return string
*/
public function getName(): string
{
return $this->firstname . ' ' . $this->lastname;
}
/**
* @return Collection|MailServer[]
*/
public function getMailServers(): Collection
{
return $this->mail_servers;
}
/**
* @return Collection|TeamUser[]
*/
public function getTeamUsers(): Collection
{
return $this->teamUsers;
}
/**
* @Groups({"get"})
* @return string
*/
public function getProfileName(): string
{
return $this->profile->getName();
}
/**
* @Groups({"get"})
* @return int
*/
public function getLimitPerPage(): int
{
$userConfig = $this->getConfig();
if (!isset($userConfig['core']['list']['limit']) || !$userConfig['core']['list']['limit']) {
$limit = 25;
} else {
$limit = (int) $userConfig['core']['list']['limit'];
}
return $limit;
}
/**
* @return Collection<int, UsersEstablishments>
*/
public function getUsersEstablishments(): Collection
{
return $this->usersEstablishments;
}
public function addUsersEstablishment(UsersEstablishments $usersEstablishment): self
{
if (!$this->usersEstablishments->contains($usersEstablishment)) {
$this->usersEstablishments[] = $usersEstablishment;
$usersEstablishment->setUser($this);
}
return $this;
}
public function removeUsersEstablishment(UsersEstablishments $usersEstablishment): self
{
if ($this->usersEstablishments->removeElement($usersEstablishment)) {
// set the owning side to null (unless already changed)
if ($usersEstablishment->getUser() === $this) {
$usersEstablishment->setUser(null);
}
}
return $this;
}
}