You are here

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

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/BasicSetup.php, line 170

Class

BasicSetup
TFA setup form router.

Namespace

Drupal\tfa\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, User $user = NULL, $method = 'tfa_totp', $reset = 0) {

  /** @var \Drupal\user\Entity\User $account */
  $account = $this->userStorage
    ->load($this
    ->currentUser()
    ->id());
  $form['account'] = [
    '#type' => 'value',
    '#value' => $user,
  ];
  $tfa_data = $this
    ->tfaGetTfaData($user
    ->id(), $this->userData);
  $enabled = isset($tfa_data['status'], $tfa_data['data']) && !empty($tfa_data['data']['plugins']) && $tfa_data['status'];
  $storage = $form_state
    ->getStorage();

  // Always require a password on the first time through.
  if (empty($storage)) {

    // Allow administrators to change TFA settings for another account.
    if ($account
      ->id() == $user
      ->id() && $account
      ->hasPermission('administer users')) {
      $current_pass_description = $this
        ->t('Enter your current password to
        alter TFA settings for account %name.', [
        '%name' => $user
          ->getAccountName(),
      ]);
    }
    else {
      $current_pass_description = $this
        ->t('Enter your current password to continue.');
    }
    $form['current_pass'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Current password'),
      '#size' => 25,
      '#required' => TRUE,
      '#description' => $current_pass_description,
      '#attributes' => [
        'autocomplete' => 'off',
      ],
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Confirm'),
    ];
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#limit_validation_errors' => [],
      '#submit' => [
        '::cancelForm',
      ],
    ];
  }
  else {
    if (!$enabled && empty($storage['steps'])) {
      $storage['full_setup'] = TRUE;
      $steps = $this
        ->tfaFullSetupSteps();
      $storage['steps_left'] = $steps;
      $storage['steps_skipped'] = [];
    }
    if (isset($storage['step_method'])) {
      $method = $storage['step_method'];
    }

    // Record methods progressed.
    $storage['steps'][] = $method;
    $plugin = $this
      ->findPlugin($method);
    $setup_plugin = $this->manager
      ->createInstance($plugin['setupPluginId'], [
      'uid' => $account
        ->id(),
    ]);
    $tfa_setup = new TfaSetup($setup_plugin);
    $form = $tfa_setup
      ->getForm($form, $form_state, $reset);
    $storage[$method] = $tfa_setup;
    $form['actions']['#type'] = 'actions';
    if (isset($storage['full_setup']) && count($storage['steps']) > 1) {
      $count = count($storage['steps_left']);
      $form['actions']['skip'] = [
        '#type' => 'submit',
        '#value' => $count > 0 ? $this
          ->t('Skip') : $this
          ->t('Skip and finish'),
        '#limit_validation_errors' => [],
        '#submit' => [
          '::cancelForm',
        ],
      ];
    }
    else {
      $form['actions']['cancel'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Cancel'),
        '#limit_validation_errors' => [],
        '#submit' => [
          '::cancelForm',
        ],
      ];
    }

    // Record the method in progress regardless of whether in full setup.
    $storage['step_method'] = $method;
  }
  $form_state
    ->setStorage($storage);
  return $form;
}