1 | <?php |
||
38 | class User extends ActiveRecord implements CSSClassNames |
||
39 | { |
||
40 | use IcybeeBindings, RegistryBindings; |
||
41 | use CreatedAtProperty, LoggedAtProperty, CSSClassNamesProperty; |
||
42 | use PasswordTrait; |
||
43 | |||
44 | const MODEL_ID = 'users'; |
||
45 | |||
46 | const UID = 'uid'; |
||
47 | const EMAIL = 'email'; |
||
48 | const PASSWORD = 'password'; |
||
49 | const PASSWORD_HASH = 'password_hash'; |
||
50 | const PASSWORD_VERIFY = 'password-verify'; |
||
51 | const USERNAME = 'username'; |
||
52 | const FIRSTNAME = 'firstname'; |
||
53 | const LASTNAME = 'lastname'; |
||
54 | const NICKNAME = 'nickname'; |
||
55 | const CREATED_AT = 'created_at'; |
||
56 | const LOGGED_AT = 'logged_at'; |
||
57 | const CONSTRUCTOR = 'constructor'; |
||
58 | const LANGUAGE = 'language'; |
||
59 | const TIMEZONE = 'timezone'; |
||
60 | const IS_ACTIVATED = 'is_activated'; |
||
61 | const ROLES = 'roles'; |
||
62 | const RESTRICTED_SITES = 'restricted_sites'; |
||
63 | |||
64 | const NAME_AS = 'name_as'; |
||
65 | |||
66 | /** |
||
67 | * The {@link $name} property should be created from `$username`. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | const NAME_AS_USERNAME = 0; |
||
72 | |||
73 | /** |
||
74 | * The {@link $name} property should be created from `$firstname`. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | const NAME_AS_FIRSTNAME = 1; |
||
79 | |||
80 | /** |
||
81 | * The {@link $name} property should be created from `$lastname`. |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | const NAME_AS_LASTNAME = 2; |
||
86 | |||
87 | /** |
||
88 | * The {@link $name} property should be created from `$firstname $lastname`. |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | const NAME_AS_FIRSTNAME_LASTNAME = 3; |
||
93 | |||
94 | /** |
||
95 | * The {@link $name} property should be created from `$lastname $firstname`. |
||
96 | * |
||
97 | * @var int |
||
98 | */ |
||
99 | const NAME_AS_LASTNAME_FIRSTNAME = 4; |
||
100 | |||
101 | /** |
||
102 | * The {@link $name} property should be created from `$nickname`. |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | const NAME_AS_NICKNAME = 5; |
||
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | * |
||
111 | * The method takes care of setting the {@link password_hash} property which is not |
||
112 | * settable otherwise. |
||
113 | * |
||
114 | * @return static |
||
115 | */ |
||
116 | static public function from($properties = null, array $construct_args = [], $class_name = null) |
||
130 | |||
131 | /** |
||
132 | * User identifier. |
||
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | public $uid; |
||
137 | |||
138 | /** |
||
139 | * Constructor of the user record (module id). |
||
140 | * |
||
141 | * The property MUST be defined to persist the record. |
||
142 | * |
||
143 | * @var string |
||
144 | */ |
||
145 | public $constructor; |
||
146 | |||
147 | /** |
||
148 | * User email. |
||
149 | * |
||
150 | * The property MUST be defined to persist the record. |
||
151 | * |
||
152 | * @var string |
||
153 | */ |
||
154 | public $email; |
||
155 | |||
156 | /** |
||
157 | * Username of the user. |
||
158 | * |
||
159 | * The property MUST be defined to persist the record. |
||
160 | * |
||
161 | * @var string |
||
162 | */ |
||
163 | public $username; |
||
164 | |||
165 | /** |
||
166 | * First name of the user. |
||
167 | * |
||
168 | * @var string |
||
169 | */ |
||
170 | public $firstname = ''; |
||
171 | |||
172 | /** |
||
173 | * Last name of the user. |
||
174 | * |
||
175 | * @var string |
||
176 | */ |
||
177 | public $lastname = ''; |
||
178 | |||
179 | /** |
||
180 | * Nickname of the user. |
||
181 | * |
||
182 | * @var string |
||
183 | */ |
||
184 | public $nickname = ''; |
||
185 | |||
186 | /** |
||
187 | * Preferred format to create the value of the {@link $name} property. |
||
188 | * |
||
189 | * @var string |
||
190 | */ |
||
191 | public $name_as = self::NAME_AS_USERNAME; |
||
192 | |||
193 | /** |
||
194 | * Preferred language of the user. |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | public $language = ''; |
||
199 | |||
200 | /** |
||
201 | * Preferred timezone of the user. |
||
202 | * |
||
203 | * @var string |
||
204 | */ |
||
205 | public $timezone = ''; |
||
206 | |||
207 | /** |
||
208 | * State of the user account activation. |
||
209 | * |
||
210 | * @var bool |
||
211 | */ |
||
212 | public $is_activated = false; |
||
213 | |||
214 | /** |
||
215 | * If empty, the {@link $constructor} property is initialized with the model identifier. |
||
216 | * |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | public function __construct($model = null) |
||
228 | |||
229 | /** |
||
230 | * @inheritdoc |
||
231 | */ |
||
232 | public function __get($property) |
||
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | public function create_validation_rules() |
||
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | public function save(array $options = []) |
||
270 | |||
271 | /** |
||
272 | * Adds the {@link $password_hash} property. |
||
273 | */ |
||
274 | public function to_array() |
||
285 | |||
286 | /** |
||
287 | * Returns the formatted name of the user. |
||
288 | * |
||
289 | * The format of the name is defined by the {@link $name_as} property. The {@link $username}, |
||
290 | * {@link $firstname}, {@link $lastname} and {@link $nickname} properties can be used to |
||
291 | * format the name. |
||
292 | * |
||
293 | * This is the getter for the {@link $name} magic property. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | protected function get_name() |
||
319 | |||
320 | /** |
||
321 | * Checks if the user is the admin user. |
||
322 | * |
||
323 | * This is the getter for the {@link $is_admin} magic property. |
||
324 | * |
||
325 | * @return boolean `true` if the user is the admin user, `false` otherwise. |
||
326 | */ |
||
327 | protected function get_is_admin() |
||
331 | |||
332 | /** |
||
333 | * Checks if the user is a guest user. |
||
334 | * |
||
335 | * This is the getter for the {@link $is_guest} magic property. |
||
336 | * |
||
337 | * @return boolean `true` if the user is a guest user, `false` otherwise. |
||
338 | */ |
||
339 | protected function get_is_guest() |
||
343 | |||
344 | /** |
||
345 | * Returns the ids of the sites the user is restricted to. |
||
346 | * |
||
347 | * This is the getter for the {@link $restricted_sites_ids} magic property. |
||
348 | * |
||
349 | * @return array The array is empty if the user has no site restriction. |
||
350 | */ |
||
351 | protected function lazy_get_restricted_sites_ids() |
||
363 | |||
364 | /** |
||
365 | * Checks if the user has a given permission. |
||
366 | * |
||
367 | * @param string|int $permission |
||
368 | * @param mixed $target |
||
369 | * |
||
370 | * @return mixed |
||
371 | */ |
||
372 | public function has_permission($permission, $target = null) |
||
381 | |||
382 | /** |
||
383 | * Checks if the user has the ownership of an entry. |
||
384 | * |
||
385 | * If the ownership information is missing from the entry (the 'uid' property is null), the user |
||
386 | * must have the ADMINISTER level to be considered the owner. |
||
387 | * |
||
388 | * @param ActiveRecord $record |
||
389 | * |
||
390 | * @return boolean |
||
391 | */ |
||
392 | public function has_ownership($record) |
||
396 | |||
397 | /** |
||
398 | * Logs the user in. |
||
399 | * |
||
400 | * A user is logged in by setting its id in the `user_id` session key. |
||
401 | * |
||
402 | * Note: The method does *not* check user authentication! |
||
403 | * |
||
404 | * The following things happen when the user is logged in: |
||
405 | * |
||
406 | * - The `$app->user` property is set to the user. |
||
407 | * - The `$app->user_id` property is set to the user id. |
||
408 | * - The session id is regenerated and the user id, ip and user agent are stored in the session. |
||
409 | * |
||
410 | * @throws \Exception in attempt to log in a guest user. |
||
411 | * |
||
412 | * @see \Icybee\Modules\Users\Hooks\get_user_id |
||
413 | */ |
||
414 | public function login() |
||
427 | |||
428 | /** |
||
429 | * Log the user out. |
||
430 | * |
||
431 | * The following things happen when the user is logged out: |
||
432 | * |
||
433 | * - The `$app->user` property is unset. |
||
434 | * - The `$app->user_id` property is unset. |
||
435 | * - The `user_id` session property is removed. |
||
436 | */ |
||
437 | public function logout() |
||
446 | |||
447 | /** |
||
448 | * @inheritdoc |
||
449 | */ |
||
450 | protected function get_css_class_names() |
||
464 | } |
||
465 |