User.php 1.04 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

	public static function findIdentity($id)
	{
29
		return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
Qiang Xue committed
30 31
	}

Qiang Xue committed
32 33 34 35
	public static function findByUsername($username)
	{
		foreach (self::$users as $user) {
			if (strcasecmp($user['username'], $username) === 0) {
36
				return new static($user);
Qiang Xue committed
37 38 39 40 41
			}
		}
		return null;
	}

Qiang Xue committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	public function getId()
	{
		return $this->id;
	}

	public function getAuthKey()
	{
		return $this->authKey;
	}

	public function validateAuthKey($authKey)
	{
		return $this->authKey === $authKey;
	}
Qiang Xue committed
56 57 58 59 60

	public function validatePassword($password)
	{
		return $this->password === $password;
	}
Zander Baldwin committed
61
}