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 notifyUserAfterValidation(\Elgg\Event $event) {
23 
24  if (!(bool) elgg_get_config('require_admin_validation')) {
25  return;
26  }
27 
28  $user = $event->getObject();
29  if (!$user instanceof \ElggUser) {
30  return;
31  }
32 
34 
35  $subject = elgg_echo('account:notification:validation:subject', [$site->getDisplayName()], $user->getLanguage());
36  $body = elgg_echo('account:notification:validation:body', [
37  $site->getDisplayName(),
38  $site->getURL(),
39  ], $user->getLanguage());
40 
41  $params = [
42  'action' => 'account:validated',
43  'object' => $user,
44  'apply_muting' => false,
45  ];
46 
47  notify_user($user->guid, $site->guid, $subject, $body, $params, ['email']);
48  }
49 
57  public static function addRiverActivityAfterValidation(\Elgg\Event $event) {
58  if (!(bool) elgg_get_config('user_joined_river')) {
59  return;
60  }
61 
63  'action_type' => 'join',
64  'subject_guid' => $event->getObject()->guid,
65  'object_guid' => elgg_get_site_entity()->guid,
66  ]);
67  }
68 
76  public static function checkAdminValidation(\Elgg\Event $event) {
77 
78  if (!(bool) elgg_get_config('require_admin_validation')) {
79  return;
80  }
81 
82  $user = $event->getUserParam();
83  if (!$user instanceof \ElggUser) {
84  return;
85  }
86 
88 
89  if ($user->isEnabled()) {
90  // disable the user until validation
91  $user->disable('admin_validation_required', false);
92  }
93 
94  // set validation status
95  $user->setValidationStatus(false);
96 
97  // store a flag in session so we can forward the user correctly
99  $session->set('admin_validation', true);
100 
101  if (elgg_get_config('admin_validation_notification') === 'direct') {
102  self::notifyAdminsAboutPendingUsers($event);
103  }
104  });
105  }
106 
114  public static function notifyAdminsAboutPendingUsers(\Elgg\Event $event) {
115 
116  if (empty(elgg_get_config('admin_validation_notification'))) {
117  return;
118  }
119 
120  $unvalidated_count = elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() {
121  return elgg_count_entities([
122  'type' => 'user',
123  'metadata_name_value_pairs' => [
124  'validated' => 0,
125  ],
126  ]);
127  });
128  if (empty($unvalidated_count)) {
129  // shouldn't be able to get here because this function is triggered when a user is marked as unvalidated
130  return;
131  }
132 
134  $admins = elgg_get_admins([
135  'limit' => false,
136  'batch' => true,
137  ]);
138 
139  $url = elgg_normalize_url('admin/users/unvalidated');
140 
141  /* @var $admin \ElggUser */
142  foreach ($admins as $admin) {
143  $user_setting = $admin->admin_validation_notification;
144  if (isset($user_setting) && !(bool) $user_setting) {
145  continue;
146  }
147 
148  $subject = elgg_echo('admin:notification:unvalidated_users:subject', [$site->getDisplayName()], $admin->getLanguage());
149  $body = elgg_echo('admin:notification:unvalidated_users:body', [
150  $unvalidated_count,
151  $site->getDisplayName(),
152  $url,
153  ], $admin->getLanguage());
154 
155  $params = [
156  'action' => 'admin:unvalidated',
157  'object' => $admin,
158  ];
159  notify_user($admin->guid, $site->guid, $subject, $body, $params, ['email']);
160  }
161  }
162 
172  public static function preventUserLogin(\Elgg\Event $event) {
173 
174  if (!(bool) elgg_get_config('require_admin_validation')) {
175  return;
176  }
177 
178  $user = $event->getObject();
179  if (!$user instanceof \ElggUser) {
180  return;
181  }
182 
183  elgg_call(ELGG_SHOW_DISABLED_ENTITIES, function() use ($user) {
184  if ($user->isEnabled() && $user->isValidated() !== false) {
185  return;
186  }
187 
188  throw new LoginException(elgg_echo('LoginException:AdminValidationPending'));
189  });
190  }
191 
199  public static function setRegistrationForwardUrl(\Elgg\Event $event) {
200 
201  $response = $event->getValue();
202  if (!$response instanceof ResponseBuilder) {
203  return;
204  }
205 
207  if (!$session->get('admin_validation')) {
208  return;
209  }
210 
211  // if other plugins already have set forwarding, don't do anything
212  if (!empty($response->getForwardURL()) && $response->getForwardURL() !== REFERRER) {
213  return;
214  }
215 
216  $response->setForwardURL(elgg_generate_url('account:validation:pending'));
217 
218  return $response;
219  }
220 
229  public static function removeUnvalidatedUsers(\Elgg\Event $event): void {
230 
231  $days = (int) elgg_get_config('remove_unvalidated_users_days');
232  if ($days < 1) {
233  return;
234  }
235 
236  // removing users could take a while
237  set_time_limit(0);
238 
239  elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() use ($days) {
240  /* @var $users \ElggBatch */
242  'type' => 'user',
243  'metadata_name_value_pairs' => [
244  'validated' => false,
245  ],
246  'created_before' => "-{$days} days",
247  'limit' => false,
248  'batch' => true,
249  'batch_inc_offset' => false,
250  ]);
251 
252  /* @var $user \ElggUser */
253  foreach ($users as $user) {
254  if (!$user->delete()) {
255  // make sure the batch skips over the failed user in the next iteration
256  $users->reportFailure();
257  }
258  }
259  });
260  }
261 }
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:114
static checkAdminValidation(\Elgg\Event $event)
Check if new users need to be validated by an administrator.
Definition: Validation.php:76
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:229
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:172
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:57
$body
Definition: useradd.php:55
static notifyUserAfterValidation(\Elgg\Event $event)
Notify the user that their account is approved.
Definition: Validation.php:22
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:199
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