You are here

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

Class

BasicOverview
TFA Basic account setup overview page.

Namespace

Drupal\tfa\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
  $output['info'] = [
    '#type' => 'markup',
    '#markup' => '<p>' . $this
      ->t('Two-factor authentication (TFA) provides
      additional security for your account. With TFA enabled, you log in to
      the site with a verification code in addition to your username and
      password.') . '</p>',
  ];

  // $form_state['storage']['account'] = $user;.
  $configuration = $this
    ->config('tfa.settings')
    ->getRawData();
  $user_tfa = $this
    ->tfaGetTfaData($user
    ->id(), $this->userData);
  $enabled = isset($user_tfa['status']) && $user_tfa['status'];
  if (!empty($user_tfa)) {
    if ($enabled && !empty($user_tfa['data']['plugins'])) {
      if ($this
        ->currentUser()
        ->hasPermission('disable own tfa')) {
        $status_text = $this
          ->t('Status: <strong>TFA enabled</strong>, set
          @time. <a href=":url">Disable TFA</a>', [
          '@time' => $this->dateFormatter
            ->format($user_tfa['saved']),
          ':url' => Url::fromRoute('tfa.disable', [
            'user' => $user
              ->id(),
          ])
            ->toString(),
        ]);
      }
      else {
        $status_text = $this
          ->t('Status: <strong>TFA enabled</strong>, set @time.', [
          '@time' => $this->dateFormatter
            ->format($user_tfa['saved']),
        ]);
      }
    }
    else {
      $status_text = $this
        ->t('Status: <strong>TFA disabled</strong>, set @time.', [
        '@time' => $this->dateFormatter
          ->format($user_tfa['saved']),
      ]);
    }
    $output['status'] = [
      '#type' => 'markup',
      '#markup' => '<p>' . $status_text . '</p>',
    ];
  }
  if ($configuration['enabled']) {
    $enabled = isset($user_tfa['status'], $user_tfa['data']) && !empty($user_tfa['data']['plugins']) && $user_tfa['status'];
    $enabled_plugins = isset($user_tfa['data']['plugins']) ? $user_tfa['data']['plugins'] : [];
    $validation_plugins = $this->tfaValidation
      ->getDefinitions();
    foreach ($validation_plugins as $plugin_id => $plugin) {
      if (!empty($configuration['allowed_validation_plugins'][$plugin_id])) {
        $output[$plugin_id] = $this
          ->tfaPluginSetupFormOverview($plugin, $user, !empty($enabled_plugins[$plugin_id]));
      }
    }
    if ($enabled) {
      $login_plugins = $this->tfaLogin
        ->getDefinitions();
      foreach ($login_plugins as $plugin_id => $plugin) {
        if (!empty($configuration['login_plugins'][$plugin_id])) {
          $output[$plugin_id] = $this
            ->tfaPluginSetupFormOverview($plugin, $user, TRUE);
        }
      }
      $send_plugins = $this->tfaSend
        ->getDefinitions();
      foreach ($send_plugins as $plugin_id => $plugin) {
        if (!empty($configuration['send_plugins'][$plugin_id])) {
          $output[$plugin_id] = $this
            ->tfaPluginSetupFormOverview($plugin, $user, TRUE);
        }
      }
    }
  }
  else {
    $output['disabled'] = [
      '#type' => 'markup',
      '#markup' => '<b>Currently there are no enabled plugins.</b>',
    ];
  }
  if ($configuration['enabled']) {
    $output['validation_skip_status'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('Number of times validation skipped: @skipped of @limit', [
        '@skipped' => isset($user_tfa['validation_skipped']) ? $user_tfa['validation_skipped'] : 0,
        '@limit' => $configuration['validation_skip'],
      ]),
    ];
  }
  if ($this
    ->canPerformReset($user)) {
    $output['actions'] = [
      '#type' => 'actions',
    ];
    $output['actions']['reset_skip_attempts'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Reset skip validation attempts'),
      '#submit' => [
        '::resetSkipValidationAttempts',
      ],
    ];
    $output['account'] = [
      '#type' => 'value',
      '#value' => $user,
    ];
  }
  return $output;
}