yii2-netdisk/models/User.php

86 lines
1.4 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
{
public $id;
public $username;
public $password;
public $authKey;
2013-10-18 17:48:27 +08:00
private static $users = [
'100' => [
2013-05-24 22:14:49 +08:00
'id' => '100',
'username' => 'admin',
'password' => 'admin',
'authKey' => 'test100key',
2013-10-18 17:48:27 +08:00
],
'101' => [
2013-05-24 22:14:49 +08:00
'id' => '101',
'username' => 'demo',
'password' => 'demo',
'authKey' => 'test101key',
2013-10-18 17:48:27 +08:00
],
];
2013-05-24 22:14:49 +08:00
/**
* @inheritdoc
*/
2013-05-24 22:14:49 +08:00
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
2013-05-24 22:14:49 +08:00
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
2013-05-24 22:14:49 +08:00
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
}
}
return null;
}
/**
* @inheritdoc
*/
2013-05-24 22:14:49 +08:00
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
2013-05-24 22:14:49 +08:00
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
2013-05-24 22:14:49 +08:00
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
*/
2013-05-24 22:14:49 +08:00
public function validatePassword($password)
{
return $this->password === $password;
}
}