<?php
declare(strict_types=1);
/*
* This file is part of SolidInvoice project.
*
* (c) Pierre du Plessis <open-source@solidworx.co>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace SolidInvoice\UserBundle\Entity;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
use Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator;
use Ramsey\Uuid\UuidInterface;
use SolidInvoice\CoreBundle\Entity\Company;
use SolidInvoice\CoreBundle\Traits\Entity\TimeStampable;
use SolidInvoice\UserBundle\Repository\UserRepository;
use Stringable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Table(name: User::TABLE_NAME)]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'This email is already in use. Do you want to log in instead?')]
#[UniqueEntity(fields: ['username'], message: 'This username is already in use')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, Stringable
{
final public const TABLE_NAME = 'users';
use TimeStampable;
#[ORM\Column(type: UuidBinaryOrderedTimeType::NAME)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidOrderedTimeGenerator::class)]
private ?UuidInterface $id = null;
#[ORM\Column(name: 'mobile', type: Types::STRING, nullable: true)]
private ?string $mobile = null;
/**
* @var Collection<int, ApiToken>
*/
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ApiToken::class, cascade: ['persist', 'remove'], fetch: 'EXTRA_LAZY', orphanRemoval: true)]
private Collection $apiTokens;
#[ORM\Column(name: 'username', type: Types::STRING, length: 180, unique: true)]
private ?string $username = null;
#[ORM\Column(name: 'email', type: Types::STRING, length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column(name: 'enabled', type: Types::BOOLEAN)]
private bool $enabled = false;
#[ORM\Column(name: 'password', type: Types::STRING)]
private ?string $password = null;
private string $plainPassword = '';
#[ORM\Column(name: 'last_login', type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $lastLogin = null;
#[ORM\Column(name: 'confirmation_token', type: Types::STRING, length: 180, unique: true, nullable: true)]
private ?string $confirmationToken = null;
#[ORM\Column(name: 'password_requested_at', type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTimeInterface $passwordRequestedAt = null;
/**
* @var string[]
*/
#[ORM\Column(name: 'roles', type: 'array')]
private array $roles = [];
/**
* @var Collection<int, Company>
*/
#[ORM\ManyToMany(targetEntity: Company::class, inversedBy: 'users')]
private Collection $companies;
public function __construct()
{
$this->apiTokens = new ArrayCollection();
$this->companies = new ArrayCollection();
}
/**
* Don't return the salt, and rely on password_hash to generate a salt.
*/
public function getSalt(): ?string
{
return null;
}
/**
* @return Collection<int, ApiToken>
*/
public function getApiTokens(): Collection
{
return $this->apiTokens;
}
/**
* @param Collection<int, ApiToken> $apiTokens
*/
public function setApiTokens(Collection $apiTokens): self
{
$this->apiTokens = $apiTokens;
return $this;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function __toString(): string
{
return $this->username;
}
public function addRole(string $role): self
{
$role = strtoupper($role);
if ('ROLE_USER' === $role) {
return $this;
}
if (! in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
public function serialize(): string
{
return serialize([
$this->password,
$this->username,
$this->enabled,
$this->id,
$this->email,
$this->roles,
$this->mobile,
$this->created,
$this->updated,
]);
}
public function unserialize(string $serialized): void
{
[
$this->password,
$this->username,
$this->enabled,
$this->id,
$this->email,
$this->roles,
$this->created,
$this->updated
] = unserialize($serialized);
}
public function eraseCredentials(): void
{
$this->plainPassword = '';
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function getEmail(): ?string
{
return $this->email;
}
public function getPassword(): ?string
{
return $this->password;
}
public function getPlainPassword(): string
{
return $this->plainPassword;
}
public function getLastLogin(): ?DateTimeInterface
{
return $this->lastLogin;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function getRoles(): array
{
$roles = $this->roles;
// we need to make sure to have at least one role
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function hasRole(string $role): bool
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function removeRole(string $role): self
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function setPlainPassword(?string $password): self
{
$this->plainPassword = (string) $password;
return $this;
}
public function setLastLogin(?DateTimeInterface $time = null): self
{
$this->lastLogin = $time;
return $this;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function setPasswordRequestedAt(?DateTimeInterface $date = null): self
{
$this->passwordRequestedAt = $date;
return $this;
}
public function getPasswordRequestedAt(): ?DateTimeInterface
{
return $this->passwordRequestedAt;
}
public function isPasswordRequestNonExpired(int $ttl): bool
{
return $this->getPasswordRequestedAt() instanceof DateTime &&
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
}
/**
* @param string[] $roles
*/
public function setRoles(array $roles): self
{
$this->roles = [];
foreach ($roles as $role) {
$this->addRole($role);
}
return $this;
}
/**
* @return Collection<int, Company>
*/
public function getCompanies(): Collection
{
return $this->companies;
}
public function addCompany(Company $company): self
{
if (! $this->companies->contains($company)) {
$this->companies[] = $company;
}
return $this;
}
public function removeCompany(Company $company): self
{
if ($this->companies->contains($company)) {
$this->companies->removeElement($company);
}
return $this;
}
}