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
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
public $username;
|
|
|
|
public $password;
|
|
|
|
public $authKey;
|
2014-03-01 23:14:16 -05:00
|
|
|
public $apiKey;
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2013-10-18 09:48:27 +00:00
|
|
|
private static $users = [
|
|
|
|
'100' => [
|
2013-05-24 10:14:49 -04:00
|
|
|
'id' => '100',
|
|
|
|
'username' => 'admin',
|
|
|
|
'password' => 'admin',
|
|
|
|
'authKey' => 'test100key',
|
2014-03-01 23:14:16 -05:00
|
|
|
'apiKey' => '100-apikey',
|
2013-10-18 09:48:27 +00:00
|
|
|
],
|
|
|
|
'101' => [
|
2013-05-24 10:14:49 -04:00
|
|
|
'id' => '101',
|
|
|
|
'username' => 'demo',
|
|
|
|
'password' => 'demo',
|
|
|
|
'authKey' => 'test101key',
|
2014-03-01 23:14:16 -05:00
|
|
|
'apiKey' => '101-apikey',
|
2013-10-18 09:48:27 +00:00
|
|
|
],
|
|
|
|
];
|
2013-05-24 10:14:49 -04:00
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public static function findIdentity($id)
|
|
|
|
{
|
2013-11-20 18:36:05 +01:00
|
|
|
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
|
2013-05-24 10:14:49 -04:00
|
|
|
}
|
|
|
|
|
2014-03-01 23:14:16 -05:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public static function findIdentityByToken($token)
|
|
|
|
{
|
|
|
|
foreach (self::$users as $user) {
|
|
|
|
if ($user['apiKey'] === $token) {
|
|
|
|
return new static($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* Finds user by username
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @return static|null
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public static function findByUsername($username)
|
|
|
|
{
|
|
|
|
foreach (self::$users as $user) {
|
|
|
|
if (strcasecmp($user['username'], $username) === 0) {
|
2013-11-20 18:36:05 +01:00
|
|
|
return new static($user);
|
2013-05-24 10:14:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public function getAuthKey()
|
|
|
|
{
|
|
|
|
return $this->authKey;
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public function validateAuthKey($authKey)
|
|
|
|
{
|
|
|
|
return $this->authKey === $authKey;
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:53:57 +01:00
|
|
|
/**
|
|
|
|
* Validates password
|
|
|
|
*
|
|
|
|
* @param string $password password to validate
|
|
|
|
* @return bool if password provided is valid for current user
|
|
|
|
*/
|
2013-05-24 10:14:49 -04:00
|
|
|
public function validatePassword($password)
|
|
|
|
{
|
|
|
|
return $this->password === $password;
|
|
|
|
}
|
|
|
|
}
|