2013-05-24 10:14:49 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\models;
|
|
|
|
|
2013-09-24 15:45:09 +04:00
|
|
|
class User extends \yii\base\Object implements \yii\web\IdentityInterface
|
2013-05-24 10:14:49 -04:00
|
|
|
{
|
2014-03-16 10:46:16 +06:00
|
|
|
public $id;
|
|
|
|
public $username;
|
|
|
|
public $password;
|
|
|
|
public $authKey;
|
|
|
|
public $accessToken;
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
private static $users = [
|
|
|
|
'100' => [
|
|
|
|
'id' => '100',
|
|
|
|
'username' => 'admin',
|
|
|
|
'password' => 'admin',
|
|
|
|
'authKey' => 'test100key',
|
|
|
|
'accessToken' => '100-token',
|
|
|
|
],
|
|
|
|
'101' => [
|
|
|
|
'id' => '101',
|
|
|
|
'username' => 'demo',
|
|
|
|
'password' => 'demo',
|
|
|
|
'authKey' => 'test101key',
|
|
|
|
'accessToken' => '101-token',
|
|
|
|
],
|
|
|
|
];
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public static function findIdentity($id)
|
|
|
|
{
|
|
|
|
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2014-05-06 18:45:18 -04:00
|
|
|
public static function findIdentityByAccessToken($token, $type = null)
|
2014-03-16 10:46:16 +06:00
|
|
|
{
|
|
|
|
foreach (self::$users as $user) {
|
|
|
|
if ($user['accessToken'] === $token) {
|
|
|
|
return new static($user);
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 23:14:16 -05:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
return null;
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
|
|
|
* Finds user by username
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @return static|null
|
|
|
|
*/
|
|
|
|
public static function findByUsername($username)
|
|
|
|
{
|
|
|
|
foreach (self::$users as $user) {
|
|
|
|
if (strcasecmp($user['username'], $username) === 0) {
|
|
|
|
return new static($user);
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
return null;
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-03-16 10:46:16 +06:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getAuthKey()
|
|
|
|
{
|
|
|
|
return $this->authKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function validateAuthKey($authKey)
|
|
|
|
{
|
|
|
|
return $this->authKey === $authKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates password
|
|
|
|
*
|
|
|
|
* @param string $password password to validate
|
|
|
|
* @return boolean if password provided is valid for current user
|
|
|
|
*/
|
|
|
|
public function validatePassword($password)
|
|
|
|
{
|
|
|
|
return $this->password === $password;
|
|
|
|
}
|
2013-05-24 10:14:49 -04:00
|
|
|
}
|