2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
|
|
|
namespace Entity;
|
|
|
|
|
|
|
|
use \Doctrine\ORM\Mapping as ORM;
|
|
|
|
use \Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Table(name="users")
|
2016-09-28 03:28:49 +00:00
|
|
|
* @Entity(repositoryClass="UserRepository")
|
2014-02-21 09:25:10 +00:00
|
|
|
*/
|
2016-05-02 08:18:32 +00:00
|
|
|
class User extends \App\Doctrine\Entity
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->roles = new ArrayCollection;
|
2015-05-22 07:26:49 +00:00
|
|
|
$this->stations = new ArrayCollection;
|
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
$this->time_created = time();
|
|
|
|
$this->time_updated = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="uid", type="integer")
|
|
|
|
* @Id
|
|
|
|
* @GeneratedValue(strategy="AUTO")
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/** @Column(name="email", type="string", length=100, nullable=true) */
|
|
|
|
protected $email;
|
|
|
|
|
2014-08-05 03:49:54 +00:00
|
|
|
public function getAvatar($size = 50)
|
|
|
|
{
|
2016-05-02 08:18:32 +00:00
|
|
|
return \App\Service\Gravatar::get($this->email, $size, 'identicon');
|
2014-08-05 03:49:54 +00:00
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
|
|
|
|
/** @Column(name="auth_password", type="string", length=255, nullable=true) */
|
|
|
|
protected $auth_password;
|
|
|
|
|
2016-09-27 20:39:54 +00:00
|
|
|
public function verifyPassword($password)
|
|
|
|
{
|
|
|
|
return password_verify($password, $this->auth_password);
|
|
|
|
}
|
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
public function getAuthPassword()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAuthPassword($password)
|
|
|
|
{
|
|
|
|
if (trim($password))
|
2015-07-22 15:05:16 +00:00
|
|
|
$this->auth_password = password_hash($password, \PASSWORD_DEFAULT);
|
2015-07-22 18:04:10 +00:00
|
|
|
|
|
|
|
return $this;
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function generateRandomPassword()
|
|
|
|
{
|
2016-05-04 00:03:28 +00:00
|
|
|
$this->setAuthPassword(md5('APP_EXTERNAL_'.mt_rand(0, 10000)));
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @Column(name="auth_last_login_time", type="integer", nullable=true) */
|
|
|
|
protected $auth_last_login_time;
|
|
|
|
|
|
|
|
/** @Column(name="auth_recovery_code", type="string", length=50, nullable=true) */
|
|
|
|
protected $auth_recovery_code;
|
|
|
|
|
|
|
|
public function generateAuthRecoveryCode()
|
|
|
|
{
|
|
|
|
$this->auth_recovery_code = sha1(mt_rand());
|
|
|
|
return $this->auth_recovery_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @Column(name="name", type="string", length=100, nullable=true) */
|
|
|
|
protected $name;
|
2016-05-04 00:03:28 +00:00
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
/** @Column(name="gender", type="string", length=1, nullable=true) */
|
|
|
|
protected $gender;
|
|
|
|
|
|
|
|
/** @Column(name="customization", type="json", nullable=true) */
|
|
|
|
protected $customization;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ManyToMany(targetEntity="Role", inversedBy="users")
|
|
|
|
* @JoinTable(name="user_has_role",
|
|
|
|
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="role_id", referencedColumnName="id", onDelete="CASCADE")}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
protected $roles;
|
|
|
|
|
2015-05-22 07:26:49 +00:00
|
|
|
/**
|
|
|
|
* @ManyToMany(targetEntity="Station", inversedBy="users")
|
|
|
|
* @JoinTable(name="user_manages_station",
|
|
|
|
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="station_id", referencedColumnName="id", onDelete="CASCADE")}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
protected $stations;
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
2016-09-28 03:28:49 +00:00
|
|
|
|
|
|
|
use App\Doctrine\Repository;
|
|
|
|
|
|
|
|
class UserRepository extends Repository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $username
|
|
|
|
* @param $password
|
|
|
|
* @return bool|null|object
|
|
|
|
*/
|
|
|
|
public function authenticate($username, $password)
|
|
|
|
{
|
|
|
|
$login_info = $this->findOneBy(['email' => $username]);
|
|
|
|
|
|
|
|
if (!($login_info instanceof User))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ($login_info->verifyPassword($password))
|
|
|
|
return $login_info;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates or returns an existing user with the specified e-mail address.
|
|
|
|
*
|
|
|
|
* @param $email
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function getOrCreate($email)
|
|
|
|
{
|
|
|
|
$user = $this->findOneBy(['email' => $email]);
|
|
|
|
|
|
|
|
if (!($user instanceof User))
|
|
|
|
{
|
|
|
|
$user = new User;
|
|
|
|
$user->email = $email;
|
|
|
|
$user->name = $email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|