You are here

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

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

Class

configure_google_authenticator

Namespace

Drupal\miniorange_2fa\form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $input = $form_state
    ->getValues();
  $secret = $input['secret'];
  $methodToConfigure = $input['methodToConfigure'];
  $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;
  $otpToken = $input['mo_auth_googleauth_token'];
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$GOOGLE_AUTHENTICATOR['code']);
  $auth_api_handler = new AuthenticationAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $auth_api_handler
    ->register($miniorange_user, AuthenticationType::$GOOGLE_AUTHENTICATOR['code'], $secret, $otpToken, NULL);

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

  // read API response
  if ($response->status == 'SUCCESS') {
    \Drupal::messenger()
      ->addStatus(t(''));
    $configured_methods = $utilities::mo_auth_get_configured_methods($user_id);

    /**
     * Delete all the configured TOTP methods as only one can be used at a time
     */
    $configured_methods = array_values(array_diff($configured_methods, array(
      AuthenticationType::$MICROSOFT_AUTHENTICATOR['code'],
      AuthenticationType::$GOOGLE_AUTHENTICATOR['code'],
      AuthenticationType::$AUTHY_AUTHENTICATOR['code'],
      AuthenticationType::$LASTPASS_AUTHENTICATOR['code'],
      AuthenticationType::$DUO_AUTHENTICATOR['code'],
    )));
    array_push($configured_methods, $methodToConfigure);
    $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' => $methodToConfigure,
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
        $database
          ->update('UserAuthenticationType')
          ->fields([
          'configured_auth_methods' => $config_methods,
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
      }
      else {
        echo "error while updating authentication method.";
        exit;
      }
      $message = t(ucwords(strtolower($methodToConfigure)) . ' configured successfully.');
      $utilities::show_error_or_success_message($message, 'status');
      return;
    }
  }
  elseif ($response->status == 'FAILED') {
    \Drupal::messenger()
      ->addError(t('The passcode you have entered is incorrect. Please try again.'));
    return;
  }
  $message = t('An error occured while processing your request. Please try again.');
  MoAuthUtilities::show_error_or_success_message($message, 'error');
  return;
}