2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Entity;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
use App\Annotations\AuditLog;
|
2019-03-14 08:40:02 +00:00
|
|
|
use App\Auth;
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Normalizer\Annotation\DeepNormalize;
|
2020-03-29 07:16:41 +00:00
|
|
|
use App\Service\Gravatar;
|
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;
|
2018-12-22 17:27:41 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2019-09-04 18:00:51 +00:00
|
|
|
use OTPHP\Factory;
|
2018-12-22 17:27:41 +00:00
|
|
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
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
|
|
|
|
|
|
|
/**
|
2018-12-22 00:01:04 +00:00
|
|
|
* @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="email_idx", columns={"email"})})
|
2019-09-29 07:50:24 +00:00
|
|
|
* @ORM\Entity()
|
2018-12-22 00:01:04 +00:00
|
|
|
* @ORM\HasLifecycleCallbacks
|
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\Auditable
|
|
|
|
*
|
2018-12-22 00:01:04 +00:00
|
|
|
* @OA\Schema(type="object")
|
2014-02-21 09:25:10 +00:00
|
|
|
*/
|
2017-08-17 18:28:48 +00:00
|
|
|
class User
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
use Traits\TruncateStrings;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="uid", type="integer")
|
|
|
|
* @ORM\Id
|
2020-12-05 07:39:05 +00:00
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY")
|
2018-12-22 00:01:04 +00:00
|
|
|
*
|
|
|
|
* @OA\Property(example=1)
|
2020-12-05 07:39:05 +00:00
|
|
|
* @var int|null
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="email", type="string", length=100, nullable=true)
|
2018-12-22 00:01:04 +00:00
|
|
|
*
|
2018-12-22 17:27:41 +00:00
|
|
|
* @OA\Property(example="demo@azuracast.com")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
2018-12-25 05:03:10 +00:00
|
|
|
*
|
|
|
|
* @Assert\NotBlank
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
|
|
|
protected $email;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="auth_password", type="string", length=255, nullable=true)
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIgnore()
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $auth_password;
|
|
|
|
|
2019-11-21 02:06:38 +00:00
|
|
|
/**
|
|
|
|
* @OA\Property(example="")
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $new_password;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="name", type="string", length=100, nullable=true)
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
|
|
|
* @OA\Property(example="Demo Account")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="locale", type="string", length=25, nullable=true)
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
|
|
|
* @OA\Property(example="en_US")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $locale;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="theme", type="string", length=25, nullable=true)
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2018-12-22 17:27:41 +00:00
|
|
|
* @OA\Property(example="dark")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $theme;
|
|
|
|
|
2019-03-14 08:40:02 +00:00
|
|
|
/**
|
|
|
|
* @ORM\Column(name="two_factor_secret", type="string", length=255, nullable=true)
|
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2019-03-14 08:40:02 +00:00
|
|
|
* @OA\Property(example="A1B2C3D4")
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $two_factor_secret;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="created_at", type="integer")
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
2019-08-15 19:01:00 +00:00
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2018-12-22 17:27:41 +00:00
|
|
|
* @OA\Property(example=SAMPLE_TIMESTAMP)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $created_at;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="updated_at", type="integer")
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2018-12-22 17:27:41 +00:00
|
|
|
* @OA\Property(example=SAMPLE_TIMESTAMP)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $updated_at;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", fetch="EAGER")
|
|
|
|
* @ORM\JoinTable(name="user_has_role",
|
2018-12-22 00:01:04 +00:00
|
|
|
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id", onDelete="CASCADE")}
|
2017-08-17 18:28:48 +00:00
|
|
|
* )
|
2018-12-22 17:27:41 +00:00
|
|
|
*
|
2019-03-03 04:35:03 +00:00
|
|
|
* @DeepNormalize(true)
|
2018-12-22 17:27:41 +00:00
|
|
|
* @Serializer\MaxDepth(1)
|
|
|
|
* @OA\Property(
|
|
|
|
* @OA\Items()
|
|
|
|
* )
|
|
|
|
*
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
protected $roles;
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\OneToMany(targetEntity="ApiKey", mappedBy="user")
|
2019-03-03 04:35:03 +00:00
|
|
|
* @DeepNormalize(true)
|
2018-02-06 11:09:05 +00:00
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
protected $api_keys;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-12 05:03:00 +00:00
|
|
|
* @ORM\PreUpdate
|
2016-10-06 06:03:00 +00:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2020-12-05 07:39:05 +00:00
|
|
|
public function getId(): ?int
|
2014-08-05 03:49:54 +00:00
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
return $this->id;
|
2014-08-05 03:49:54 +00:00
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
/**
|
|
|
|
* @AuditLog\AuditIdentifier()
|
|
|
|
*/
|
|
|
|
public function getIdentifier(): string
|
|
|
|
{
|
|
|
|
return $this->getName() . ' (' . $this->getEmail() . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-03-29 07:16:41 +00:00
|
|
|
$this->name = $this->truncateString($name, 100);
|
2016-09-27 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:00:51 +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
|
|
|
}
|
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function setEmail(?string $email = null): void
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2020-03-29 07:16:41 +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
|
|
|
}
|
|
|
|
|
2019-10-26 23:35:24 +00:00
|
|
|
public function setNewPassword(string $password): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2019-09-04 18:00:51 +00:00
|
|
|
if (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
|
|
|
{
|
2019-10-26 23:35:24 +00:00
|
|
|
$this->setNewPassword(bin2hex(random_bytes(20)));
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
$totp = Factory::loadFromProvisioningUri($this->two_factor_secret);
|
2019-03-14 08:40:02 +00:00
|
|
|
return $totp->verify($otp, null, Auth::TOTP_WINDOW);
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getCreatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->created_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUpdatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->updated_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-29 07:16:41 +00:00
|
|
|
* @return Collection|Role[]
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
|
|
|
public function getRoles(): Collection
|
|
|
|
{
|
|
|
|
return $this->roles;
|
|
|
|
}
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
2020-03-29 07:16:41 +00:00
|
|
|
* @return Collection|ApiKey[]
|
2018-02-06 11:09:05 +00:00
|
|
|
*/
|
|
|
|
public function getApiKeys(): Collection
|
|
|
|
{
|
|
|
|
return $this->api_keys;
|
|
|
|
}
|
2020-03-29 07:29:29 +00:00
|
|
|
|
2020-03-29 07:16:41 +00:00
|
|
|
public function getAvatar(int $size = 50): string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2019-09-04 18:00:51 +00:00
|
|
|
return Gravatar::get($this->email, $size, 'https://www.azuracast.com/img/avatar.png');
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|