1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Model; |
13
|
|
|
|
14
|
|
|
use Da\User\Helper\SecurityHelper; |
15
|
|
|
use Da\User\Query\UserQuery; |
16
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
17
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\NotSupportedException; |
20
|
|
|
use yii\behaviors\TimestampBehavior; |
21
|
|
|
use yii\db\ActiveRecord; |
22
|
|
|
use yii\helpers\ArrayHelper; |
23
|
|
|
use yii\web\Application; |
24
|
|
|
use yii\web\IdentityInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* User ActiveRecord model. |
28
|
|
|
* |
29
|
|
|
* @property bool $isAdmin |
30
|
|
|
* @property bool $isBlocked |
31
|
|
|
* @property bool $isConfirmed whether user account has been confirmed or not |
32
|
|
|
* |
33
|
|
|
* Database fields: |
34
|
|
|
* @property int $id |
35
|
|
|
* @property string $username |
36
|
|
|
* @property string $email |
37
|
|
|
* @property string $unconfirmed_email |
38
|
|
|
* @property string $password_hash |
39
|
|
|
* @property string $auth_key |
40
|
|
|
* @property int $registration_ip |
41
|
|
|
* @property int $confirmed_at |
42
|
|
|
* @property int $blocked_at |
43
|
|
|
* @property int $flags |
44
|
|
|
* @property int $created_at |
45
|
|
|
* @property int $updated_at |
46
|
|
|
* @property int $last_login_at |
47
|
|
|
* |
48
|
|
|
* Defined relations: |
49
|
|
|
* @property SocialNetworkAccount[] $socialNetworkAccounts |
50
|
|
|
* @property Profile $profile |
51
|
|
|
*/ |
52
|
|
|
class User extends ActiveRecord implements IdentityInterface |
53
|
|
|
{ |
54
|
|
|
use ModuleAwareTrait; |
55
|
|
|
use ContainerAwareTrait; |
56
|
|
|
|
57
|
|
|
// following constants are used on secured email changing process |
58
|
|
|
const OLD_EMAIL_CONFIRMED = 0b1; |
59
|
|
|
const NEW_EMAIL_CONFIRMED = 0b10; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string Plain password. Used for model validation |
63
|
|
|
*/ |
64
|
|
|
public $password; |
65
|
|
|
/** |
66
|
|
|
* @var array connected account list |
67
|
|
|
*/ |
68
|
|
|
protected $connectedAccounts; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function beforeSave($insert) |
74
|
|
|
{ |
75
|
|
|
/** @var SecurityHelper $security */ |
76
|
|
|
$security = $this->make(SecurityHelper::class); |
77
|
|
|
if ($insert) { |
78
|
|
|
$this->setAttribute('auth_key', $security->generateRandomString()); |
79
|
|
|
if (Yii::$app instanceof Application) { |
80
|
|
|
$this->setAttribute('registration_ip', Yii::$app->request->getUserIP()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!empty($this->password)) { |
85
|
|
|
$this->setAttribute( |
86
|
|
|
'password_hash', |
87
|
|
|
$security->generatePasswordHash($this->password, $this->getModule()->blowfishCost) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return parent::beforeSave($insert); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function afterSave($insert, $changedAttributes) |
98
|
|
|
{ |
99
|
|
|
parent::afterSave($insert, $changedAttributes); |
100
|
|
|
|
101
|
|
|
if ($insert && $this->profile === null) { |
102
|
|
|
$profile = $this->make(Profile::class); |
103
|
|
|
$profile->link('user', $this); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
10 |
|
public static function tableName() |
111
|
|
|
{ |
112
|
10 |
|
return '{{%user}}'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function behaviors() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
1 |
|
TimestampBehavior::className(), |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function attributeLabels() |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
'username' => Yii::t('usuario', 'Username'), |
132
|
|
|
'email' => Yii::t('usuario', 'Email'), |
133
|
|
|
'registration_ip' => Yii::t('usuario', 'Registration IP'), |
134
|
|
|
'unconfirmed_email' => Yii::t('usuario', 'New email'), |
135
|
|
|
'password' => Yii::t('usuario', 'Password'), |
136
|
|
|
'created_at' => Yii::t('usuario', 'Registration time'), |
137
|
|
|
'confirmed_at' => Yii::t('usuario', 'Confirmation time'), |
138
|
|
|
'last_login_at' => Yii::t('usuario', 'Last login'), |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function scenarios() |
146
|
|
|
{ |
147
|
|
|
return ArrayHelper::merge( |
148
|
|
|
parent::scenarios(), |
149
|
|
|
[ |
150
|
|
|
'register' => ['username', 'email', 'password'], |
151
|
|
|
'connect' => ['username', 'email'], |
152
|
|
|
'create' => ['username', 'email', 'password'], |
153
|
|
|
'update' => ['username', 'email', 'password'], |
154
|
|
|
'settings' => ['username', 'email', 'password'], |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function rules() |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
|
|
// username rules |
166
|
|
|
'usernameRequired' => ['username', 'required', 'on' => ['register', 'create', 'connect', 'update']], |
167
|
|
|
'usernameMatch' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'], |
168
|
|
|
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], |
169
|
|
|
'usernameTrim' => ['username', 'trim'], |
170
|
|
|
'usernameUnique' => [ |
171
|
|
|
'username', |
172
|
|
|
'unique', |
173
|
|
|
'message' => Yii::t('usuario', 'This username has already been taken'), |
174
|
|
|
], |
175
|
|
|
|
176
|
|
|
// email rules |
177
|
|
|
'emailRequired' => ['email', 'required', 'on' => ['register', 'connect', 'create', 'update']], |
178
|
|
|
'emailPattern' => ['email', 'email'], |
179
|
|
|
'emailLength' => ['email', 'string', 'max' => 255], |
180
|
|
|
'emailUnique' => [ |
181
|
|
|
'email', |
182
|
|
|
'unique', |
183
|
|
|
'message' => Yii::t('usuario', 'This email address has already been taken'), |
184
|
|
|
], |
185
|
|
|
'emailTrim' => ['email', 'trim'], |
186
|
|
|
|
187
|
|
|
// password rules |
188
|
|
|
'passwordRequired' => ['password', 'required', 'on' => ['register']], |
189
|
|
|
'passwordLength' => ['password', 'string', 'min' => 6, 'max' => 72, 'on' => ['register', 'create']], |
190
|
|
|
]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function validateAuthKey($authKey) |
197
|
|
|
{ |
198
|
|
|
return $this->getAttribute('auth_key') === $authKey; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function getId() |
205
|
|
|
{ |
206
|
|
|
return $this->getAttribute('id'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function getAuthKey() |
213
|
|
|
{ |
214
|
|
|
return $this->getAttribute('auth_key'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public static function findIdentity($id) |
221
|
|
|
{ |
222
|
|
|
return static::findOne($id); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return bool whether is blocked or not |
227
|
|
|
*/ |
228
|
|
|
public function getIsBlocked() |
229
|
|
|
{ |
230
|
|
|
return $this->blocked_at !== null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool whether the user is an admin or not |
235
|
|
|
*/ |
236
|
|
|
public function getIsAdmin() |
237
|
|
|
{ |
238
|
|
|
return $this->getAuth()->isAdmin($this->username); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns whether user account has been confirmed or not. |
243
|
|
|
* @return bool whether user account has been confirmed or not |
244
|
|
|
*/ |
245
|
|
|
public function getIsConfirmed() |
246
|
|
|
{ |
247
|
|
|
return $this->confirmed_at !== null; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Checks whether a user has a specific role. |
252
|
|
|
* |
253
|
|
|
* @param string $role |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
public function hasRole($role) |
258
|
|
|
{ |
259
|
|
|
return $this->getAuth()->hasRole($this->id, $role); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return \yii\db\ActiveQuery |
264
|
|
|
*/ |
265
|
|
|
public function getProfile() |
266
|
|
|
{ |
267
|
|
|
return $this->hasOne($this->getClassMap()->get(Profile::class), ['user_id' => 'id']); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ] |
272
|
|
|
*/ |
273
|
|
|
public function getSocialNetworkAccounts() |
274
|
|
|
{ |
275
|
|
|
if ($this->connectedAccounts == null) { |
276
|
|
|
/** @var SocialNetworkAccount[] $accounts */ |
277
|
|
|
$accounts = $this->hasMany( |
278
|
|
|
$this->getClassMap() |
279
|
|
|
->get(SocialNetworkAccount::class), |
280
|
|
|
['user_id' => 'id'] |
281
|
|
|
) |
282
|
|
|
->all(); |
283
|
|
|
|
284
|
|
|
foreach ($accounts as $account) { |
285
|
|
|
$this->connectedAccounts[$account->provider] = $account; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this->connectedAccounts; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return UserQuery |
294
|
|
|
*/ |
295
|
10 |
|
public static function find() |
296
|
|
|
{ |
297
|
10 |
|
return new UserQuery(static::class); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* {@inheritdoc} |
302
|
|
|
*/ |
303
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
304
|
|
|
{ |
305
|
|
|
throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.'); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|