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\base\models\traits; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Assemble PasswordTrait, RegistrationTrait and IdentityTrait into UserTrait. |
17
|
|
|
* This trait can only be used in the class extended from [[BaseEntityModel]], |
18
|
|
|
* [[BaseMongoEntityModel]], [[BaseRedisEntityModel]], or any other classes used |
19
|
|
|
* [[EntityTrait]]. |
20
|
|
|
* This trait implements two methods `create()` and `findOneOrCreate()`. |
21
|
|
|
* Please read the notes of methods and used traits for further detailed usage. |
22
|
|
|
* |
23
|
|
|
* @version 1.0 |
24
|
|
|
* @author vistart <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait UserTrait |
27
|
|
|
{ |
28
|
|
|
use PasswordTrait, |
29
|
|
|
RegistrationTrait, |
30
|
|
|
IdentityTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create new entity model associated with current user. The model to be created |
34
|
|
|
* must be extended from [[BaseBlameableModel]], [[BaseMongoBlameableModel]], |
35
|
|
|
* [[BaseRedisBlameableModel]], or any other classes used [[BlameableTrait]]. |
36
|
|
|
* if $config does not specify `userClass` property, self will be assigned to. |
37
|
|
|
* @param string $className Full qualified class name. |
38
|
|
|
* @param array $config name-value pairs that will be used to initialize |
39
|
|
|
* the object properties. |
40
|
|
|
* @param boolean $loadDefault Determines whether loading default values |
41
|
|
|
* after entity model created. |
42
|
|
|
* Notice! The [[\yii\mongodb\ActiveRecord]] and [[\yii\redis\ActiveRecord]] |
43
|
|
|
* does not support loading default value. If you want to assign properties |
44
|
|
|
* with default values, please define the `default` rule(s) for properties in |
45
|
|
|
* `rules()` method and return them by yourself if you don't specified them in config param. |
46
|
|
|
* @param boolean $skipIfSet whether existing value should be preserved. |
47
|
|
|
* This will only set defaults for attributes that are `null`. |
48
|
|
|
* @return [[$className]] new model created with specified configuration. |
49
|
|
|
*//* |
50
|
|
|
public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
51
|
|
|
{ |
52
|
|
|
if (!isset($config['hostClass'])) { |
53
|
|
|
$config['hostClass'] = static::class; |
54
|
|
|
} |
55
|
|
|
if (isset($config['class'])) { |
56
|
|
|
unset($config['class']); |
57
|
|
|
} |
58
|
|
|
$entity = new $className($config); |
59
|
|
|
$entity->setHost($this); |
60
|
|
|
if ($loadDefault && method_exists($entity, 'loadDefaultValues')) { |
61
|
|
|
$entity->loadDefaultValues($skipIfSet); |
62
|
|
|
} |
63
|
|
|
return $entity; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* This method is only used for overriding [[removeSelf()]] in [[TimestampTrait]]. |
68
|
|
|
* @see deregister() |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
4 |
|
public function removeSelf() |
72
|
|
|
{ |
73
|
4 |
|
return $this->deregister(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get all rules with current user properties. |
78
|
|
|
* @return array all rules. |
79
|
|
|
*/ |
80
|
172 |
|
public function rules() |
81
|
|
|
{ |
82
|
172 |
|
return array_merge( |
83
|
172 |
|
parent::rules(), |
84
|
172 |
|
$this->getPasswordHashRules(), |
85
|
172 |
|
$this->getPasswordResetTokenRules(), |
86
|
172 |
|
$this->getSourceRules(), |
87
|
172 |
|
$this->getStatusRules(), |
88
|
172 |
|
$this->getAuthKeyRules(), |
89
|
172 |
|
$this->getAccessTokenRules() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|