You are here

public function configure_kba::submitForm in Google Authenticator / 2 Factor Authentication - 2FA 8.2

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

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/configure_kba.php, line 116

Class

configure_kba

Namespace

Drupal\miniorange_2fa\form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $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;
  $form_values = $form_state
    ->getValues();
  $qa1 = array(
    "question" => $form_values['mo2f_kbaquestion1'],
    "answer" => $form_values['mo2f_kbaanswer1'],
  );
  $qa2 = array(
    "question" => $form_values['mo2f_kbaquestion2'],
    "answer" => $form_values['mo2f_kbaanswer2'],
  );
  $qa3 = array(
    "question" => $form_values['mo2f_kbaquestion3'],
    "answer" => $form_values['mo2f_kbaanswer3'],
  );
  $kba = array(
    $qa1,
    $qa2,
    $qa3,
  );
  $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
    ->register($miniorange_user, AuthenticationType::$KBA['code'], NULL, NULL, $kba);

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

  // read API response
  if ($response->status == 'SUCCESS') {
    $configured_methods = $utilities::mo_auth_get_configured_methods($user_id);
    if (!in_array(AuthenticationType::$KBA['code'], $configured_methods)) {
      array_push($configured_methods, AuthenticationType::$KBA['code']);
    }
    $config_methods = implode(', ', $configured_methods);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {

      // Save User
      $user_id = $user
        ->id();
      $available = $utilities::check_for_userID($user_id);
      $database = \Drupal::database();
      if ($available == TRUE) {
        $database
          ->update('UserAuthenticationType')
          ->fields([
          'activated_auth_methods' => AuthenticationType::$KBA['code'],
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
        $database
          ->update('UserAuthenticationType')
          ->fields([
          'configured_auth_methods' => $config_methods,
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
      }
      else {
        echo t("error while saving the authentication method.");
        exit;
      }
      $message = t('KBA Authentication configured successfully.');
      MoAuthUtilities::show_error_or_success_message($message, 'status');
      return;
    }
  }
  elseif ($response->status == 'FAILED') {
    $message = t('An error occurred while configuring KBA Authentication. Please try again.');
    MoAuthUtilities::show_error_or_success_message($message, 'error');
    return;
  }
  $message = t('An error occurred while processing your request. Please try again.');
  MoAuthUtilities::show_error_or_success_message($message, 'error');
  return;
}