You are here

public function BasicSetup::validateForm in Two-factor Authentication (TFA) 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/BasicSetup.php, line 265

Class

BasicSetup
TFA setup form router.

Namespace

Drupal\tfa\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\user\Entity\User $user */
  $user = $this->userStorage
    ->load($this
    ->currentUser()
    ->id());
  $storage = $form_state
    ->getStorage();
  $values = $form_state
    ->getValues();
  $account = $form['account']['#value'];
  if (isset($values['current_pass'])) {

    // Allow administrators to change TFA settings for another account using
    // their own password.
    if ($account
      ->id() != $user
      ->id()) {
      if ($user
        ->hasPermission('administer users')) {
        $account = $user;
      }
      else {
        throw new NotFoundHttpException();
      }
    }
    $current_pass = $this->passwordChecker
      ->check(trim($form_state
      ->getValue('current_pass')), $account
      ->getPassword());
    if (!$current_pass) {
      $form_state
        ->setErrorByName('current_pass', $this
        ->t("Incorrect password."));
    }
    return;
  }
  elseif (!empty($storage['step_method'])) {
    $method = $storage['step_method'];
    $tfa_setup = $storage[$method];

    // Validate plugin form.
    if (!$tfa_setup
      ->validateForm($form, $form_state)) {
      foreach ($tfa_setup
        ->getErrorMessages() as $element => $message) {
        $form_state
          ->setErrorByName($element, $message);
      }
    }
  }
}