241 lines
4.6 KiB
PHP
241 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Model\Data;
|
|
|
|
use Phalcon\Mvc\Model;
|
|
use InvalidArgumentException;
|
|
|
|
class User extends Model
|
|
{
|
|
const STATUS_ACTIVE = 'Active';
|
|
const STATUS_DELETED = 'Deleted';
|
|
const STATUS_SUSPENDED = 'Suspended';
|
|
|
|
protected $id;
|
|
|
|
protected $username;
|
|
|
|
protected $name;
|
|
|
|
protected $email;
|
|
|
|
protected $status;
|
|
|
|
protected $password;
|
|
|
|
protected $github_id;
|
|
|
|
protected $github_user;
|
|
|
|
public function initialize()
|
|
{
|
|
$this->useDynamicUpdate(true);
|
|
|
|
// Keep snapshots so we know if something changes.
|
|
$this->keepSnapshots(true);
|
|
|
|
// Set default event manager
|
|
$this->setEventsManager($this->getDI()->get('eventsManager'));
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $id
|
|
* @return User
|
|
*/
|
|
public function setId($id)
|
|
{
|
|
$this->id = $id;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getUsername()
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $username
|
|
* @return User
|
|
*/
|
|
public function setUsername($username)
|
|
{
|
|
$this->username = $username;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $name
|
|
* @return User
|
|
*/
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getEmail()
|
|
{
|
|
return $this->email;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $email
|
|
* @return User
|
|
*/
|
|
public function setEmail($email)
|
|
{
|
|
$this->email = $email;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getStatus()
|
|
{
|
|
return $this->status;
|
|
}
|
|
|
|
/**
|
|
* @param string $value
|
|
* @return User
|
|
*/
|
|
public function setStatus($value)
|
|
{
|
|
$allowed_values = array(
|
|
self::STATUS_ACTIVE,
|
|
self::STATUS_DELETED,
|
|
self::STATUS_SUSPENDED
|
|
);
|
|
|
|
if (!in_array($value, $allowed_values)) {
|
|
$msg = "Status '{$value}' is not a valid enum value'";
|
|
throw new InvalidArgumentException($msg);
|
|
}
|
|
|
|
$this->status = $value;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getPassword()
|
|
{
|
|
return $this->password;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $password
|
|
* @return User
|
|
*/
|
|
public function setPassword($password)
|
|
{
|
|
$this->password = $password;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getGithubUser()
|
|
{
|
|
return $this->github_user;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $github_user
|
|
* @return User
|
|
*/
|
|
public function setGithubUser($github_user)
|
|
{
|
|
$this->github_user = $github_user;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getGithubId()
|
|
{
|
|
return $this->github_id;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $github_id
|
|
* @return User
|
|
*/
|
|
public function setGithubId($github_id)
|
|
{
|
|
$this->github_id = $github_id;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Find the first user by Username or Email
|
|
*
|
|
* @param string $value
|
|
* @return User|bool
|
|
*/
|
|
static public function findFirstByUsernameOrEmail($value)
|
|
{
|
|
return self::findFirst([
|
|
"email = :v: OR username = :v:",
|
|
"bind" => [ 'v' => $value ]
|
|
]);
|
|
}
|
|
|
|
public function beforeSave()
|
|
{
|
|
// Fire event on password create/changed.
|
|
$manager = $this->getEventsManager();
|
|
|
|
// EventManager exist
|
|
if ($manager) {
|
|
|
|
// If we have Snapshot data (existing row)
|
|
if ($this->hasSnapshotData()) {
|
|
$has_new_value = $this->hasChanged('password');
|
|
$old_value = $this->getOldSnapshotData()['password'];
|
|
}
|
|
// New row, check if we have a password.
|
|
else {
|
|
$has_new_value = strlen($this->password) > 0;
|
|
$old_value = null;
|
|
}
|
|
|
|
// we have a new password.
|
|
if ($has_new_value) {
|
|
|
|
// Empty password before
|
|
if (strlen($old_value) < 1) {
|
|
$manager->fire('user:onPasswordCreated', $this);
|
|
} else {
|
|
$manager->fire('user:onPasswordChanged', $this);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|