Elgg  Version master
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 
43  if (!(bool) elgg_get_config('require_admin_validation')) {
44  return;
45  }
46 
47  $user = $event->getUserParam();
48  if (!$user instanceof \ElggUser) {
49  return;
50  }
51 
53 
54  if ($user->isEnabled()) {
55  // disable the user until validation
56  $user->disable('admin_validation_required', false);
57  }
58 
59  // set validation status
60  $user->setValidationStatus(false);
61 
62  // store a flag in session so we can forward the user correctly
64  $session->set('admin_validation', true);
65 
66  if (elgg_get_config('admin_validation_notification') === 'direct') {
67  self::notifyAdminsAboutPendingUsers($event);
68  }
69  });
70  }
71 
79  public static function notifyAdminsAboutPendingUsers(\Elgg\Event $event) {
80 
81  if (empty(elgg_get_config('admin_validation_notification'))) {
82  return;
83  }
84 
85  $unvalidated_count = elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() {
86  return elgg_count_entities([
87  'type' => 'user',
88  'metadata_name_value_pairs' => [
89  'validated' => 0,
90  ],
91  ]);
92  });
93  if (empty($unvalidated_count)) {
94  // shouldn't be able to get here because this function is triggered when a user is marked as unvalidated
95  return;
96  }
97 
99  $admins = elgg_get_admins([
100  'limit' => false,
101  'batch' => true,
102  ]);
103 
104  $url = elgg_normalize_url('admin/users/unvalidated');
105 
106  /* @var $admin \ElggUser */
107  foreach ($admins as $admin) {
108  $user_setting = $admin->admin_validation_notification;
109  if (isset($user_setting) && !(bool) $user_setting) {
110  continue;
111  }
112 
113  $subject = elgg_echo('admin:notification:unvalidated_users:subject', [$site->getDisplayName()], $admin->getLanguage());
114  $body = elgg_echo('admin:notification:unvalidated_users:body', [
115  $unvalidated_count,
116  $site->getDisplayName(),
117  $url,
118  ], $admin->getLanguage());
119 
120  $params = [
121  'action' => 'admin:unvalidated',
122  'object' => $admin,
123  ];
124  notify_user($admin->guid, $site->guid, $subject, $body, $params, ['email']);
125  }
126  }
127 
137  public static function preventUserLogin(\Elgg\Event $event) {
138 
139  if (!(bool) elgg_get_config('require_admin_validation')) {
140  return;
141  }
142 
143  $user = $event->getObject();
144  if (!$user instanceof \ElggUser) {
145  return;
146  }
147 
148  elgg_call(ELGG_SHOW_DISABLED_ENTITIES, function() use ($user) {
149  if ($user->isEnabled() && $user->isValidated() !== false) {
150  return;
151  }
152 
153  throw new LoginException(elgg_echo('LoginException:AdminValidationPending'));
154  });
155  }
156 
164  public static function setRegistrationForwardUrl(\Elgg\Event $event) {
165 
166  $response = $event->getValue();
167  if (!$response instanceof ResponseBuilder) {
168  return;
169  }
170 
172  if (!$session->get('admin_validation')) {
173  return;
174  }
175 
176  // if other plugins already have set forwarding, don't do anything
177  if (!empty($response->getForwardURL()) && $response->getForwardURL() !== REFERRER) {
178  return;
179  }
180 
181  $response->setForwardURL(elgg_generate_url('account:validation:pending'));
182 
183  return $response;
184  }
185 
194  public static function removeUnvalidatedUsers(\Elgg\Event $event): void {
195 
196  $days = (int) elgg_get_config('remove_unvalidated_users_days');
197  if ($days < 1) {
198  return;
199  }
200 
201  // removing users could take a while
202  set_time_limit(0);
203 
204  elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() use ($days) {
205  /* @var $users \ElggBatch */
207  'type' => 'user',
208  'metadata_name_value_pairs' => [
209  'validated' => false,
210  ],
211  'created_before' => "-{$days} days",
212  'limit' => false,
213  'batch' => true,
214  'batch_inc_offset' => false,
215  ]);
216 
217  /* @var $user \ElggUser */
218  foreach ($users as $user) {
219  if (!$user->delete()) {
220  // make sure the batch skips over the failed user in the next iteration
221  $users->reportFailure();
222  }
223  }
224  });
225  }
226 }
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:304
User Validation related events.
Definition: Validation.php:13
HTTP response builder interface.
$params
Saves global plugin settings.
Definition: save.php:13
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
Generic parent class for login exceptions.
if(empty($user_guids)) $users
Definition: ban.php:12
$response
Definition: content.php:10
$admin
Definition: useradd.php:19
static notifyAdminsAboutPendingUsers(\Elgg\Event $event)
Send a notification to all admins that there are pending user validations.
Definition: Validation.php:79
static checkAdminValidation(\Elgg\Event $event)
Check if new users need to be validated by an administrator.
Definition: Validation.php:41
elgg_get_session()
Gets Elgg&#39;s session object.
Definition: sessions.php:15
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
$site
Definition: icons.php:5
static removeUnvalidatedUsers(\Elgg\Event $event)
Remove unvalidated users after x days.
Definition: Validation.php:194
elgg_get_admins(array $options=[])
Elgg admin functions.
Definition: admin.php:26
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
static preventUserLogin(\Elgg\Event $event)
Prevent unvalidated users from logging in.
Definition: Validation.php:137
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:132
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
$user
Definition: ban.php:7
static addRiverActivityAfterValidation(\Elgg\Event $event)
Adds river activity that a new user joined the site.
Definition: Validation.php:22
$body
Definition: useradd.php:55
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
static setRegistrationForwardUrl(\Elgg\Event $event)
Set the correct forward url after user registration.
Definition: Validation.php:164
notify_user(int|array $to, int $from=0, string $subject= '', string $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
elgg_normalize_url(string $url)
Definition: output.php:163
Login as the specified user.
$subject
Definition: useradd.php:54
Models an event passed to event handlers.
Definition: Event.php:11