1 | <?php |
||
42 | class User extends ActiveRecord implements CSSClassNames |
||
43 | { |
||
44 | use IcybeeBindings, RegistryBindings; |
||
45 | use CreatedAtProperty, LoggedAtProperty, CSSClassNamesProperty; |
||
46 | use PasswordTrait; |
||
47 | |||
48 | const MODEL_ID = 'users'; |
||
49 | |||
50 | const UID = 'uid'; |
||
51 | const EMAIL = 'email'; |
||
52 | const PASSWORD = 'password'; |
||
53 | const PASSWORD_HASH = 'password_hash'; |
||
54 | const PASSWORD_VERIFY = 'password-verify'; |
||
55 | const USERNAME = 'username'; |
||
56 | const FIRSTNAME = 'firstname'; |
||
57 | const LASTNAME = 'lastname'; |
||
58 | const NICKNAME = 'nickname'; |
||
59 | const CREATED_AT = 'created_at'; |
||
60 | const LOGGED_AT = 'logged_at'; |
||
61 | const CONSTRUCTOR = 'constructor'; |
||
62 | const LANGUAGE = 'language'; |
||
63 | const TIMEZONE = 'timezone'; |
||
64 | const IS_ACTIVATED = 'is_activated'; |
||
65 | const ROLES = 'roles'; |
||
66 | const RESTRICTED_SITES = 'restricted_sites'; |
||
67 | |||
68 | const NAME_AS = 'name_as'; |
||
69 | |||
70 | /** |
||
71 | * The {@link $name} property should be created from `$username`. |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | const NAME_AS_USERNAME = 0; |
||
76 | |||
77 | /** |
||
78 | * The {@link $name} property should be created from `$firstname`. |
||
79 | * |
||
80 | * @var int |
||
81 | */ |
||
82 | const NAME_AS_FIRSTNAME = 1; |
||
83 | |||
84 | /** |
||
85 | * The {@link $name} property should be created from `$lastname`. |
||
86 | * |
||
87 | * @var int |
||
88 | */ |
||
89 | const NAME_AS_LASTNAME = 2; |
||
90 | |||
91 | /** |
||
92 | * The {@link $name} property should be created from `$firstname $lastname`. |
||
93 | * |
||
94 | * @var int |
||
95 | */ |
||
96 | const NAME_AS_FIRSTNAME_LASTNAME = 3; |
||
97 | |||
98 | /** |
||
99 | * The {@link $name} property should be created from `$lastname $firstname`. |
||
100 | * |
||
101 | * @var int |
||
102 | */ |
||
103 | const NAME_AS_LASTNAME_FIRSTNAME = 4; |
||
104 | |||
105 | /** |
||
106 | * The {@link $name} property should be created from `$nickname`. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | const NAME_AS_NICKNAME = 5; |
||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | * |
||
115 | * The method takes care of setting the {@link password_hash} property which is not |
||
116 | * settable otherwise. |
||
117 | * |
||
118 | * @return static |
||
119 | */ |
||
120 | static public function from($properties = null, array $construct_args = [], $class_name = null) |
||
134 | |||
135 | /** |
||
136 | * User identifier. |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | public $uid; |
||
141 | |||
142 | /** |
||
143 | * Constructor of the user record (module id). |
||
144 | * |
||
145 | * The property MUST be defined to persist the record. |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | public $constructor; |
||
150 | |||
151 | /** |
||
152 | * User email. |
||
153 | * |
||
154 | * The property MUST be defined to persist the record. |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | public $email; |
||
159 | |||
160 | /** |
||
161 | * Username of the user. |
||
162 | * |
||
163 | * The property MUST be defined to persist the record. |
||
164 | * |
||
165 | * @var string |
||
166 | */ |
||
167 | public $username; |
||
168 | |||
169 | /** |
||
170 | * First name of the user. |
||
171 | * |
||
172 | * @var string |
||
173 | */ |
||
174 | public $firstname = ''; |
||
175 | |||
176 | /** |
||
177 | * Last name of the user. |
||
178 | * |
||
179 | * @var string |
||
180 | */ |
||
181 | public $lastname = ''; |
||
182 | |||
183 | /** |
||
184 | * Nickname of the user. |
||
185 | * |
||
186 | * @var string |
||
187 | */ |
||
188 | public $nickname = ''; |
||
189 | |||
190 | /** |
||
191 | * Preferred format to create the value of the {@link $name} property. |
||
192 | * |
||
193 | * @var string |
||
194 | */ |
||
195 | public $name_as = self::NAME_AS_USERNAME; |
||
196 | |||
197 | /** |
||
198 | * Preferred language of the user. |
||
199 | * |
||
200 | * @var string |
||
201 | */ |
||
202 | public $language = ''; |
||
203 | |||
204 | /** |
||
205 | * Preferred timezone of the user. |
||
206 | * |
||
207 | * @var string |
||
208 | */ |
||
209 | public $timezone = ''; |
||
210 | |||
211 | /** |
||
212 | * State of the user account activation. |
||
213 | * |
||
214 | * @var bool |
||
215 | */ |
||
216 | public $is_activated = false; |
||
217 | |||
218 | /** |
||
219 | * If empty, the {@link $constructor} property is initialized with the model identifier. |
||
220 | * |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | public function __construct($model = null) |
||
232 | |||
233 | /** |
||
234 | * @inheritdoc |
||
235 | */ |
||
236 | public function __get($property) |
||
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | */ |
||
251 | public function create_validation_rules() |
||
261 | |||
262 | /** |
||
263 | * @inheritdoc |
||
264 | */ |
||
265 | public function save(array $options = []) |
||
274 | |||
275 | /** |
||
276 | * Adds the {@link $password_hash} property. |
||
277 | */ |
||
278 | public function to_array() |
||
289 | |||
290 | /** |
||
291 | * Returns the formatted name of the user. |
||
292 | * |
||
293 | * The format of the name is defined by the {@link $name_as} property. The {@link $username}, |
||
294 | * {@link $firstname}, {@link $lastname} and {@link $nickname} properties can be used to |
||
295 | * format the name. |
||
296 | * |
||
297 | * This is the getter for the {@link $name} magic property. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function get_name() |
||
323 | |||
324 | /** |
||
325 | * Returns the role of the user. |
||
326 | * |
||
327 | * This is the getter for the {@link $role} magic property. |
||
328 | * |
||
329 | * @return Role |
||
330 | */ |
||
331 | protected function lazy_get_role() |
||
356 | |||
357 | /** |
||
358 | * Returns all the roles associated with the user. |
||
359 | * |
||
360 | * This is the getter for the {@link $roles} magic property. |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | protected function lazy_get_roles() |
||
401 | |||
402 | /** |
||
403 | * Checks if the user is the admin user. |
||
404 | * |
||
405 | * This is the getter for the {@link $is_admin} magic property. |
||
406 | * |
||
407 | * @return boolean `true` if the user is the admin user, `false` otherwise. |
||
408 | */ |
||
409 | protected function get_is_admin() |
||
413 | |||
414 | /** |
||
415 | * Checks if the user is a guest user. |
||
416 | * |
||
417 | * This is the getter for the {@link $is_guest} magic property. |
||
418 | * |
||
419 | * @return boolean `true` if the user is a guest user, `false` otherwise. |
||
420 | */ |
||
421 | protected function get_is_guest() |
||
425 | |||
426 | /** |
||
427 | * Returns the ids of the sites the user is restricted to. |
||
428 | * |
||
429 | * This is the getter for the {@link $restricted_sites_ids} magic property. |
||
430 | * |
||
431 | * @return array The array is empty if the user has no site restriction. |
||
432 | */ |
||
433 | protected function lazy_get_restricted_sites_ids() |
||
445 | |||
446 | /** |
||
447 | * Checks if the user has a given permission. |
||
448 | * |
||
449 | * @param string|int $permission |
||
450 | * @param mixed $target |
||
451 | * |
||
452 | * @return mixed |
||
453 | */ |
||
454 | public function has_permission($permission, $target = null) |
||
463 | |||
464 | /** |
||
465 | * Checks if the user has the ownership of an entry. |
||
466 | * |
||
467 | * If the ownership information is missing from the entry (the 'uid' property is null), the user |
||
468 | * must have the ADMINISTER level to be considered the owner. |
||
469 | * |
||
470 | * @param ActiveRecord $record |
||
471 | * |
||
472 | * @return boolean |
||
473 | */ |
||
474 | public function has_ownership($record) |
||
478 | |||
479 | /** |
||
480 | * Logs the user in. |
||
481 | * |
||
482 | * A user is logged in by setting its id in the `user_id` session key. |
||
483 | * |
||
484 | * Note: The method does *not* check user authentication! |
||
485 | * |
||
486 | * The following things happen when the user is logged in: |
||
487 | * |
||
488 | * - The `$app->user` property is set to the user. |
||
489 | * - The `$app->user_id` property is set to the user id. |
||
490 | * - The session id is regenerated and the user id, ip and user agent are stored in the session. |
||
491 | * |
||
492 | * @throws \Exception in attempt to log in a guest user. |
||
493 | * |
||
494 | * @see \Icybee\Modules\Users\Hooks\get_user_id |
||
495 | */ |
||
496 | public function login() |
||
509 | |||
510 | /** |
||
511 | * Log the user out. |
||
512 | * |
||
513 | * The following things happen when the user is logged out: |
||
514 | * |
||
515 | * - The `$app->user` property is unset. |
||
516 | * - The `$app->user_id` property is unset. |
||
517 | * - The `user_id` session property is removed. |
||
518 | */ |
||
519 | public function logout() |
||
528 | |||
529 | /** |
||
530 | * @inheritdoc |
||
531 | */ |
||
532 | protected function get_css_class_names() |
||
546 | } |
||
547 |