1 | <?php |
||
31 | trait RegistrationTrait |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * @event Event an event that is triggered after user is registered successfully. |
||
36 | */ |
||
37 | public static $eventAfterRegister = "afterRegister"; |
||
38 | |||
39 | /** |
||
40 | * @event Event an event that is triggered before registration. |
||
41 | */ |
||
42 | public static $eventBeforeRegister = "beforeRegister"; |
||
43 | |||
44 | /** |
||
45 | * @event Event an event that is triggered when registration failed. |
||
46 | */ |
||
47 | public static $eventRegisterFailed = "registerFailed"; |
||
48 | |||
49 | /** |
||
50 | * @event Event an event that is triggered after user is deregistered successfully. |
||
51 | */ |
||
52 | public static $eventAfterDeregister = "afterDeregister"; |
||
53 | |||
54 | /** |
||
55 | * @event Event an event that is triggered before deregistration. |
||
56 | */ |
||
57 | public static $eventBeforeDeregister = "beforeDeregister"; |
||
58 | |||
59 | /** |
||
60 | * @event Event an event that is triggered when deregistration failed. |
||
61 | */ |
||
62 | public static $eventDeregisterFailed = "deregisterFailed"; |
||
63 | |||
64 | /** |
||
65 | * @var string name of attribute which store the source. if you don't want to |
||
66 | * record source, please assign false. |
||
67 | */ |
||
68 | public $sourceAttribute = 'source'; |
||
69 | private $_sourceRules = []; |
||
70 | public static $sourceSelf = '0'; |
||
71 | |||
72 | /** |
||
73 | * @var string auth manager component id. |
||
74 | */ |
||
75 | public $authManagerId = 'authManager'; |
||
76 | |||
77 | /** |
||
78 | * Get auth manager. If auth manager not configured, Yii::$app->authManager |
||
79 | * will be given. |
||
80 | * @return BaseManager |
||
81 | */ |
||
82 | 125 | public function getAuthManager() |
|
87 | |||
88 | /** |
||
89 | * Register new user. |
||
90 | * It is equivalent to store the current user and its associated models into |
||
91 | * database synchronously. The registration will be terminated immediately |
||
92 | * if any errors occur in the process, and all the earlier steps succeeded |
||
93 | * are rolled back. |
||
94 | * If auth manager configured, and auth role(s) provided, it(they) will be |
||
95 | * assigned to user after registration. |
||
96 | * If current user is not a new one(isNewRecord = false), the registration |
||
97 | * will be skipped and return false. |
||
98 | * The $eventBeforeRegister will be triggered before registration starts. |
||
99 | * If registration finished, the $eventAfterRegister will be triggered. or |
||
100 | * $eventRegisterFailed will be triggered when any errors occured. |
||
101 | * @param array $associatedModels The models associated with user to be stored synchronously. |
||
102 | * @param string|array $authRoles auth name, auth instance, auth name array or auth instance array. |
||
103 | * @return boolean Whether the registration succeeds or not. |
||
104 | * @throws IntegrityException when inserting user and associated models failed. |
||
105 | */ |
||
106 | 126 | public function register($associatedModels = [], $authRoles = []) |
|
151 | |||
152 | /** |
||
153 | * Deregister current user itself. |
||
154 | * It is equivalent to delete current user and its associated models. BUT it |
||
155 | * deletes current user ONLY, the associated models will not be deleted |
||
156 | * forwardly. So you should set the foreign key of associated models' table |
||
157 | * referenced from primary key of user table, and their association mode is |
||
158 | * 'on update cascade' and 'on delete cascade'. |
||
159 | * the $eventBeforeDeregister will be triggered before deregistration starts. |
||
160 | * if deregistration finished, the $eventAfterDeregister will be triggered. or |
||
161 | * $eventDeregisterFailed will be triggered when any errors occured. |
||
162 | * @return boolean Whether deregistration succeeds or not. |
||
163 | * @throws IntegrityException when deleting user failed. |
||
164 | */ |
||
165 | 125 | public function deregister() |
|
194 | |||
195 | /** |
||
196 | * Get source. |
||
197 | * @return string |
||
198 | */ |
||
199 | 1 | public function getSource() |
|
204 | |||
205 | /** |
||
206 | * Set source. |
||
207 | * @param string $source |
||
208 | */ |
||
209 | 2 | public function setSource($source) |
|
214 | |||
215 | /** |
||
216 | * Get the rules associated with source attribute. |
||
217 | * @return array rules. |
||
218 | */ |
||
219 | 136 | public function getSourceRules() |
|
229 | |||
230 | /** |
||
231 | * Set the rules associated with source attribute. |
||
232 | * @param array $rules |
||
233 | */ |
||
234 | 1 | public function setSourceRules($rules) |
|
240 | |||
241 | /** |
||
242 | * Initialize the source attribute with $sourceSelf. |
||
243 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
244 | * override or modify it directly, unless you know the consequences. |
||
245 | * @param ModelEvent $event |
||
246 | */ |
||
247 | 142 | public function onInitSourceAttribute($event) |
|
253 | } |
||
254 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.