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")
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class User extends \DF\Doctrine\Entity
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->roles = new ArrayCollection;
|
2014-08-04 12:46:14 +00:00
|
|
|
$this->external_accounts = new ArrayCollection;
|
|
|
|
|
2015-05-22 07:26:49 +00:00
|
|
|
$this->stations = new ArrayCollection;
|
|
|
|
$this->podcasts = 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)
|
|
|
|
{
|
|
|
|
return \DF\Service\Gravatar::get($this->email, $size, 'identicon');
|
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
|
|
|
|
/** @Column(name="auth_password", type="string", length=255, nullable=true) */
|
|
|
|
protected $auth_password;
|
|
|
|
|
|
|
|
/** @Column(name="auth_password_salt", type="string", length=255, nullable=true) */
|
|
|
|
protected $auth_password_salt;
|
|
|
|
|
|
|
|
/** @Column(name="auth_external_provider", type="string", length=255, nullable=true) */
|
|
|
|
protected $auth_external_provider;
|
|
|
|
|
|
|
|
/** @Column(name="auth_external_id", type="string", length=255, nullable=true) */
|
|
|
|
protected $auth_external_id;
|
|
|
|
|
|
|
|
public function getAuthPassword()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAuthPassword($password)
|
|
|
|
{
|
|
|
|
if (trim($password))
|
|
|
|
{
|
2015-07-22 15:05:16 +00:00
|
|
|
$this->auth_password_salt = 'PHP';
|
|
|
|
$this->auth_password = password_hash($password, \PASSWORD_DEFAULT);
|
|
|
|
|
|
|
|
// $this->auth_password_salt = sha1(mt_rand());
|
|
|
|
// $this->auth_password = sha1($password.$this->auth_password_salt);
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateRandomPassword()
|
|
|
|
{
|
|
|
|
$this->setAuthPassword(md5('PVL_EXTERNAL_'.mt_rand(0, 10000)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
2014-08-10 18:14:00 +00:00
|
|
|
/** @Column(name="legal_name", type="string", length=100, nullable=true) */
|
|
|
|
protected $legal_name;
|
|
|
|
|
|
|
|
/** @Column(name="pony_name", type="string", length=100, nullable=true) */
|
|
|
|
protected $pony_name;
|
|
|
|
|
|
|
|
/** @Column(name="phone", type="string", length=50, nullable=true) */
|
|
|
|
protected $phone;
|
|
|
|
|
|
|
|
/** @Column(name="pvl_affiliation", type="string", length=50, nullable=true) */
|
|
|
|
protected $pvl_affiliation;
|
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
/** @Column(name="title", type="string", length=100, nullable=true) */
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ManyToMany(targetEntity="Podcast", inversedBy="managers")
|
|
|
|
* @JoinTable(name="user_manages_podcast",
|
|
|
|
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="podcast_id", referencedColumnName="id", onDelete="CASCADE")}
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
protected $podcasts;
|
|
|
|
|
2014-08-04 12:46:14 +00:00
|
|
|
/**
|
2014-08-04 12:47:09 +00:00
|
|
|
* @OneToMany(targetEntity="UserExternal", mappedBy="user")
|
2014-08-04 12:46:14 +00:00
|
|
|
*/
|
|
|
|
protected $external_accounts;
|
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
/**
|
|
|
|
* Static Functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static function authenticate($username, $password)
|
|
|
|
{
|
|
|
|
$login_info = self::getRepository()->findOneBy(array('email' => $username));
|
|
|
|
|
|
|
|
if (!($login_info instanceof self))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-07-22 15:05:16 +00:00
|
|
|
if ($login_info->auth_password_salt === 'PHP')
|
|
|
|
{
|
|
|
|
if (password_verify($password, $login_info->auth_password))
|
|
|
|
return $login_info;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$hashed_password = sha1($password . $login_info->auth_password_salt);
|
2014-02-21 09:25:10 +00:00
|
|
|
|
2015-07-22 15:05:16 +00:00
|
|
|
if (strcasecmp($hashed_password, $login_info->auth_password) == 0)
|
|
|
|
return $login_info;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|
2015-03-12 01:05:47 +00:00
|
|
|
|
2015-05-22 07:26:49 +00:00
|
|
|
/**
|
|
|
|
* Creates or returns an existing user with the specified e-mail address.
|
|
|
|
*
|
|
|
|
* @param $email
|
|
|
|
* @return User
|
|
|
|
*/
|
2015-03-12 01:05:47 +00:00
|
|
|
public static function getOrCreate($email)
|
|
|
|
{
|
|
|
|
$user = User::getRepository()->findOneBy(array('email' => $email));
|
|
|
|
|
|
|
|
if (!($user instanceof User))
|
|
|
|
{
|
|
|
|
$user = new User;
|
|
|
|
$user->email = $email;
|
|
|
|
$user->name = $email;
|
|
|
|
}
|
2015-05-22 07:26:49 +00:00
|
|
|
|
|
|
|
return $user;
|
2015-03-12 01:05:47 +00:00
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|