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