User.php 1.38 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 11
	public $authKey;

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

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

35 36 37 38 39 40
	/**
	 * Finds user by username
	 *
	 * @param string $username
	 * @return static|null
	 */
Qiang Xue committed
41 42 43 44
	public static function findByUsername($username)
	{
		foreach (self::$users as $user) {
			if (strcasecmp($user['username'], $username) === 0) {
45
				return new static($user);
Qiang Xue committed
46 47 48 49 50
			}
		}
		return null;
	}

51 52 53
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
54 55 56 57 58
	public function getId()
	{
		return $this->id;
	}

59 60 61
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
62 63 64 65 66
	public function getAuthKey()
	{
		return $this->authKey;
	}

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

75 76 77 78 79 80
	/**
	 * Validates password
	 *
	 * @param string $password password to validate
	 * @return bool if password provided is valid for current user
	 */
Qiang Xue committed
81 82 83 84
	public function validatePassword($password)
	{
		return $this->password === $password;
	}
Zander Baldwin committed
85
}