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