|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace modules\users\models; |
|
5
|
|
|
|
|
6
|
|
|
use modules\users\Module; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\base\Exception; |
|
9
|
|
|
use yii\db\ActiveRecord; |
|
10
|
|
|
use yii\helpers\ArrayHelper; |
|
11
|
|
|
use yii\helpers\Html; |
|
12
|
|
|
use yii\web\IdentityInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class BaseUser |
|
16
|
|
|
* @package modules\users\models |
|
17
|
|
|
* |
|
18
|
|
|
* @property int $id ID |
|
19
|
|
|
* @property string $auth_key Authorization Key |
|
20
|
|
|
* @property string $email_confirm_token Email Confirm Token |
|
21
|
|
|
*/ |
|
22
|
|
|
class BaseUser extends ActiveRecord implements IdentityInterface |
|
23
|
|
|
{ |
|
24
|
|
|
// Statuses |
|
25
|
|
|
const STATUS_BLOCKED = 0; |
|
26
|
|
|
const STATUS_ACTIVE = 1; |
|
27
|
|
|
const STATUS_WAIT = 2; |
|
28
|
|
|
const STATUS_DELETED = 3; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function tableName() |
|
35
|
|
|
{ |
|
36
|
|
|
return '{{%user}}'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
* @param int|string $id |
|
42
|
|
|
* @return User|null|IdentityInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function findIdentity($id) |
|
45
|
|
|
{ |
|
46
|
|
|
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param mixed $token |
|
51
|
|
|
* @param mixed $type |
|
52
|
|
|
* @return User|IdentityInterface|null |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
55
|
|
|
{ |
|
56
|
|
|
return static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
* @return int|string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getId() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->id; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getAuthKey() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->auth_key; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
* @param string $authKey |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function validateAuthKey($authKey) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getAuthKey() === $authKey; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Generates "remember me" authentication key |
|
89
|
|
|
* @throws Exception |
|
90
|
|
|
*/ |
|
91
|
|
|
public function generateAuthKey() |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var yii\base\Security $security */ |
|
94
|
|
|
$security = Yii::$app->security; |
|
95
|
|
|
$this->auth_key = $security->generateRandomString(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Generates email confirmation token |
|
100
|
|
|
* @throws Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function generateEmailConfirmToken() |
|
103
|
|
|
{ |
|
104
|
|
|
/** @var yii\base\Security $security */ |
|
105
|
|
|
$security = Yii::$app->security; |
|
106
|
|
|
$this->email_confirm_token = $security->generateRandomString(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Finds out if password reset token is valid |
|
111
|
|
|
* |
|
112
|
|
|
* @param mixed $token password reset token |
|
113
|
|
|
* @return boolean |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function isPasswordResetTokenValid($token) |
|
116
|
|
|
{ |
|
117
|
|
|
if (empty($token)) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
$expire = Module::$passwordResetTokenExpire; |
|
121
|
|
|
$parts = explode('_', $token); |
|
122
|
|
|
$timestamp = (int)end($parts); |
|
123
|
|
|
return $timestamp + $expire >= time(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Finds user by password reset token |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $token password reset token |
|
130
|
|
|
* @return static|null |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function findByPasswordResetToken($token) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!static::isPasswordResetTokenValid($token)) { |
|
135
|
|
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
return static::findOne([ |
|
138
|
|
|
'password_reset_token' => $token, |
|
139
|
|
|
'status' => self::STATUS_ACTIVE |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function getStatusesArray() |
|
147
|
|
|
{ |
|
148
|
|
|
return [ |
|
149
|
|
|
self::STATUS_BLOCKED => Module::t('module', 'Blocked'), |
|
150
|
|
|
self::STATUS_ACTIVE => Module::t('module', 'Active'), |
|
151
|
|
|
self::STATUS_WAIT => Module::t('module', 'Wait'), |
|
152
|
|
|
self::STATUS_DELETED => Module::t('module', 'Deleted') |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function getLabelsArray() |
|
160
|
|
|
{ |
|
161
|
|
|
return [ |
|
162
|
|
|
self::STATUS_BLOCKED => 'default', |
|
163
|
|
|
self::STATUS_ACTIVE => 'success', |
|
164
|
|
|
self::STATUS_WAIT => 'warning', |
|
165
|
|
|
self::STATUS_DELETED => 'danger' |
|
166
|
|
|
]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getStatusName() |
|
173
|
|
|
{ |
|
174
|
|
|
return ArrayHelper::getValue(self::getStatusesArray(), $this->status); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Return <span class="label label-success">Active</span> |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getStatusLabelName() |
|
182
|
|
|
{ |
|
183
|
|
|
$name = ArrayHelper::getValue(self::getLabelsArray(), $this->status); |
|
|
|
|
|
|
184
|
|
|
return Html::tag('span', $this->getStatusName(), ['class' => 'label label-' . $name]); |
|
185
|
|
|
} |
|
186
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: