You are here

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

Same name and namespace in other branches
  1. 8.2 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 142
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, \Drupal\Core\Form\FormStateInterface $form_state) {
  $user_obj = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user_obj
    ->id();
  $configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
  $utilities = new MoAuthUtilities();
  $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)) {
    $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, $authType);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {
      \Drupal::messenger()
        ->addMessage(t('Authentication method updated successfully.'), 'status');
    }
    else {
      \Drupal::messenger()
        ->addMessage(t('An error occured while updating the authentication type. Please try again.'), 'error');
      return;
    }
  }
  else {
    \Drupal::messenger()
      ->addMessage(t('Please configure this authentication method first to enable it.'), 'error');
    return;
  }
}