yii2-netdisk/models/User.php

105 lines
2.1 KiB
PHP
Raw Normal View History

2013-05-24 10:14:49 -04:00
<?php
namespace app\models;
class User extends \yii\base\BaseObject 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
2016-04-28 18:06:20 +02: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
*/
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
*
2016-04-28 18:06:20 +02:00
* @param string $username
2014-03-16 10:46:16 +06: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 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
*
2016-04-28 18:06:20 +02:00
* @param string $password password to validate
2016-10-20 14:10:38 +02:00
* @return bool if password provided is valid for current user
2014-03-16 10:46:16 +06:00
*/
public function validatePassword($password)
{
return $this->password === $password;
}
2013-05-24 10:14:49 -04:00
}