1 | <?php |
||
29 | trait RegistrationTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @event Event an event that is triggered after user is registered successfully. |
||
34 | */ |
||
35 | public static $eventAfterRegister = "afterRegister"; |
||
36 | |||
37 | /** |
||
38 | * @event Event an event that is triggered before registration. |
||
39 | */ |
||
40 | public static $eventBeforeRegister = "beforeRegister"; |
||
41 | |||
42 | /** |
||
43 | * @event Event an event that is triggered when registration failed. |
||
44 | */ |
||
45 | public static $eventRegisterFailed = "registerFailed"; |
||
46 | |||
47 | /** |
||
48 | * @event Event an event that is triggered after user is deregistered successfully. |
||
49 | */ |
||
50 | public static $eventAfterDeregister = "afterDeregister"; |
||
51 | |||
52 | /** |
||
53 | * @event Event an event that is triggered before deregistration. |
||
54 | */ |
||
55 | public static $eventBeforeDeregister = "beforeDeregister"; |
||
56 | |||
57 | /** |
||
58 | * @event Event an event that is triggered when deregistration failed. |
||
59 | */ |
||
60 | public static $eventDeregisterFailed = "deregisterFailed"; |
||
61 | |||
62 | /** |
||
63 | * @var string name of attribute which store the source. if you don't want to |
||
64 | * record source, please assign false. |
||
65 | */ |
||
66 | public $sourceAttribute = 'source'; |
||
67 | private $_sourceRules = []; |
||
68 | public static $sourceSelf = '0'; |
||
69 | |||
70 | /** |
||
71 | * @var string auth manager component id. |
||
72 | */ |
||
73 | public $authManagerId = 'authManager'; |
||
74 | |||
75 | /** |
||
76 | * Get auth manager. If auth manager not configured, Yii::$app->authManager |
||
77 | * will be given. |
||
78 | * @return BaseManager |
||
79 | */ |
||
80 | 56 | public function getAuthManager() |
|
85 | |||
86 | /** |
||
87 | * Register new user. |
||
88 | * It is equivalent to store the current user and its associated models into |
||
89 | * database synchronously. The registration will be terminated immediately |
||
90 | * if any errors occur in the process, and all the earlier steps succeeded |
||
91 | * are rolled back. |
||
92 | * If auth manager configured, and auth role(s) provided, it(they) will be |
||
93 | * assigned to user after registration. |
||
94 | * If current user is not a new one(isNewRecord = false), the registration |
||
95 | * will be skipped and return false. |
||
96 | * The $eventBeforeRegister will be triggered before registration starts. |
||
97 | * If registration finished, the $eventAfterRegister will be triggered. or |
||
98 | * $eventRegisterFailed will be triggered when any errors occured. |
||
99 | * @param array $associatedModels The models associated with user to be stored synchronously. |
||
100 | * @param string|array $authRoles auth name, auth instance, auth name array or auth instance array. |
||
101 | * @return boolean Whether the registration succeeds or not. |
||
102 | * @throws IntegrityException when inserting user and associated models failed. |
||
103 | */ |
||
104 | 56 | public function register($associatedModels = [], $authRoles = []) |
|
149 | |||
150 | /** |
||
151 | * Deregister current user itself. |
||
152 | * It is equivalent to delete current user and its associated models. BUT it |
||
153 | * deletes current user ONLY, the associated models will not be deleted |
||
154 | * forwardly. So you should set the foreign key of associated models' table |
||
155 | * referenced from primary key of user table, and their association mode is |
||
156 | * 'on update cascade' and 'on delete cascade'. |
||
157 | * the $eventBeforeDeregister will be triggered before deregistration starts. |
||
158 | * if deregistration finished, the $eventAfterDeregister will be triggered. or |
||
159 | * $eventDeregisterFailed will be triggered when any errors occured. |
||
160 | * @return boolean Whether deregistration succeeds or not. |
||
161 | * @throws IntegrityException when deleting user failed. |
||
162 | */ |
||
163 | 56 | public function deregister() |
|
189 | |||
190 | /** |
||
191 | * Get source. |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getSource() |
||
199 | |||
200 | /** |
||
201 | * Set source. |
||
202 | * @param string $source |
||
203 | */ |
||
204 | public function setSource($source) |
||
209 | |||
210 | /** |
||
211 | * Get the rules associated with source attribute. |
||
212 | * @return array rules. |
||
213 | */ |
||
214 | 56 | public function getSourceRules() |
|
224 | |||
225 | /** |
||
226 | * Set the rules associated with source attribute. |
||
227 | * @param array $rules |
||
228 | */ |
||
229 | public function setSourceRules($rules) |
||
235 | |||
236 | /** |
||
237 | * Initialize the source attribute with $sourceSelf. |
||
238 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
239 | * override or modify it directly, unless you know the consequences. |
||
240 | * @param ModelEvent $event |
||
241 | */ |
||
242 | 60 | public function onInitSourceAttribute($event) |
|
248 | } |
||
249 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: