You are here

public function authenticate_user::buildForm in Google Authenticator / 2 Factor Authentication - 2FA 8

Same name and namespace in other branches
  1. 8.2 src/Form/authenticate_user.php \Drupal\miniorange_2fa\form\authenticate_user::buildForm()

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/authenticate_user.php, line 26
This is used to authenticate user during login.

Class

authenticate_user
@file This is used to authenticate user during login.

Namespace

Drupal\miniorange_2fa\form

Code

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  \Drupal::service('page_cache_kill_switch')
    ->trigger();
  global $base_url;
  $form['markup_library'] = array(
    '#attached' => array(
      'library' => array(
        "miniorange_2fa/miniorange_2fa.admin",
        "miniorange_2fa/miniorange_2fa.license",
      ),
    ),
  );
  $query_param = \Drupal::service('path.current')
    ->getPath();
  $url_parts = explode('/', $query_param);
  end($url_parts);
  $user_id = prev($url_parts);
  $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  if (\Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_challanged') == 0) {
    $this
      ->mo_auth_challenge_user($form, $user_email, $user_id, $form_state);
  }
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_challanged', 1)
    ->save();
  if (empty($_SESSION['mo_auth']['mo_challenge_response'])) {
    return;
  }
  if (isset($_SESSION['mo_auth']['status']) && $_SESSION['mo_auth']['status'] === '1ST_FACTOR_AUTHENTICATED') {
    $challenge_response = $_SESSION['mo_auth']['mo_challenge_response'];
    $authType = $challenge_response->authType;
    $form['actions'] = array(
      '#type' => 'actions',
    );
    if (!empty($authType)) {
      $form['authType'] = array(
        '#type' => 'hidden',
        '#value' => $authType,
      );
      $authType = AuthenticationType::getAuthType($authType);
      $form = self::mo_auth_build_form($form, $base_url, $authType, $challenge_response, TRUE);
      unset($form['mo_message']);
    }
    else {
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#attributes' => array(
          'class' => array(
            'hidebutton',
          ),
        ),
      );
    }
    return $form;
  }
  $response = new RedirectResponse($base_url . '/user/login');
  $response
    ->send();
  exit;
}