|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiSite Yii2 base project. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hisite |
|
6
|
|
|
* @package hisite |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hisite\models; |
|
12
|
|
|
|
|
13
|
|
|
class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public $id; |
|
16
|
|
|
public $username; |
|
17
|
|
|
public $password; |
|
18
|
|
|
public $authKey; |
|
19
|
|
|
public $accessToken; |
|
20
|
|
|
|
|
21
|
|
|
private static $users = [ |
|
22
|
|
|
'100' => [ |
|
23
|
|
|
'id' => '100', |
|
24
|
|
|
'username' => 'admin', |
|
25
|
|
|
'password' => 'admin', |
|
26
|
|
|
'authKey' => 'test100key', |
|
27
|
|
|
'accessToken' => '100-token', |
|
28
|
|
|
], |
|
29
|
|
|
'101' => [ |
|
30
|
|
|
'id' => '101', |
|
31
|
|
|
'username' => 'demo', |
|
32
|
|
|
'password' => 'demo', |
|
33
|
|
|
'authKey' => 'test101key', |
|
34
|
|
|
'accessToken' => '101-token', |
|
35
|
|
|
], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function findIdentity($id) |
|
42
|
|
|
{ |
|
43
|
|
|
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach (self::$users as $user) { |
|
52
|
|
|
if ($user['accessToken'] === $token) { |
|
53
|
|
|
return new static($user); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Finds user by username. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $username |
|
64
|
|
|
* @return static|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function findByUsername($username) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach (self::$users as $user) { |
|
69
|
|
|
if (strcasecmp($user['username'], $username) === 0) { |
|
70
|
|
|
return new static($user); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getId() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->id; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAuthKey() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->authKey; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function validateAuthKey($authKey) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->authKey === $authKey; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Validates password. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $password password to validate |
|
105
|
|
|
* @return boolean if password provided is valid for current user |
|
106
|
|
|
*/ |
|
107
|
|
|
public function validatePassword($password) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->password === $password; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|