2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
2020-12-08 18:40:33 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Entity;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
2019-03-14 08:40:02 +00:00
|
|
|
use App\Auth;
|
2021-11-03 01:38:45 +00:00
|
|
|
use App\Entity\Interfaces\EntityGroupsInterface;
|
2021-07-19 05:53:45 +00:00
|
|
|
use App\Entity\Interfaces\IdentifiableEntityInterface;
|
2022-01-07 08:26:40 +00:00
|
|
|
use App\Enums\SupportedThemes;
|
2021-12-29 21:19:34 +00:00
|
|
|
use App\OpenApi;
|
2021-07-19 05:53:45 +00:00
|
|
|
use App\Utilities\Strings;
|
2021-04-21 05:27:05 +00:00
|
|
|
use App\Validator\Constraints\UniqueEntity;
|
2021-11-07 08:20:52 +00:00
|
|
|
use Azura\Normalizer\Attributes\DeepNormalize;
|
2017-01-24 00:35:16 +00:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2017-08-17 18:28:48 +00:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2017-01-24 00:35:16 +00:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2021-12-28 18:26:41 +00:00
|
|
|
use OpenApi\Attributes as OA;
|
2019-09-04 18:00:51 +00:00
|
|
|
use OTPHP\Factory;
|
2021-05-30 18:55:26 +00:00
|
|
|
use Stringable;
|
2018-12-22 17:27:41 +00:00
|
|
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
2021-11-03 01:38:45 +00:00
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2018-12-25 05:03:10 +00:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
use const PASSWORD_BCRYPT;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
2021-05-30 18:55:26 +00:00
|
|
|
#[
|
2021-12-19 18:50:37 +00:00
|
|
|
OA\Schema(type: "object"),
|
2021-05-30 18:55:26 +00:00
|
|
|
ORM\Entity,
|
|
|
|
ORM\Table(name: 'users'),
|
|
|
|
ORM\HasLifecycleCallbacks,
|
|
|
|
ORM\UniqueConstraint(name: 'email_idx', columns: ['email']),
|
2021-05-31 01:15:34 +00:00
|
|
|
Attributes\Auditable,
|
2021-05-30 18:55:26 +00:00
|
|
|
UniqueEntity(fields: ['email'])
|
|
|
|
]
|
2021-07-19 05:53:45 +00:00
|
|
|
class User implements Stringable, IdentifiableEntityInterface
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2021-05-30 18:55:26 +00:00
|
|
|
use Traits\HasAutoIncrementId;
|
2018-04-06 21:29:27 +00:00
|
|
|
use Traits\TruncateStrings;
|
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: "demo@azuracast.com"),
|
|
|
|
ORM\Column(length: 100, nullable: false),
|
|
|
|
Assert\NotBlank,
|
|
|
|
Assert\Email,
|
|
|
|
Groups([EntityGroupsInterface::GROUP_GENERAL, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-07-19 05:53:45 +00:00
|
|
|
protected string $email;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
ORM\Column(length: 255, nullable: false),
|
|
|
|
Attributes\AuditIgnore
|
|
|
|
]
|
2021-07-19 05:53:45 +00:00
|
|
|
protected string $auth_password = '';
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: ""),
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected ?string $new_password = null;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: "Demo Account"),
|
|
|
|
ORM\Column(length: 100, nullable: true),
|
|
|
|
Groups([EntityGroupsInterface::GROUP_GENERAL, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected ?string $name = null;
|
2019-11-21 02:06:38 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: "en_US"),
|
|
|
|
ORM\Column(length: 25, nullable: true),
|
|
|
|
Groups([EntityGroupsInterface::GROUP_GENERAL, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected ?string $locale = null;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: "dark"),
|
|
|
|
ORM\Column(length: 25, nullable: true),
|
|
|
|
Attributes\AuditIgnore,
|
|
|
|
Groups([EntityGroupsInterface::GROUP_GENERAL, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected ?string $theme = null;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(example: "A1B2C3D4"),
|
|
|
|
ORM\Column(length: 255, nullable: true),
|
|
|
|
Attributes\AuditIgnore,
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected ?string $two_factor_secret = null;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
2021-12-29 21:19:34 +00:00
|
|
|
OA\Property(example: OpenApi::SAMPLE_TIMESTAMP),
|
2021-12-19 18:50:37 +00:00
|
|
|
ORM\Column,
|
|
|
|
Attributes\AuditIgnore,
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected int $created_at;
|
2019-03-14 08:40:02 +00:00
|
|
|
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
2021-12-29 21:19:34 +00:00
|
|
|
OA\Property(example: OpenApi::SAMPLE_TIMESTAMP),
|
2021-12-19 18:50:37 +00:00
|
|
|
ORM\Column,
|
|
|
|
Attributes\AuditIgnore,
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL])
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected int $updated_at;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2022-05-31 11:41:35 +00:00
|
|
|
/** @var Collection<int, Role> */
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
OA\Property(type: "array", items: new OA\Items()),
|
|
|
|
ORM\ManyToMany(targetEntity: Role::class, inversedBy: 'users', fetch: 'EAGER'),
|
|
|
|
ORM\JoinTable(name: 'user_has_role'),
|
|
|
|
ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE'),
|
|
|
|
ORM\InverseJoinColumn(name: 'role_id', referencedColumnName: 'id', onDelete: 'CASCADE'),
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL]),
|
|
|
|
DeepNormalize(true),
|
|
|
|
Serializer\MaxDepth(1)
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected Collection $roles;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2022-05-31 11:41:35 +00:00
|
|
|
/** @var Collection<int, ApiKey> */
|
2021-12-19 18:50:37 +00:00
|
|
|
#[
|
|
|
|
ORM\OneToMany(mappedBy: 'user', targetEntity: ApiKey::class),
|
|
|
|
Groups([EntityGroupsInterface::GROUP_ADMIN, EntityGroupsInterface::GROUP_ALL]),
|
|
|
|
DeepNormalize(true)
|
|
|
|
]
|
2021-05-30 18:55:26 +00:00
|
|
|
protected Collection $api_keys;
|
2018-02-06 11:09:05 +00:00
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-10-06 06:03:00 +00:00
|
|
|
$this->created_at = time();
|
|
|
|
$this->updated_at = time();
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2020-10-14 22:19:31 +00:00
|
|
|
$this->roles = new ArrayCollection();
|
|
|
|
$this->api_keys = new ArrayCollection();
|
2016-10-06 06:03:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 18:55:26 +00:00
|
|
|
#[ORM\PreUpdate]
|
2019-08-12 05:03:00 +00:00
|
|
|
public function preUpdate(): void
|
2016-10-06 06:03:00 +00:00
|
|
|
{
|
|
|
|
$this->updated_at = time();
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
public function getName(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2019-09-04 18:00:51 +00:00
|
|
|
return $this->name;
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function setName(?string $name = null): void
|
2016-09-27 20:39:54 +00:00
|
|
|
{
|
2021-05-30 18:55:26 +00:00
|
|
|
$this->name = $this->truncateNullableString($name, 100);
|
2016-09-27 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function getEmail(): string
|
2019-08-14 23:50:53 +00:00
|
|
|
{
|
2019-09-04 18:00:51 +00:00
|
|
|
return $this->email;
|
2019-08-14 23:50:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function setEmail(string $email): void
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
$this->email = $this->truncateString($email, 100);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2015-07-22 18:04:10 +00:00
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function verifyPassword(string $password): bool
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2017-12-13 06:10:37 +00:00
|
|
|
if (password_verify($password, $this->auth_password)) {
|
2020-03-29 07:16:41 +00:00
|
|
|
[$algo, $algo_opts] = $this->getPasswordAlgorithm();
|
2017-12-13 06:10:37 +00:00
|
|
|
|
|
|
|
if (password_needs_rehash($this->auth_password, $algo, $algo_opts)) {
|
2019-10-26 23:35:24 +00:00
|
|
|
$this->setNewPassword($password);
|
2017-12-13 06:10:37 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 06:10:37 +00:00
|
|
|
/**
|
|
|
|
* Get the most secure available password hashing algorithm.
|
|
|
|
*
|
2020-10-14 22:19:31 +00:00
|
|
|
* @return mixed[] [algorithm constant string, algorithm options array]
|
2017-12-13 06:10:37 +00:00
|
|
|
*/
|
2020-03-29 07:16:41 +00:00
|
|
|
protected function getPasswordAlgorithm(): array
|
2017-12-13 06:10:37 +00:00
|
|
|
{
|
2020-01-05 21:29:56 +00:00
|
|
|
if (defined('PASSWORD_ARGON2ID')) {
|
|
|
|
return [PASSWORD_ARGON2ID, []];
|
2017-12-13 06:10:37 +00:00
|
|
|
}
|
2019-01-31 17:54:17 +00:00
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
return [PASSWORD_BCRYPT, []];
|
2017-12-13 06:10:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
public function setNewPassword(?string $password): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
if (null !== $password && trim($password)) {
|
2020-03-29 07:16:41 +00:00
|
|
|
[$algo, $algo_opts] = $this->getPasswordAlgorithm();
|
2019-09-04 18:00:51 +00:00
|
|
|
$this->auth_password = password_hash($password, $algo, $algo_opts);
|
|
|
|
}
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
public function generateRandomPassword(): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2021-07-19 05:53:45 +00:00
|
|
|
$this->setNewPassword(Strings::generatePassword());
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2019-01-31 17:54:17 +00:00
|
|
|
public function getLocale(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
return $this->locale;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function setLocale(?string $locale = null): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
$this->locale = $locale;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2019-01-31 17:54:17 +00:00
|
|
|
public function getTheme(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
2022-01-07 08:26:40 +00:00
|
|
|
public function getThemeEnum(): SupportedThemes
|
|
|
|
{
|
|
|
|
if (null !== $this->theme) {
|
|
|
|
$theme = SupportedThemes::tryFrom($this->theme);
|
|
|
|
if (null !== $theme) {
|
|
|
|
return $theme;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SupportedThemes::default();
|
|
|
|
}
|
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function setTheme(?string $theme = null): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:40:02 +00:00
|
|
|
public function getTwoFactorSecret(): ?string
|
|
|
|
{
|
|
|
|
return $this->two_factor_secret;
|
|
|
|
}
|
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function setTwoFactorSecret(?string $two_factor_secret = null): void
|
2019-03-14 08:40:02 +00:00
|
|
|
{
|
|
|
|
$this->two_factor_secret = $two_factor_secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verifyTwoFactor(string $otp): bool
|
|
|
|
{
|
|
|
|
if (null === $this->two_factor_secret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-08 06:40:49 +00:00
|
|
|
return Factory::loadFromProvisioningUri($this->two_factor_secret)->verify($otp, null, Auth::TOTP_WINDOW);
|
2019-03-14 08:40:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getCreatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->created_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUpdatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->updated_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-31 11:41:35 +00:00
|
|
|
* @return Collection<int, Role>
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
|
|
|
public function getRoles(): Collection
|
|
|
|
{
|
|
|
|
return $this->roles;
|
|
|
|
}
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
2022-05-31 11:41:35 +00:00
|
|
|
* @return Collection<int, ApiKey>
|
2018-02-06 11:09:05 +00:00
|
|
|
*/
|
|
|
|
public function getApiKeys(): Collection
|
|
|
|
{
|
|
|
|
return $this->api_keys;
|
|
|
|
}
|
2021-05-30 18:55:26 +00:00
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->getName() . ' (' . $this->getEmail() . ')';
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|