You are here

public function AutologoutSettingsForm::validateForm in Automated Logout 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormBase::validateForm

File

src/Form/AutologoutSettingsForm.php, line 337

Class

AutologoutSettingsForm
Provides settings for autologout module.

Namespace

Drupal\autologout\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $new_stack = [];
  foreach ($values['table'] as $key => $pair) {
    if (is_array($pair)) {
      foreach ($pair as $pairkey => $pairvalue) {
        $new_stack[$key][$pairkey] = $pairvalue;
      }
    }
  }
  $max_timeout = $values['max_timeout'];
  if ($values['role_logout']) {

    // Validate timeouts for each role.
    foreach (array_keys(user_roles(TRUE)) as $role) {
      if (empty($new_stack[$role]) || $new_stack[$role]['enabled'] == 0) {

        // Don't validate role timeouts for non enabled roles.
        continue;
      }
      $timeout = $new_stack[$role]['timeout'];
      $validate = $this
        ->timeoutValidate($timeout, $max_timeout);
      if (!$validate) {
        $form_state
          ->setErrorByName('table][' . $role . '][timeout', $this
          ->t('%role role timeout must be an integer greater than 60, less then %max or 0 to disable autologout for that role.', [
          '%role' => $role,
          '%max' => $max_timeout,
        ]));
      }
      $role_redirect_url = $new_stack[$role]['url'];
      if (!empty($role_redirect_url) && strpos($role_redirect_url, '/') !== 0) {
        $form_state
          ->setErrorByName('table][' . $role . '][url', $this
          ->t("%role role redirect URL at logout :redirect_url must begin with a '/'", [
          '%role' => $role,
          ':redirect_url' => $role_redirect_url,
        ]));
      }
    }
  }
  $timeout = $values['timeout'];

  // Validate timeout.
  if ($timeout < 60) {
    $form_state
      ->setErrorByName('timeout', $this
      ->t('The timeout value must be an integer 60 seconds or greater.'));
  }
  elseif ($max_timeout <= 60) {
    $form_state
      ->setErrorByName('max_timeout', $this
      ->t('The max timeout must be an integer greater than 60.'));
  }
  elseif (!is_numeric($timeout) || (int) $timeout != $timeout || $timeout < 60 || $timeout > $max_timeout) {
    $form_state
      ->setErrorByName('timeout', $this
      ->t('The timeout must be an integer greater than or equal to 60 and less then or equal to %max.', [
      '%max' => $max_timeout,
    ]));
  }
  $redirect_url = $values['redirect_url'];

  // Validate redirect url.
  if (strpos($redirect_url, '/') !== 0) {
    $form_state
      ->setErrorByName('redirect_url', $this
      ->t("Redirect URL at logout :redirect_url must begin with a '/'", [
      ':redirect_url' => $redirect_url,
    ]));
  }

  // Validate ip address list.
  $whitelisted_ip_addresses_list = explode("\n", trim($values['whitelisted_ip_addresses']));
  foreach ($whitelisted_ip_addresses_list as $ip_address) {
    if (!empty($ip_address) && !filter_var(trim($ip_address), FILTER_VALIDATE_IP)) {
      $form_state
        ->setErrorByName('whitelisted_ip_addresses', $this
        ->t('Whitlelisted IP address list should contain only valid IP addresses, one per row'));
    }
  }
  parent::validateForm($form, $form_state);
}