You are here

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

Same name and namespace in other branches
  1. 8 src/Form/MoAuthSetupTwoFactor.php \Drupal\miniorange_2fa\Form\MoAuthSetupTwoFactor::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/MoAuthSetupTwoFactor.php, line 186
Contains Setup Two-Factor page for miniOrange 2FA Login Module.

Class

MoAuthSetupTwoFactor
Showing Setup Two-Factor page.

Namespace

Drupal\miniorange_2fa\Form

Code

function submitForm(array &$form, FormStateInterface $form_state) {
  $utilities = new MoAuthUtilities();
  $user_obj = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user_obj
    ->id();
  $configured_methods = $utilities::mo_auth_get_configured_methods($user_id);
  $custom_attribute = $utilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $form_state
    ->setRebuild();
  $input = $form_state
    ->getUserInput();
  $authType = $input['mo_auth_method'];
  $database = \Drupal::database();
  $database
    ->update('UserAuthenticationType')
    ->fields([
    'activated_auth_methods' => $authType,
  ])
    ->condition('miniorange_registered_email', $user_email, '=')
    ->execute();
  if (in_array($authType, $configured_methods)) {
    $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, $authType);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {
      \Drupal::messenger()
        ->addStatus(t('Authentication method updated successfully.'));
      return;
    }
    \Drupal::messenger()
      ->addError(t('An error occurred while updating the authentication type. Please try again.'));
    return;
  }
  \Drupal::messenger()
    ->addError(t('Please configure this authentication method first to enable it.'));
  return;
}