-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUser.php
228 lines (204 loc) · 5.58 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use app\models\behaviors\TimestampBehavior;
use app\models\queries\UserQuery;
use app\helpers\Mail;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $is_email_verified
* @property string $password_hash
* @property string $password_reset_token
* @property string $email_confirmation_token
* @property string $email
* @property string $auth_key
* @property integer $role
* @property integer $status
* @property string $created_at
* @property string $updated_at
* @property string $password write-only password
*/
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const ROLE_USER = 10;
/**
* @var string|null the current password value from form input
*/
protected $_password;
/**
* @return UserQuery custom query class with user scopes
*/
public static function find()
{
return new UserQuery(get_called_class());
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
['role', 'default', 'value' => self::ROLE_USER],
['role', 'in', 'range' => [self::ROLE_USER]],
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
],
];
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if ($this->isNewRecord) {
$this->generateAuthKey();
$this->generateEmailConfirmationToken();
}
return parent::beforeSave($insert);
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
if ($insert && !Mail::toUser($this, 'confirm-email')) {
Yii::warning('Failed to send confirmation email to new user.', __METHOD__);
}
parent::afterSave($insert, $changedAttributes);
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
/**
* Sets a new password or keeps the old password if the provided password
* is empty.
*
* @param string $password
*/
public function setPassword($password)
{
$this->_password = $password;
if (!empty($password)) {
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
}
/**
* @return string|null the current password value, if set from form. Null otherwise.
*/
public function getPassword()
{
return $this->_password;
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* Generates new email confirmation token
* @param bool $save whether to save the record. Default is `false`.
* @return bool|null whether the save was successful or null if $save was false.
*/
public function generateEmailConfirmationToken($save = false)
{
$this->email_confirmation_token = Yii::$app->security->generateRandomString() . '_' . time();
if ($save) {
return $this->save();
}
}
/**
* Generates new password reset token
* @param bool $save whether to save the record. Default is `false`.
* @return bool|null whether the save was successful or null if $save was false.
*/
public function generatePasswordResetToken($save = false)
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
if ($save) {
return $this->save();
}
}
/**
* Resets to a new password and deletes the password reset token.
* @param string $password the new password for this user.
* @return bool whether the record was updated successfully
*/
public function resetPassword($password)
{
$this->setPassword($password);
$this->password_reset_token = null;
return $this->save();
}
/**
* Confirms an email an deletes the email confirmation token.
* @return bool whether the record was updated successfully
*/
public function confirmEmail()
{
$this->email_confirmation_token = null;
$this->is_email_verified = 1;
return $this->save();
}
}