You are here

public function test_kba_authentication::submitForm in Google Authenticator / 2 Factor Authentication - 2FA 8

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

Form submit handler for email verify.

Overrides FormInterface::submitForm

File

src/Form/test_kba_authentication.php, line 127
Email verification functions.

Class

test_kba_authentication
Menu callback for email verification.

Namespace

Drupal\miniorange_2fa\Form

Code

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $input = $form_state
    ->getUserInput();
  $txId = $input['txId'];
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $utilities = new MoAuthUtilities();
  $custom_attribute = $utilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $count = $input['question_count'];
  $kba = array();
  for ($i = 1; $i <= $count; $i++) {
    $ques = $input['mo2f_kbaquestion' . $i];
    $ans = $input['mo2f_kbaanswer' . $i];
    if (empty($ans)) {
      \Drupal::messenger()
        ->addMessage(t('Please enter all the answers first.'), 'error');
      return;
    }
    $qa = array(
      "question" => $ques,
      "answer" => $ans,
    );
    array_push($kba, $qa);
  }
  if (count($kba) > 0) {
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$KBA['code']);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->validate($miniorange_user, $txId, NULL, $kba);

    // Clear all the messages
    \Drupal::messenger()
      ->deleteAll();

    // read API response
    if ($response->status == 'SUCCESS') {
      $message = 'You have successfully completed the test.';
      MoAuthUtilities::show_error_or_success_message($message, 'status');
    }
    elseif ($response->status == 'FAILED') {
      \Drupal::messenger()
        ->addMessage(t('The answers you have entered are incorrect. Please try again.'), 'error');
      return;
    }
  }
  else {
    $message = 'An error occured while processing your request. Please try again.';
    MoAuthUtilities::show_error_or_success_message($message, 'error');
  }
}