2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
|
|
|
namespace Entity;
|
|
|
|
|
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;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
|
|
|
/**
|
2017-12-14 10:58:45 +00:00
|
|
|
* @Table(name="users", uniqueConstraints={@UniqueConstraint(name="email_idx", columns={"email"})})
|
2017-01-17 08:46:43 +00:00
|
|
|
* @Entity(repositoryClass="Entity\Repository\UserRepository")
|
2016-10-06 06:03:00 +00:00
|
|
|
* @HasLifecycleCallbacks
|
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
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="uid", type="integer")
|
|
|
|
* @Id
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="email", type="string", length=100, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $email;
|
|
|
|
|
|
|
|
/**
|
2017-08-29 03:44:51 +00:00
|
|
|
* @Column(name="auth_password", type="string", length=255, nullable=true)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $auth_password;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="name", type="string", length=100, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="timezone", type="string", length=100, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $timezone;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="locale", type="string", length=25, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="theme", type="string", length=25, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $theme;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="created_at", type="integer")
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $created_at;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="updated_at", type="integer")
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $updated_at;
|
|
|
|
|
|
|
|
/**
|
2018-02-06 17:55:58 +00:00
|
|
|
* @ManyToMany(targetEntity="Role", inversedBy="users", fetch="EAGER")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @JoinTable(name="user_has_role",
|
|
|
|
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="role_id", referencedColumnName="id", onDelete="CASCADE")}
|
|
|
|
* )
|
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
protected $roles;
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
|
|
|
* @OneToMany(targetEntity="ApiKey", mappedBy="user")
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PrePersist
|
|
|
|
*/
|
|
|
|
public function preSave()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function getEmail()
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $email
|
|
|
|
*/
|
|
|
|
public function setEmail($email)
|
2016-09-27 20:39:54 +00:00
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
$this->email = $email;
|
2016-09-27 20:39:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthPassword(): string
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $password
|
|
|
|
*/
|
|
|
|
public function setAuthPassword(string $password)
|
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
|
|
|
/**
|
|
|
|
* @param $password
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function verifyPassword($password)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-12-13 06:10:37 +00:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2014-02-21 09:25:10 +00:00
|
|
|
public function generateRandomPassword()
|
|
|
|
{
|
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]
|
|
|
|
*/
|
|
|
|
protected function _getPasswordAlgorithm()
|
|
|
|
{
|
|
|
|
if (defined('PASSWORD_ARGON2I')) {
|
|
|
|
return [\PASSWORD_ARGON2I, []];
|
|
|
|
} else {
|
|
|
|
return [\PASSWORD_BCRYPT, []];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $name
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getTimezone()
|
|
|
|
{
|
|
|
|
return $this->timezone;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $timezone
|
|
|
|
*/
|
|
|
|
public function setTimezone($timezone)
|
|
|
|
{
|
|
|
|
$this->timezone = $timezone;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getLocale()
|
|
|
|
{
|
|
|
|
return $this->locale;
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @param null|string $locale
|
|
|
|
*/
|
|
|
|
public function setLocale($locale)
|
|
|
|
{
|
|
|
|
$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
|
|
|
*/
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getTheme()
|
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $theme
|
|
|
|
*/
|
|
|
|
public function setTheme($theme)
|
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getAvatar($size = 50)
|
|
|
|
{
|
|
|
|
return \App\Service\Gravatar::get($this->email, $size, 'identicon');
|
|
|
|
}
|
2016-09-28 03:28:49 +00:00
|
|
|
}
|