yii2-netdisk/models/User.php

105 lines
2.1 KiB
PHP
Raw Normal View History

2013-05-24 22:14:49 +08:00
<?php
namespace app\models;
class User extends \yii\base\Object implements \yii\web\IdentityInterface
2013-05-24 22:14:49 +08:00
{
2014-03-16 12:46:16 +08:00
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
2013-05-24 22:14:49 +08:00
2014-03-16 12:46:16 +08: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 22:14:49 +08:00
2016-04-29 00:06:20 +08:00
2014-03-16 12:46:16 +08:00
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
2013-05-24 22:14:49 +08:00
2014-03-16 12:46:16 +08:00
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
2014-03-16 12:46:16 +08:00
{
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
2014-03-02 12:14:16 +08:00
2014-03-16 12:46:16 +08:00
return null;
}
2013-05-24 22:14:49 +08:00
2014-03-16 12:46:16 +08:00
/**
* Finds user by username
*
2016-04-29 00:06:20 +08:00
* @param string $username
2014-03-16 12:46:16 +08:00
* @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 22:14:49 +08:00
2014-03-16 12:46:16 +08:00
return null;
}
2013-05-24 22:14:49 +08:00
2014-03-16 12:46:16 +08:00
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
2013-05-24 22:14:49 +08:00
2014-03-16 12:46:16 +08:00
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
2016-04-29 00:06:20 +08:00
* @param string $password password to validate
2014-03-16 12:46:16 +08:00
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
2013-05-24 22:14:49 +08:00
}