You are here

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

Same name and namespace in other branches
  1. 8 src/Form/configure_qrcode_authentication.php \Drupal\miniorange_2fa\form\configure_qrcode_authentication::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_qrcode_authentication.php, line 195

Class

configure_qrcode_authentication

Namespace

Drupal\miniorange_2fa\form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $input = $form_state
    ->getUserInput();
  $txId = $input['txId'];
  $authTypeCode = $input['authTypeCode'];
  $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;
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$QR_CODE['code']);
  $auth_api_handler = new AuthenticationAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $auth_api_handler
    ->getRegistrationStatus($txId);

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

  // read API response
  if ($response->status == 'SUCCESS') {
    $configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);

    /**
     * If one of the methods in Soft Token, QR Code Authentication, Push Notification is configured then all three methods are configured.
     */
    if (!in_array(AuthenticationType::$SOFT_TOKEN['code'], $configured_methods)) {
      array_push($configured_methods, AuthenticationType::$SOFT_TOKEN['code']);
    }
    if (!in_array(AuthenticationType::$QR_CODE['code'], $configured_methods)) {
      array_push($configured_methods, AuthenticationType::$QR_CODE['code']);
    }
    if (!in_array(AuthenticationType::$PUSH_NOTIFICATIONS['code'], $configured_methods)) {
      array_push($configured_methods, AuthenticationType::$PUSH_NOTIFICATIONS['code']);
    }
    $config_methods = implode(', ', $configured_methods);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());

    /**
     * Updating the authentication method for the user
     */
    $miniorange_user
      ->setAuthType($authTypeCode);
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {

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