You are here

function miniorange_2fa_inline_registration::handle_page_one_submit in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 src/Form/miniorange_2fa_inline_registration.php \Drupal\miniorange_2fa\form\miniorange_2fa_inline_registration::handle_page_one_submit()

File

src/Form/miniorange_2fa_inline_registration.php, line 178
Page 1: Select Email address. Page 2: Verify OTP. Page 3: Select Auth Method. Page 4: Configure Auth Method. Page 5: Configure KBA.

Class

miniorange_2fa_inline_registration
@file Page 1: Select Email address. Page 2: Verify OTP. Page 3: Select Auth Method. Page 4: Configure Auth Method. Page 5: Configure KBA.

Namespace

Drupal\miniorange_2fa\form

Code

function handle_page_one_submit(array $form, FormStateInterface $form_state) {
  $email = MoAuthUtilities::getSession()
    ->get('mo_auth')['user_email'];
  if (!\Drupal::service('email.validator')
    ->isValid($email)) {

    // Send Status as this to show error message
    $_SESSION['success_status'] = FALSE;
    $_SESSION['message'] = t('The email address <b class="mo2f_bold"> %email </b> is not valid.', array(
      '%email' => $email,
    ));
    $form_state
      ->setRebuild();
    return $form;
  }
  $connection = \Drupal::database();
  $query = $connection
    ->query("SELECT * FROM {UserAuthenticationType} where miniorange_registered_email = '{$email}'");
  $result = $query
    ->fetchAll();
  $email_used = FALSE;
  if (!empty($result)) {
    $email_used = TRUE;
  }
  if ($email_used) {

    // Send Status as this to show error message
    $_SESSION['success_status'] = FALSE;
    $form_state
      ->setRebuild();
    return $form;
  }
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $email, NULL, NULL, NULL);
  $user_api_handler = new UsersAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $user_api_handler
    ->search($miniorange_user);
  if ($response->status == 'USER_FOUND' || $response->status == 'USER_NOT_FOUND') {
    $challenge_response = $this
      ->send_otp_email_to_user($email);
    if ($challenge_response->status == 'SUCCESS') {
      $page = [
        'page_two' => TRUE,
        'page_one_values' => $form_state
          ->getValues(),
        [
          'user_search_response' => $response,
          'user_challenge_response' => $challenge_response,
        ],
      ];
      $form_state
        ->setStorage($page);
      $form_state
        ->setRebuild();
    }
    else {
      MoAuthUtilities::mo_add_loggers_for_failures($challenge_response->message, 'error');
      \Drupal::messenger()
        ->addError(t('An error occured while registering. Please contact your administrator.'));
    }
  }
  elseif (is_object($response) && $response->status == 'USER_FOUND_UNDER_DIFFERENT_CUSTOMER') {
    $_SESSION['success_status'] = FALSE;
    $form_state
      ->setRebuild();
    return $form;
  }
  else {
    unset($_SESSION['success_status']);
    MoAuthUtilities::mo_add_loggers_for_failures(is_object($response) ? $response->message : '', 'error');
    \Drupal::messenger()
      ->addError(t("An error occurred. Please contact your administrator."), TRUE);
    $url = Url::fromRoute('user.login')
      ->toString();
    $response = new RedirectResponse($url);
    $response
      ->send();
  }
}