Elgg  Version 3.0
users.php
Go to the documentation of this file.
1 <?php
18  try {
20  if (!$entity) {
21  return false;
22  }
23  return _elgg_services()->entityTable->disableEntities($entity);
24  } catch (DatabaseException $ex) {
25  elgg_log($ex, 'ERROR');
26 
27  return false;
28  }
29 }
30 
40 function get_user($guid) {
41  try {
42  return _elgg_services()->entityTable->get($guid, 'user');
43  } catch (InvalidParameterException $ex) {
44  elgg_log($ex, 'ERROR');
45 
46  return false;
47  } catch (ClassException $ex) {
48  elgg_log($ex, 'ERROR');
49 
50  return false;
51  }
52 }
53 
62  return _elgg_services()->usersTable->getByUsername($username);
63 }
64 
72 function get_user_by_code($hash) {
73  return _elgg_services()->persistentLogin->getUserFromHash($hash);
74 }
75 
84  return _elgg_services()->usersTable->getByEmail($email);
85 }
86 
99 function find_active_users(array $options = []) {
100  return _elgg_services()->usersTable->findActive($options);
101 }
102 
111  return _elgg_services()->passwords->sendNewPasswordRequest($user_guid);
112 }
113 
125  return _elgg_services()->passwords->forcePasswordReset($user_guid, $password);
126 }
127 
137 function execute_new_password_request($user_guid, $conf_code, $password = null) {
138  return _elgg_services()->passwords->executeNewPasswordReset($user_guid, $conf_code, $password);
139 }
140 
147  return _elgg_services()->crypto->getRandomString(12, \ElggCrypto::CHARS_PASSWORD);
148 }
149 
161  elgg()->accounts->assertValidUsername($username);
162  return true;
163 }
164 
174  elgg()->accounts->assertValidPassword($password);
175  return true;
176 }
177 
186 function validate_email_address($address) {
187  elgg()->accounts->assertValidEmail($address);
188  return true;
189 }
190 
205 function register_user($username, $password, $name, $email, $allow_multiple_emails = false, $subtype = null) {
206  return elgg()->accounts->register($username, $password, $name, $email, $allow_multiple_emails, $subtype);
207 }
208 
222 function elgg_validate_registration_data($username, $password, $name, $email, $allow_multiple_emails = false) {
223  return elgg()->accounts->validateAccountData($username, $password, $name, $email, $allow_multiple_emails);
224 }
225 
235  return _elgg_services()->usersTable->generateInviteCode($username);
236 }
237 
249  return _elgg_services()->usersTable->validateInviteCode($username, $code);
250 }
251 
262 function elgg_get_registration_url(array $query = [], $fragment = '') {
263  $url = elgg_normalize_url(elgg_generate_url('account:register'));
265  return elgg_trigger_plugin_hook('registration_url', 'site', $query, $url);
266 }
267 
277 function elgg_get_login_url(array $query = [], $fragment = '') {
278  $url = elgg_normalize_url(elgg_generate_url('account:login'));
280  return elgg_trigger_plugin_hook('login_url', 'site', $query, $url);
281 }
282 
295 function elgg_user_hover_menu($hook, $type, $return, $params) {
296  $user = elgg_extract('entity', $params);
297  /* @var \ElggUser $user */
298 
299  if (!$user instanceof \ElggUser) {
300  return;
301  }
302 
303  if (!elgg_is_logged_in()) {
304  return;
305  }
306 
307  if ($user->canEdit()) {
308  $return[] = ElggMenuItem::factory([
309  'name' => 'avatar:edit',
310  'text' => elgg_echo('avatar:edit'),
311  'icon' => 'image',
312  'href' => elgg_generate_entity_url($user, 'edit', 'avatar'),
313  'section' => (elgg_get_logged_in_user_guid() == $user->guid)? 'action' : 'admin',
314  ]);
315  }
316 
317  // prevent admins from banning or deleting themselves
318  if (elgg_get_logged_in_user_guid() == $user->guid) {
319  return $return;
320  }
321 
322  if (!elgg_is_admin_logged_in()) {
323  return $return;
324  }
325 
326  // following items are admin only
327  if (!$user->isBanned()) {
328  $return[] = ElggMenuItem::factory([
329  'name' => 'ban',
330  'text' => elgg_echo('ban'),
331  'icon' => 'ban',
332  'href' => "action/admin/user/ban?guid={$user->guid}",
333  'confirm' => true,
334  'section' => 'admin',
335  ]);
336  } else {
337  $return[] = ElggMenuItem::factory([
338  'name' => 'unban',
339  'text' => elgg_echo('unban'),
340  'icon' => 'ban',
341  'href' => "action/admin/user/unban?guid={$user->guid}",
342  'confirm' => true,
343  'section' => 'admin',
344  ]);
345  }
346 
347  $return[] = ElggMenuItem::factory([
348  'name' => 'delete',
349  'text' => elgg_echo('delete'),
350  'icon' => 'delete',
351  'href' => "action/admin/user/delete?guid={$user->guid}",
352  'confirm' => true,
353  'section' => 'admin',
354  ]);
355 
356  $return[] = ElggMenuItem::factory([
357  'name' => 'resetpassword',
358  'text' => elgg_echo('resetpassword'),
359  'icon' => 'refresh',
360  'href' => "action/admin/user/resetpassword?guid={$user->guid}",
361  'confirm' => true,
362  'section' => 'admin',
363  ]);
364 
365  if (!$user->isAdmin()) {
366  $return[] = ElggMenuItem::factory([
367  'name' => 'makeadmin',
368  'text' => elgg_echo('makeadmin'),
369  'icon' => 'level-up',
370  'href' => "action/admin/user/makeadmin?guid={$user->guid}",
371  'confirm' => true,
372  'section' => 'admin',
373  ]);
374  } else {
375  $return[] = ElggMenuItem::factory([
376  'name' => 'removeadmin',
377  'text' => elgg_echo('removeadmin'),
378  'icon' => 'level-down',
379  'href' => "action/admin/user/removeadmin?guid={$user->guid}",
380  'confirm' => true,
381  'section' => 'admin',
382  ]);
383  }
384 
385  $return[] = ElggMenuItem::factory([
386  'name' => 'settings:edit',
387  'text' => elgg_echo('settings:edit'),
388  'icon' => 'cogs',
389  'href' => "settings/user/$user->username",
390  'section' => 'admin',
391  ]);
392 
393  return $return;
394 }
395 
406 function _elgg_user_title_menu(\Elgg\Hook $hook) {
407  $user = $hook->getEntityParam();
408  /* @var \ElggUser $user */
409 
410  if (!$user instanceof \ElggUser) {
411  return;
412  }
413 
414  if (!elgg_is_logged_in()) {
415  return;
416  }
417 
418  if (!$user->canEdit()) {
419  return;
420  }
421 
422  $return = $hook->getValue();
423  $return[] = ElggMenuItem::factory([
424  'name' => 'avatar:edit',
425  'text' => elgg_echo('avatar:edit'),
426  'icon' => 'image',
427  'class' => ['elgg-button', 'elgg-button-action'],
428  'href' => elgg_generate_entity_url($user, 'edit', 'avatar'),
429  ]);
430 
431  return $return;
432 }
433 
447 function _elgg_user_page_menu($hook, $type, $return, $params) {
448 
450 
451  if (!$owner instanceof ElggUser) {
452  return;
453  }
454 
455  if (!elgg_is_logged_in()) {
456  return;
457  }
458 
459  if (!$owner->canEdit()) {
460  return;
461  }
462 
463  $return[] = \ElggMenuItem::factory([
464  'name' => 'edit_avatar',
465  'href' => elgg_generate_entity_url($owner, 'edit', 'avatar'),
466  'text' => elgg_echo('avatar:edit'),
467  'section' => '1_profile',
468  'contexts' => ['settings'],
469  ]);
470 
471  return $return;
472 }
473 
487 function _elgg_user_topbar_menu($hook, $type, $return, $params) {
488 
489  $viewer = elgg_get_logged_in_user_entity();
490  if (!$viewer) {
491  return;
492  }
493 
494  $return[] = \ElggMenuItem::factory([
495  'name' => 'account',
496  'text' => elgg_echo('account'),
497  'href' => $viewer->getURL(),
498  'link_class' => 'elgg-avatar-small',
499  'icon' => elgg_view('output/img', [
500  'src' => $viewer->getIconURL('small'),
501  'alt' => $viewer->getDisplayName(),
502  ]),
503  'icon_alt' => 'angle-down',
504  'priority' => 800,
505  'section' => 'alt',
506  ]);
507 
508  $return[] = \ElggMenuItem::factory([
509  'name' => 'usersettings',
510  'parent_name' => 'account',
511  'href' => "settings/user/{$viewer->username}",
512  'text' => elgg_echo('settings'),
513  'icon' => 'sliders',
514  'priority' => 300,
515  'section' => 'alt',
516  ]);
517 
518  if ($viewer->isAdmin()) {
519  $return[] = \ElggMenuItem::factory([
520  'name' => 'administration',
521  'parent_name' => 'account',
522  'href' => 'admin',
523  'text' => elgg_echo('admin'),
524  'icon' => 'cogs',
525  'priority' => 800,
526  'section' => 'alt',
527  ]);
528  }
529 
530  $return[] = \ElggMenuItem::factory([
531  'name' => 'logout',
532  'parent_name' => 'account',
533  'href' => 'action/logout',
534  'text' => elgg_echo('logout'),
535  'icon' => 'sign-out',
536  'is_action' => true,
537  'priority' => 900,
538  'section' => 'alt',
539  ]);
540 
541  return $return;
542 }
543 
554 
555  $entity = elgg_extract('entity', $params);
556  $size = elgg_extract('size', $params, 'medium');
557 
558  $icon->owner_guid = $entity->guid;
559  $icon->setFilename("profile/{$entity->guid}{$size}.jpg");
560 
561  return $icon;
562 }
563 
574 function _elgg_user_get_subscriber_unban_action($hook, $type, $return_value, $params) {
575 
576  if (!_elgg_config()->security_notify_user_ban) {
577  return;
578  }
579 
580  $event = elgg_extract('event', $params);
581  if (!$event instanceof \Elgg\Notifications\SubscriptionNotificationEvent) {
582  return;
583  }
584 
585  if ($event->getAction() !== 'unban') {
586  return;
587  }
588 
589  $user = $event->getObject();
590  if (!$user instanceof \ElggUser) {
591  return;
592  }
593 
594  $return_value[$user->guid] = ['email'];
595 
596  return $return_value;
597 }
598 
611 
612  if (!_elgg_config()->security_notify_user_ban) {
613  return;
614  }
615 
616  if (!($user instanceof \ElggUser)) {
617  return;
618  }
619 
621  $language = $user->getLanguage();
622 
623  $subject = elgg_echo('user:notification:ban:subject', [$site->getDisplayName()], $language);
624  $body = elgg_echo('user:notification:ban:body', [
625  $user->getDisplayName(),
626  $site->getDisplayName(),
627  $site->getURL(),
628  ], $language);
629 
630  $params = [
631  'action' => 'ban',
632  'object' => $user,
633  ];
634 
635  notify_user($user->getGUID(), $site->getGUID(), $subject, $body, $params, ['email']);
636 }
637 
648 function _elgg_user_prepare_unban_notification($hook, $type, $return_value, $params) {
649 
650  if (!($return_value instanceof \Elgg\Notifications\Notification)) {
651  return;
652  }
653 
654  $recipient = elgg_extract('recipient', $params);
655  $object = elgg_extract('object', $params);
656  $language = elgg_extract('language', $params);
657 
658  if (!($recipient instanceof ElggUser) || !($object instanceof ElggUser)) {
659  return;
660  }
661 
662  if ($recipient->getGUID() !== $object->getGUID()) {
663  return;
664  }
665 
667 
668  $return_value->subject = elgg_echo('user:notification:unban:subject', [$site->getDisplayName()], $language);
669  $return_value->body = elgg_echo('user:notification:unban:body', [
670  $recipient->getDisplayName(),
671  $site->getDisplayName(),
672  $site->getURL(),
673  ], $language);
674 
675  $return_value->url = $recipient->getURL();
676 
677  return $return_value;
678 }
679 
692 function _elgg_user_unvalidated_menu(\Elgg\Hook $hook) {
693 
694  if (!elgg_is_admin_logged_in()) {
695  return;
696  }
697 
698  $entity = $hook->getEntityParam();
699  if (!$entity instanceof ElggUser || $entity->isValidated()) {
700  return;
701  }
702 
703  $return = $hook->getValue();
704 
705  $return[] = ElggMenuItem::factory([
706  'name' => 'validate',
707  'text' => elgg_echo('validate'),
708  'href' => elgg_http_add_url_query_elements('action/admin/user/validate', [
709  'user_guid' => $entity->guid,
710  ]),
711  'confirm' => true,
712  'priority' => 400,
713  ]);
714 
715  $return[] = ElggMenuItem::factory([
716  'name' => 'delete',
717  'text' => elgg_echo('delete'),
718  'href' => elgg_http_add_url_query_elements('action/admin/user/delete', [
719  'guid' => $entity->guid,
720  ]),
721  'confirm' => elgg_echo('deleteconfirm'),
722  'priority' => 500,
723  ]);
724 
725  return $return;
726 }
727 
734 function users_init() {
735 
736  elgg_register_plugin_hook_handler('register', 'menu:user_hover', 'elgg_user_hover_menu');
737  elgg_register_plugin_hook_handler('register', 'menu:title', '_elgg_user_title_menu');
738  elgg_register_plugin_hook_handler('register', 'menu:page', '_elgg_user_page_menu');
739  elgg_register_plugin_hook_handler('register', 'menu:topbar', '_elgg_user_topbar_menu');
740  elgg_register_plugin_hook_handler('register', 'menu:user:unvalidated', '_elgg_user_unvalidated_menu');
741 
742  // Register the user type
743  elgg_register_entity_type('user', 'user');
744 
745  elgg_register_plugin_hook_handler('entity:icon:file', 'user', '_elgg_user_set_icon_file');
746 
747  elgg_register_notification_event('user', 'user', ['unban']);
748  elgg_register_plugin_hook_handler('get', 'subscriptions', '_elgg_user_get_subscriber_unban_action');
749  elgg_register_event_handler('ban', 'user', '_elgg_user_ban_notification');
750  elgg_register_plugin_hook_handler('prepare', 'notification:unban:user:user', '_elgg_user_prepare_unban_notification');
751 
752 }
753 
757 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
758  $events->registerHandler('init', 'system', 'users_init', 0);
759 };
force_user_password_reset($user_guid, $password)
Low level function to reset a given user&#39;s password.
Definition: users.php:124
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:942
elgg_validate_registration_data($username, $password, $name, $email, $allow_multiple_emails=false)
Assert that given registration details are valid and can be used to register the user.
Definition: users.php:222
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:48
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$username
Definition: delete.php:23
$params
Saves global plugin settings.
Definition: save.php:13
get_user($guid)
Get a user object from a GUID.
Definition: users.php:40
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:57
elgg_normalize_url($url)
Definition: output.php:186
get_user_by_email($email)
Get an array of users from an email address.
Definition: users.php:83
const CHARS_PASSWORD
Character set for temp passwords (no risk of embedded profanity/glyphs that look similar) ...
Definition: ElggCrypto.php:13
disable_user_entities($owner_guid)
Disables all of a user&#39;s entities.
Definition: users.php:17
validate_email_address($address)
Simple validation of a email.
Definition: users.php:186
$size
Definition: remove.php:24
_elgg_user_unvalidated_menu(\Elgg\Hook $hook)
Register menu items to the user:unvalidated menu.
Definition: users.php:692
Events service.
$subtype
Definition: delete.php:22
_elgg_user_title_menu(\Elgg\Hook $hook)
Adds avatar edit button to title menu.
Definition: users.php:406
$guid
Removes an admin notice.
users_init()
Users initialisation function, which establishes the page handler.
Definition: users.php:734
send_new_password_request($user_guid)
Generate and send a password request email to a given user&#39;s registered email address.
Definition: users.php:110
$type
Definition: delete.php:21
$email
Definition: register.php:18
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
_elgg_user_get_subscriber_unban_action($hook, $type, $return_value, $params)
Add the user to the subscribers when (un)banning the account.
Definition: users.php:574
$options
Definition: users.php:9
_elgg_user_prepare_unban_notification($hook, $type, $return_value, $params)
Prepare the notification content for the user being unbanned.
Definition: users.php:648
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_user_hover_menu($hook, $type, $return, $params)
Setup the default user hover menu.
Definition: users.php:295
notify_user($to, $from=0, $subject= '', $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
_elgg_user_set_icon_file($hook, $type, $icon, $params)
Set user icon file.
Definition: users.php:553
if(!$owner) $icon
Definition: default.php:16
$code
get_user_by_username($username)
Get user by username.
Definition: users.php:61
static factory($options)
Create an ElggMenuItem from an associative array.
$user_guid
Validate a user.
Definition: validate.php:6
$owner_guid
Configuration exception.
generate_invite_code($username)
Generates a unique invite code for a user.
Definition: users.php:234
if(!$user||!$user->canEdit()) $password
Base class for events and hooks.
_elgg_user_ban_notification($event, $type, $user)
Send a notification to the user that the account was banned.
Definition: users.php:610
$entity
Definition: reset.php:8
$owner
Definition: crop.php:7
$language
Definition: useradd.php:17
if(elgg_extract('friends_only', $vars, false)) if(!(bool) elgg_extract('include_banned', $vars, true)) $body
Definition: users.php:51
get_user_by_code($hash)
Get user by persistent login password.
Definition: users.php:72
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
Definition: pagehandler.php:58
$user
Definition: ban.php:7
$url
Definition: default.php:33
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
_elgg_user_topbar_menu($hook, $type, $return, $params)
Register menu items for the topbar menu.
Definition: users.php:487
validate_password($password)
Simple validation of a password.
Definition: users.php:173
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:65
$query
Definition: users.php:6
validate_username($username)
Simple function which ensures that a username contains only valid characters.
Definition: users.php:160
register_user($username, $password, $name, $email, $allow_multiple_emails=false, $subtype=null)
Registers a user, returning false if the username already exists.
Definition: users.php:205
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg_get_registration_url(array $query=[], $fragment= '')
Returns site&#39;s registration URL Triggers a &#39;registration_url&#39;, &#39;site&#39; plugin hook that can be used by...
Definition: users.php:262
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
_elgg_config()
Get the Elgg config service.
generate_random_cleartext_password()
Generate a random 12 character clear text password.
Definition: users.php:146
elgg_register_notification_event($object_type, $object_subtype, array $actions=[])
Register a notification event.
_elgg_user_page_menu($hook, $type, $return, $params)
Register menu items for the page menu.
Definition: users.php:447
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:29
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:657
elgg_validate_invite_code($username, $code)
Validate a user&#39;s invite code.
Definition: users.php:248
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246
elgg_generate_entity_url(ElggEntity $entity, $resource= 'view', $subresource=null, array $parameters=[])
Generate entity URL from a named route.
Definition: pagehandler.php:84
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:39
$subject
Definition: useradd.php:59
find_active_users(array $options=[])
Return users (or the number of them) who have been active within a recent period. ...
Definition: users.php:99
var elgg
Definition: elgglib.js:4
elgg_get_login_url(array $query=[], $fragment= '')
Returns site&#39;s login URL Triggers a &#39;login_url&#39;, &#39;site&#39; plugin hook that can be used by plugins to al...
Definition: users.php:277
execute_new_password_request($user_guid, $conf_code, $password=null)
Validate and change password for a user.
Definition: users.php:137
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87