Elgg  Version 6.3
Validation.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Users;
4 
7 
13 class Validation {
14 
22  public static function addRiverActivityAfterValidation(\Elgg\Event $event) {
23  if (!(bool) elgg_get_config('user_joined_river')) {
24  return;
25  }
26 
28  'action_type' => 'join',
29  'subject_guid' => $event->getObject()->guid,
30  'object_guid' => elgg_get_site_entity()->guid,
31  ]);
32  }
33 
41  public static function checkAdminValidation(\Elgg\Event $event) {
42  if (!(bool) elgg_get_config('require_admin_validation')) {
43  return;
44  }
45 
46  $user = $event->getUserParam();
47  if (!$user instanceof \ElggUser) {
48  return;
49  }
50 
52  if ($user->isEnabled()) {
53  // disable the user until validation
54  $user->disable('admin_validation_required', false);
55  }
56 
57  // set validation status
58  $user->setValidationStatus(false);
59 
60  // store a flag in session so we can forward the user correctly
62  $session->set('admin_validation', true);
63 
64  if ((bool) elgg_get_config('admin_validation_notification')) {
65  _elgg_services()->notifications->enqueueEvent('admin_validation', $user, elgg_get_site_entity());
66  }
67  });
68  }
69 
79  public static function preventUserLogin(\Elgg\Event $event) {
80  if (!(bool) elgg_get_config('require_admin_validation')) {
81  return;
82  }
83 
84  $user = $event->getObject();
85  if (!$user instanceof \ElggUser) {
86  return;
87  }
88 
89  elgg_call(ELGG_SHOW_DISABLED_ENTITIES, function() use ($user) {
90  if ($user->isEnabled() && $user->isValidated() !== false) {
91  return;
92  }
93 
94  throw new LoginException(elgg_echo('LoginException:AdminValidationPending'));
95  });
96  }
97 
105  public static function setRegistrationForwardUrl(\Elgg\Event $event) {
106  $response = $event->getValue();
107  if (!$response instanceof ResponseBuilder) {
108  return;
109  }
110 
112  if (!$session->get('admin_validation')) {
113  return;
114  }
115 
116  // if other plugins already have set forwarding, don't do anything
117  if (!empty($response->getForwardURL()) && $response->getForwardURL() !== REFERRER) {
118  return;
119  }
120 
121  $response->setForwardURL(elgg_generate_url('account:validation:pending'));
122 
123  return $response;
124  }
125 
134  public static function removeUnvalidatedUsers(\Elgg\Event $event): void {
135  $days = (int) elgg_get_config('remove_unvalidated_users_days');
136  if ($days < 1) {
137  return;
138  }
139 
140  // removing users could take a while
141  set_time_limit(0);
142 
143  elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() use ($days) {
144  /* @var $users \ElggBatch */
146  'type' => 'user',
147  'metadata_name_value_pairs' => [
148  'validated' => false,
149  ],
150  'created_before' => "-{$days} days",
151  'limit' => false,
152  'batch' => true,
153  'batch_inc_offset' => false,
154  ]);
155 
156  /* @var $user \ElggUser */
157  foreach ($users as $user) {
158  if (!$user->delete()) {
159  // make sure the batch skips over the failed user in the next iteration
160  $users->reportFailure();
161  }
162  }
163  });
164  }
165 }
$user
Definition: ban.php:7
if(empty($user_guids)) $users
Definition: ban.php:12
Models an event passed to event handlers.
Definition: Event.php:11
Generic parent class for login exceptions.
User Validation related events.
Definition: Validation.php:13
static setRegistrationForwardUrl(\Elgg\Event $event)
Set the correct forward url after user registration.
Definition: Validation.php:105
static removeUnvalidatedUsers(\Elgg\Event $event)
Remove unvalidated users after x days.
Definition: Validation.php:134
static preventUserLogin(\Elgg\Event $event)
Prevent unvalidated users from logging in.
Definition: Validation.php:79
static checkAdminValidation(\Elgg\Event $event)
Check if new users need to be validated by an administrator.
Definition: Validation.php:41
static addRiverActivityAfterValidation(\Elgg\Event $event)
Adds river activity that a new user joined the site.
Definition: Validation.php:22
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page.
Definition: constants.php:37
_elgg_services()
Get the global service provider.
Definition: elgglib.php:337
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:290
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
HTTP response builder interface.
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
elgg_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
elgg_get_session()
Gets Elgg's session object.
Definition: sessions.php:15
$response
Definition: content.php:10