4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-14 21:26:37 +00:00
AzuraCast/app/models/Entity/User.php

144 lines
3.7 KiB
PHP
Raw Normal View History

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(repositoryClass="UserRepository")
2014-02-21 09:25:10 +00:00
*/
class User extends \App\Doctrine\Entity
2014-02-21 09:25:10 +00:00
{
public function __construct()
{
$this->roles = new ArrayCollection;
$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)
{
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;
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))
$this->auth_password = password_hash($password, \PASSWORD_DEFAULT);
return $this;
2014-02-21 09:25:10 +00:00
}
public function generateRandomPassword()
{
$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;
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;
/**
* @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
}
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;
}
}