2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
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;
|
2019-08-07 04:33:55 +00:00
|
|
|
use Azura\Normalizer\Annotation\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;
|
2019-08-14 23:50:53 +00:00
|
|
|
use App\Annotations\AuditLog;
|
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;
|
|
|
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
2018-12-25 05:03:10 +00:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
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"})})
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Entity(repositoryClass="App\Entity\Repository\UserRepository")
|
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
|
|
|
|
* @ORM\GeneratedValue(strategy="AUTO")
|
2018-12-22 00:01:04 +00:00
|
|
|
*
|
|
|
|
* @OA\Property(example=1)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
*
|
2018-12-22 17:27:41 +00:00
|
|
|
* @OA\Property(example="")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $auth_password;
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
|
|
$this->roles = new ArrayCollection;
|
2018-02-06 11:09:05 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-01-24 00:17:50 +00:00
|
|
|
/**
|
2017-08-17 18:28:48 +00:00
|
|
|
* @return int
|
2017-01-24 00:17:50 +00:00
|
|
|
*/
|
2017-08-17 18:28:48 +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
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function getEmail(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $email
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setEmail($email): void
|
2016-09-27 20:39:54 +00:00
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->email = $this->_truncateString($email, 100);
|
2016-09-27 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 23:50:53 +00:00
|
|
|
/**
|
|
|
|
* @AuditLog\AuditIdentifier()
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getIdentifier(): string
|
|
|
|
{
|
|
|
|
return $this->getName().' ('.$this->getEmail().')';
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $password
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setAuthPassword(string $password): void
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2017-01-24 00:35:16 +00:00
|
|
|
if (trim($password)) {
|
2017-12-13 06:10:37 +00:00
|
|
|
[$algo, $algo_opts] = $this->_getPasswordAlgorithm();
|
|
|
|
$this->auth_password = password_hash($password, $algo, $algo_opts);
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2015-07-22 18:04:10 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2019-01-31 17:54:17 +00:00
|
|
|
* @param string $password
|
2017-08-17 18:28:48 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function verifyPassword($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)) {
|
|
|
|
[$algo, $algo_opts] = $this->_getPasswordAlgorithm();
|
|
|
|
|
|
|
|
if (password_needs_rehash($this->auth_password, $algo, $algo_opts)) {
|
|
|
|
$this->setAuthPassword($password);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:54:17 +00:00
|
|
|
public function generateRandomPassword(): void
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
$this->setAuthPassword(bin2hex(random_bytes(20)));
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-12-13 06:10:37 +00:00
|
|
|
/**
|
|
|
|
* Get the most secure available password hashing algorithm.
|
|
|
|
*
|
|
|
|
* @return array [algorithm constant, algorithm options array]
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
protected function _getPasswordAlgorithm(): array
|
2017-12-13 06:10:37 +00:00
|
|
|
{
|
|
|
|
if (defined('PASSWORD_ARGON2I')) {
|
|
|
|
return [\PASSWORD_ARGON2I, []];
|
|
|
|
}
|
2019-01-31 17:54:17 +00:00
|
|
|
|
|
|
|
return [\PASSWORD_BCRYPT, []];
|
2017-12-13 06:10:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function getName(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $name
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setName($name): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->name = $this->_truncateString($name, 100);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
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
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $locale
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setLocale($locale): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
$this->locale = $locale;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-17 18:28:48 +00:00
|
|
|
* @return null|string
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $theme
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setTheme($theme): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:40:02 +00:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getTwoFactorSecret(): ?string
|
|
|
|
{
|
|
|
|
return $this->two_factor_secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|null $two_factor_secret
|
|
|
|
*/
|
|
|
|
public function setTwoFactorSecret(?string $two_factor_secret): void
|
|
|
|
{
|
|
|
|
$this->two_factor_secret = $two_factor_secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $otp
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function verifyTwoFactor(string $otp): bool
|
|
|
|
{
|
|
|
|
if (null === $this->two_factor_secret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totp = \OTPHP\Factory::loadFromProvisioningUri($this->two_factor_secret);
|
|
|
|
return $totp->verify($otp, null, Auth::TOTP_WINDOW);
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCreatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->created_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getUpdatedAt(): int
|
|
|
|
{
|
|
|
|
return $this->updated_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getRoles(): Collection
|
|
|
|
{
|
|
|
|
return $this->roles;
|
|
|
|
}
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getApiKeys(): Collection
|
|
|
|
{
|
|
|
|
return $this->api_keys;
|
|
|
|
}
|
|
|
|
|
2019-01-31 17:54:17 +00:00
|
|
|
/**
|
|
|
|
* @param int $size
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAvatar($size = 50): string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2019-05-27 14:05:43 +00:00
|
|
|
return \App\Service\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
|
|
|
}
|