User.php 1.04 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php

namespace app\models;

class User extends \yii\base\Object implements \yii\web\Identity
{
	public $id;
Qiang Xue committed
8 9
	public $username;
	public $password;
Qiang Xue committed
10 11 12 13 14
	public $authKey;

	private static $users = array(
		'100' => array(
			'id' => '100',
Qiang Xue committed
15 16
			'username' => 'admin',
			'password' => 'admin',
Qiang Xue committed
17 18 19 20
			'authKey' => 'test100key',
		),
		'101' => array(
			'id' => '101',
Qiang Xue committed
21 22
			'username' => 'demo',
			'password' => 'demo',
Qiang Xue committed
23 24 25 26 27 28 29 30 31
			'authKey' => 'test101key',
		),
	);

	public static function findIdentity($id)
	{
		return isset(self::$users[$id]) ? new self(self::$users[$id]) : null;
	}

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