You are here

function user_restrictions_validate in User restrictions 8

Validation function to determine if the user is allowed on the site.

Parameters

array $form: Nested array of form elements that comprise the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

3 string references to 'user_restrictions_validate'
user_restrictions_form_user_form_alter in ./user_restrictions.module
Implements hook_form_FORM_ID_alter() for user_form.
user_restrictions_form_user_login_form_alter in ./user_restrictions.module
Implements hook_form_FORM_ID_alter() for user_login_form.
user_restrictions_form_user_register_form_alter in ./user_restrictions.module
Implements hook_form_FORM_ID_alter() for user_register_form.

File

./user_restrictions.module, line 83
Specifies rules for restricting the data users can set for their accounts.

Code

function user_restrictions_validate(array $form, FormStateInterface $form_state) {
  if (\Drupal::currentUser()
    ->hasPermission('bypass user restriction rules')) {
    return;
  }

  // During log in the current user will not be logged in so check first and
  // load the user attempting to log in if this operation is occuring.
  if (!\Drupal::currentUser()
    ->isAuthenticated()) {
    $user = user_load_by_name($form_state
      ->getValue('name'));
    if ($user && $user
      ->hasPermission('bypass user restriction rules')) {
      return;
    }
  }

  /** @var Drupal\user_restrictions\UserRestrictionsManagerInterface $restriction_manager */
  $restriction_manager = \Drupal::service('user_restrictions.manager');
  if ($restriction_manager
    ->matchesRestrictions($form_state
    ->getValues())) {
    foreach ($restriction_manager
      ->getErrors() as $type => $message) {
      $form_state
        ->setError($form, $message);
    }
  }
}