User.php 1.68 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php

namespace app\models;

5
class User extends \yii\base\Object implements \yii\web\IdentityInterface
Qiang Xue committed
6 7
{
	public $id;
Qiang Xue committed
8 9
	public $username;
	public $password;
Qiang Xue committed
10
	public $authKey;
11
	public $accessToken;
Qiang Xue committed
12

13 14
	private static $users = [
		'100' => [
Qiang Xue committed
15
			'id' => '100',
Qiang Xue committed
16 17
			'username' => 'admin',
			'password' => 'admin',
Qiang Xue committed
18
			'authKey' => 'test100key',
19
			'accessToken' => '100-token',
20 21
		],
		'101' => [
Qiang Xue committed
22
			'id' => '101',
Qiang Xue committed
23 24
			'username' => 'demo',
			'password' => 'demo',
Qiang Xue committed
25
			'authKey' => 'test101key',
26
			'accessToken' => '101-token',
27 28
		],
	];
Qiang Xue committed
29

30 31 32
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
33 34
	public static function findIdentity($id)
	{
35
		return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
Qiang Xue committed
36 37
	}

Qiang Xue committed
38 39 40
	/**
	 * @inheritdoc
	 */
41
	public static function findIdentityByAccessToken($token)
Qiang Xue committed
42 43
	{
		foreach (self::$users as $user) {
44
			if ($user['accessToken'] === $token) {
Qiang Xue committed
45 46 47 48 49 50
				return new static($user);
			}
		}
		return null;
	}

51 52 53 54 55 56
	/**
	 * Finds user by username
	 *
	 * @param string $username
	 * @return static|null
	 */
Qiang Xue committed
57 58 59 60
	public static function findByUsername($username)
	{
		foreach (self::$users as $user) {
			if (strcasecmp($user['username'], $username) === 0) {
61
				return new static($user);
Qiang Xue committed
62 63 64 65 66
			}
		}
		return null;
	}

67 68 69
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
70 71 72 73 74
	public function getId()
	{
		return $this->id;
	}

75 76 77
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
78 79 80 81 82
	public function getAuthKey()
	{
		return $this->authKey;
	}

83 84 85
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
86 87 88 89
	public function validateAuthKey($authKey)
	{
		return $this->authKey === $authKey;
	}
Qiang Xue committed
90

91 92 93 94
	/**
	 * Validates password
	 *
	 * @param string $password password to validate
95
	 * @return boolean if password provided is valid for current user
96
	 */
Qiang Xue committed
97 98 99 100
	public function validatePassword($password)
	{
		return $this->password === $password;
	}
Zander Baldwin committed
101
}