You are here

public function BasicSetup::submitForm in Two-factor Authentication (TFA) 8

Form submission 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 FormInterface::submitForm

File

src/Form/BasicSetup.php, line 318

Class

BasicSetup
TFA setup form router.

Namespace

Drupal\tfa\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $account = $form['account']['#value'];
  $storage = $form_state
    ->getStorage();
  $values = $form_state
    ->getValues();

  // Password validation.
  if (isset($values['current_pass'])) {
    $storage['pass_confirmed'] = TRUE;
    $form_state
      ->setRebuild();
    $form_state
      ->setStorage($storage);
    return;
  }
  elseif (!empty($storage['step_method'])) {
    $method = $storage['step_method'];
    $skipped_method = FALSE;

    // Support skipping optional steps when in full setup.
    if (isset($values['skip']) && $values['op'] === $values['skip']) {
      $skipped_method = $method;
      $storage['steps_skipped'][] = $method;
      unset($storage[$method]);
    }
    if (!empty($storage[$method])) {

      // Trigger multi-step if in full setup.
      if (!empty($storage['full_setup'])) {
        $this
          ->tfaNextSetupStep($form_state, $method, $storage[$method], $skipped_method);
      }

      // Plugin form submit.
      $setup_class = $storage[$method];
      if (!$setup_class
        ->submitForm($form, $form_state)) {
        $this
          ->messenger()
          ->addError($this
          ->t('There was an error during TFA setup. Your settings have not been saved.'));
        $form_state
          ->setRedirect('tfa.overview', [
          'user' => $account
            ->id(),
        ]);
        return;
      }
    }

    // Return if multi-step.
    if ($form_state
      ->getRebuildInfo()) {
      return;
    }

    // Else, setup complete and return to overview page.
    $this
      ->messenger()
      ->addStatus($this
      ->t('TFA setup complete.'));
    $form_state
      ->setRedirect('tfa.overview', [
      'user' => $account
        ->id(),
    ]);

    // Log and notify if this was full setup.
    if (!empty($storage['step_method'])) {
      $data = [
        'plugins' => $storage['step_method'],
      ];
      $this
        ->tfaSaveTfaData($account
        ->id(), $this->userData, $data);
      $this
        ->logger('tfa')
        ->info('TFA enabled for user @name UID @uid', [
        '@name' => $account
          ->getAccountName(),
        '@uid' => $account
          ->id(),
      ]);
      $params = [
        'account' => $account,
      ];
      $this->mailManager
        ->mail('tfa', 'tfa_enabled_configuration', $account
        ->getEmail(), $account
        ->getPreferredLangcode(), $params);
    }
  }
}