1 | <?php |
||
95 | class User extends BaseUserModel |
||
96 | { |
||
97 | use UserPasswordHistoryTrait, UserLoginTrait, UserUsernameTrait, UserInvitationRegistrationTrait; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | public $searchClass = UserSearch::class; |
||
103 | |||
104 | /** |
||
105 | * @return null |
||
106 | */ |
||
107 | 18 | public function getSearchModel() |
|
108 | 18 | { |
|
109 | $class = $this->searchClass; |
||
110 | if (empty($class) || !class_exists($class)) { |
||
111 | return null; |
||
112 | } |
||
113 | return new $class; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 19 | public function attributeLabels() |
|
120 | { |
||
121 | $labels = [ |
||
122 | 1 | $this->guidAttribute => Yii::t('user', 'GUID'), |
|
123 | 1 | $this->idAttribute => Yii::t('user', 'ID'), |
|
124 | 1 | $this->passwordHashAttribute => Yii::t('user', 'Password Hash'), |
|
125 | 1 | $this->ipAttribute => Yii::t('user', 'IP Address'), |
|
126 | 1 | $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'), |
|
127 | 1 | $this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
|
128 | 1 | $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
|
129 | 1 | $this->authKeyAttribute => Yii::t('user', 'Authentication Key'), |
|
130 | 1 | $this->accessTokenAttribute => Yii::t('user', 'Access Token'), |
|
131 | 1 | $this->passwordResetTokenAttribute => Yii::t('user', 'Password Reset Token'), |
|
132 | 1 | $this->statusAttribute => Yii::t('user', 'Status'), |
|
133 | 1 | 'type' => Yii::t('user', 'Type'), |
|
134 | 1 | $this->sourceAttribute => Yii::t('user', 'Source'), |
|
135 | 19 | 'createdAt' => Yii::t('user', 'Registration Time'), |
|
136 | 1 | 'updatedAt' => Yii::t('user', 'Last Updated Time'), |
|
137 | 1 | ]; |
|
138 | 1 | return $labels; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public $idAttributeType = 1; |
||
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | public $idAttributeLength = 8; |
||
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | public $idAttributePrefix = '4'; |
||
155 | |||
156 | /** |
||
157 | * @var bool |
||
158 | */ |
||
159 | public $idPreassigned = true; |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | 53 | public static function tableName() |
|
168 | |||
169 | /** |
||
170 | * @var string|false Profile class name. If you do not need profile model, |
||
171 | * please set it false. |
||
172 | */ |
||
173 | public $profileClass = false; |
||
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | * ----------- |
||
178 | * When the user is updated or deleted, the cache contents tagged with the corresponding user tag will be invalidated. |
||
179 | */ |
||
180 | 53 | public function init() |
|
188 | |||
189 | /** |
||
190 | * @var string |
||
191 | */ |
||
192 | public $cacheTagPrefix = 'tag_user_'; |
||
193 | |||
194 | /** |
||
195 | * Get cache tag. |
||
196 | * The cache tag ends with the user ID, but after the user ID is modified, the old ID will prevail. |
||
197 | * @return string |
||
198 | */ |
||
199 | 44 | public function getCacheTag() |
|
204 | |||
205 | /** |
||
206 | * @param Event $event |
||
207 | * @return bool|string|array |
||
208 | */ |
||
209 | 44 | public function onInvalidTags($event) |
|
210 | { |
||
211 | try { |
||
212 | 44 | $cache = Yii::$app->get('cache'); |
|
213 | 44 | } catch (InvalidConfigException $ex) { |
|
214 | return true; |
||
215 | } |
||
216 | 44 | $sender = $event->sender; |
|
217 | /*@var $sender static */ |
||
218 | 44 | return TagDependency::invalidate($cache, $sender->getCacheTag()); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * Create profile. |
||
223 | * If profile of this user exists, it will be returned instead of creating it. |
||
224 | * Meanwhile, the $config parameter will be skipped. |
||
225 | * @param array $config Profile configuration. Skipped if it exists. |
||
226 | * @return Profile |
||
227 | */ |
||
228 | 19 | public function createProfile($config = []) |
|
229 | { |
||
230 | 19 | $profileClass = $this->profileClass; |
|
231 | 19 | if (empty($profileClass) || !is_string($this->profileClass)) { |
|
232 | 1 | return null; |
|
233 | } |
||
234 | 18 | $profile = $profileClass::findOne($this->getGUID()); |
|
235 | 18 | if (!$profile) { |
|
236 | 18 | $profile = $this->create($profileClass, $config); |
|
237 | 18 | $profile->setGUID($this->getGUID()); |
|
238 | 18 | } |
|
239 | 18 | return $profile; |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * |
||
244 | * @return boolean |
||
245 | */ |
||
246 | 4 | public function hasProfile() |
|
253 | |||
254 | /** |
||
255 | * Get Profile query. |
||
256 | * If you want to get profile model, please access this method in magic property way, |
||
257 | * like following: |
||
258 | * |
||
259 | * ```php |
||
260 | * $user->profile; |
||
261 | * ``` |
||
262 | * |
||
263 | * @return BaseBlameableQuery |
||
264 | */ |
||
265 | 4 | public function getProfile() |
|
275 | |||
276 | /** |
||
277 | * @param $event |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 48 | public function onGenerateId($event) |
|
286 | |||
287 | /** |
||
288 | * @return array |
||
289 | */ |
||
290 | 53 | public function generateIdBehavior() |
|
291 | { |
||
292 | return [ |
||
293 | 53 | 'class' => AttributeBehavior::class, |
|
294 | 'attributes' => [ |
||
295 | 53 | ActiveRecord::EVENT_BEFORE_INSERT => $this->idAttribute, |
|
296 | 53 | ], |
|
297 | 53 | 'value' => [$this, 'onGenerateId'], |
|
298 | 53 | ]; |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * @return array |
||
303 | */ |
||
304 | 53 | public function behaviors() |
|
305 | { |
||
306 | 53 | $behaviors = parent::behaviors(); |
|
307 | 53 | $behavior = $this->generateIdBehavior(); |
|
308 | 53 | if (!empty($behavior)) { |
|
309 | 53 | $behaviors[] = $behavior; |
|
310 | 53 | } |
|
311 | 53 | return $behaviors; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * @var string |
||
316 | */ |
||
317 | public static $idRegex = '/^\d{5,8}$/'; |
||
318 | |||
319 | /** |
||
320 | * @return array |
||
321 | */ |
||
322 | 2 | public function getIdRules() |
|
323 | { |
||
324 | return [ |
||
325 | 2 | [$this->idAttribute, 'safe'], |
|
326 | 2 | ]; |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * Friendly to IDE. |
||
331 | * @return UserQuery |
||
332 | */ |
||
333 | 48 | public static function find() |
|
337 | } |
||
338 |