You are here

public function BasicDisable::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/BasicDisable.php, line 101

Class

BasicDisable
TFA disable form router.

Namespace

Drupal\tfa\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, User $user = NULL) {

  /** @var \Drupal\user\Entity\User $account */
  $account = $this->userStorage
    ->load($this
    ->currentUser()
    ->id());
  $storage = $form_state
    ->getStorage();
  $storage['account'] = $user;

  // @todo Check require permissions and give warning about being locked out.
  if ($account
    ->id() != $user
    ->id() && $account
    ->hasPermission('administer users')) {
    $preamble_desc = $this
      ->t('Are you sure you want to disable TFA for user %name?', [
      '%name' => $user
        ->getDisplayName(),
    ]);
    $notice_desc = $this
      ->t('TFA settings and data will be lost. %name can re-enable TFA again from their profile.', [
      '%name' => $user
        ->getDisplayName(),
    ]);
  }
  else {
    $preamble_desc = $this
      ->t('Are you sure you want to disable your two-factor authentication setup?');
    $notice_desc = $this
      ->t("Your settings and data will be lost. You can re-enable two-factor authentication again from your profile.");
  }
  $form['preamble'] = [
    '#prefix' => '<p class="preamble">',
    '#suffix' => '</p>',
    '#markup' => $preamble_desc,
  ];
  $form['notice'] = [
    '#prefix' => '<p class="preamble">',
    '#suffix' => '</p>',
    '#markup' => $notice_desc,
  ];
  $form['account']['current_pass'] = [
    '#type' => 'password',
    '#title' => $this
      ->t('Confirm your current password'),
    '#description_display' => 'before',
    '#size' => 25,
    '#weight' => -5,
    '#attributes' => [
      'autocomplete' => 'off',
    ],
    '#required' => TRUE,
  ];
  $form['account']['mail'] = [
    '#type' => 'value',
    '#value' => $user
      ->getEmail(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => $this
      ->t('Disable'),
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Cancel'),
    '#limit_validation_errors' => [],
    '#submit' => [
      '::cancelForm',
    ],
  ];
  $form_state
    ->setStorage($storage);
  return $form;
}