|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\user; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
|
16
|
|
|
use rhosocial\base\models\queries\BaseBlameableQuery; |
|
17
|
|
|
use rhosocial\user\models\log\UserLoginTrait; |
|
18
|
|
|
use rhosocial\user\security\UserPasswordHistoryTrait; |
|
19
|
|
|
use Yii; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Common User Model. |
|
23
|
|
|
* This model should be stored in a relational database. You can create a foreign |
|
24
|
|
|
* key constraint on other models and this model. |
|
25
|
|
|
* |
|
26
|
|
|
* If you're using MySQL, we recommend that you create a data table using the following statement: |
|
27
|
|
|
* |
|
28
|
|
|
* ``` |
|
29
|
|
|
* CREATE TABLE `user` ( |
|
30
|
|
|
* `guid` varbinary(16) NOT NULL COMMENT 'GUID', |
|
31
|
|
|
* `id` varchar(16) COLLATE utf8_unicode_ci NOT NULL COMMENT 'ID', |
|
32
|
|
|
* `pass_hash` varchar(80) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Password Hash', |
|
33
|
|
|
* `ip` varbinary(16) NOT NULL DEFAULT '0' COMMENT 'IP', |
|
34
|
|
|
* `ip_type` tinyint(3) unsigned NOT NULL DEFAULT '4' COMMENT 'IP Address Type', |
|
35
|
|
|
* `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Create Time', |
|
36
|
|
|
* `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Update Time', |
|
37
|
|
|
* `auth_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Authentication Key', |
|
38
|
|
|
* `access_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Access Token', |
|
39
|
|
|
* `password_reset_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Password Reset Token', |
|
40
|
|
|
* `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT 'Status', |
|
41
|
|
|
* `type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Type', |
|
42
|
|
|
* `source` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Source', |
|
43
|
|
|
* PRIMARY KEY (`guid`), |
|
44
|
|
|
* UNIQUE KEY `user_id_unique` (`id`), |
|
45
|
|
|
* KEY `user_auth_key_normal` (`auth_key`), |
|
46
|
|
|
* KEY `user_access_token_normal` (`access_token`), |
|
47
|
|
|
* KEY `user_password_reset_token` (`password_reset_token`), |
|
48
|
|
|
* KEY `user_create_time_normal` (`created_at`) |
|
49
|
|
|
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='User'; |
|
50
|
|
|
* ``` |
|
51
|
|
|
* |
|
52
|
|
|
* The fields of User table in database are following: |
|
53
|
|
|
* @property string $guid User's GUID. This property is used to uniquely identify a user. |
|
54
|
|
|
* This property is automatically generated when the class is created, we do not |
|
55
|
|
|
* recommend that you modify this property, unless you know the consequences of doing so. |
|
56
|
|
|
* This property is also regareded as the foreign key target of other models associated |
|
57
|
|
|
* with this model. If you have to modify this property, the foreign key constraints |
|
58
|
|
|
* should be updating and deleting on cascade. |
|
59
|
|
|
* @property string $id User Identifier No. It is a 8-digit number beginning with 4 by default. |
|
60
|
|
|
* @property string $pass_hash Password Hash. |
|
61
|
|
|
* We strongly recommend you NOT to change this property directly! |
|
62
|
|
|
* If you want to set or reset password, please use setPassword() magic property instead. |
|
63
|
|
|
* @property integer $ip IP address. |
|
64
|
|
|
* @property integer $ipType |
|
65
|
|
|
* @property string $createdAt |
|
66
|
|
|
* @property string $updatedAt |
|
67
|
|
|
* @property string $auth_key |
|
68
|
|
|
* @property string $access_token |
|
69
|
|
|
* @property string $password_reset_token |
|
70
|
|
|
* @property integer $status |
|
71
|
|
|
* @property integer $type |
|
72
|
|
|
* @property string $source |
|
73
|
|
|
* |
|
74
|
|
|
* @property-read Profile $profile Profile. This magic property is read-only. |
|
75
|
|
|
* If you want to modify anyone property of Profile model, please get it first, |
|
76
|
|
|
* then change and save it, like following: |
|
77
|
|
|
* ```php |
|
78
|
|
|
* $profile = $user->profile; |
|
79
|
|
|
* $profile->nickname = 'vistart'; |
|
80
|
|
|
* $profile->save(); |
|
81
|
|
|
* ``` |
|
82
|
|
|
* If $profileClass is `false`, `null` returned. |
|
83
|
|
|
* @version 1.0 |
|
84
|
|
|
* @author vistart <[email protected]> |
|
85
|
|
|
*/ |
|
86
|
|
|
class User extends BaseUserModel |
|
87
|
|
|
{ |
|
88
|
|
|
use UserPasswordHistoryTrait, UserLoginTrait; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function attributeLabels() |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
1 |
|
'guid' => Yii::t('user', 'GUID'), |
|
97
|
1 |
|
'id' => Yii::t('user', 'ID'), |
|
98
|
1 |
|
'pass_hash' => Yii::t('user', 'Password Hash'), |
|
99
|
1 |
|
'ip' => Yii::t('user', 'IP Address'), |
|
100
|
1 |
|
'ip_type' => Yii::t('user', 'IP Address Type'), |
|
101
|
1 |
|
'created_at' => Yii::t('user', 'Creation Time'), |
|
102
|
1 |
|
'updated_at' => Yii::t('user', 'Last Updated Time'), |
|
103
|
1 |
|
'auth_key' => Yii::t('user', 'Authentication Key'), |
|
104
|
1 |
|
'access_token' => Yii::t('user', 'Access Token'), |
|
105
|
1 |
|
'password_reset_token' => Yii::t('user', 'Password Reset Token'), |
|
106
|
1 |
|
'status' => Yii::t('user', 'Status'), |
|
107
|
1 |
|
'type' => Yii::t('user', 'Type'), |
|
108
|
1 |
|
'source' => Yii::t('user', 'Source'), |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public $idAttributeType = 1; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritdoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public $idAttributeLength = 8; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public $idAttributePrefix = '4'; |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @inheritdoc |
|
129
|
|
|
*/ |
|
130
|
41 |
|
public static function tableName() |
|
131
|
|
|
{ |
|
132
|
41 |
|
return '{{%user}}'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @var string|false Profile class name. If you do not need profile model, |
|
137
|
|
|
* please set it false. |
|
138
|
|
|
*/ |
|
139
|
|
|
public $profileClass = false; |
|
140
|
|
|
|
|
141
|
41 |
|
public function init() |
|
142
|
|
|
{ |
|
143
|
41 |
|
$this->on(static::$eventAfterRegister, [$this, 'onAddPasswordToHistory']); |
|
144
|
41 |
|
$this->on(static::$eventAfterResetPassword, [$this, 'onAddPasswordToHistory']); |
|
145
|
41 |
|
parent::init(); |
|
146
|
41 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Create profile. |
|
150
|
|
|
* If profile of this user exists, it will be returned instead of creating it. |
|
151
|
|
|
* Meanwhile, the $config parameter will be skipped. |
|
152
|
|
|
* @param array $config Profile configuration. Skipped if it exists. |
|
153
|
|
|
* @return Profile |
|
154
|
|
|
*/ |
|
155
|
19 |
|
public function createProfile($config = []) |
|
156
|
|
|
{ |
|
157
|
19 |
|
$profileClass = $this->profileClass; |
|
158
|
19 |
|
if (empty($profileClass) || !is_string($this->profileClass)) { |
|
159
|
1 |
|
return null; |
|
160
|
|
|
} |
|
161
|
18 |
|
$profile = $profileClass::findOne($this->getGUID()); |
|
162
|
18 |
|
if (!$profile) { |
|
163
|
18 |
|
$profile = $this->create($profileClass, $config); |
|
164
|
18 |
|
$profile->setGUID($this->getGUID()); |
|
165
|
|
|
} |
|
166
|
18 |
|
return $profile; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* |
|
171
|
|
|
* @return boolean |
|
172
|
|
|
*/ |
|
173
|
4 |
|
public function hasProfile() |
|
174
|
|
|
{ |
|
175
|
4 |
|
if ($this->profileClass === false || !is_string($this->profileClass) || !class_exists($this->profileClass)) { |
|
176
|
2 |
|
return false; |
|
177
|
|
|
} |
|
178
|
2 |
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get Profile query. |
|
183
|
|
|
* If you want to get profile model, please access this method in magic property way, |
|
184
|
|
|
* like following: |
|
185
|
|
|
* |
|
186
|
|
|
* ```php |
|
187
|
|
|
* $user->profile; |
|
188
|
|
|
* ``` |
|
189
|
|
|
* |
|
190
|
|
|
* @return BaseBlameableQuery |
|
191
|
|
|
*/ |
|
192
|
4 |
|
public function getProfile() |
|
193
|
|
|
{ |
|
194
|
4 |
|
if (!$this->hasProfile()) { |
|
195
|
2 |
|
return null; |
|
196
|
|
|
} |
|
197
|
2 |
|
$profileClass = $this->profileClass; |
|
198
|
2 |
|
$profileModel = $profileClass::buildNoInitModel(); |
|
199
|
2 |
|
return $this->hasOne($profileClass, |
|
200
|
2 |
|
[$profileModel->createdByAttribute => $this->guidAttribute])->inverseOf('user'); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|