You are here

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

Same name and namespace in other branches
  1. 8 src/Form/MoAuthCustomerSetup.php \Drupal\miniorange_2fa\Form\MoAuthCustomerSetup::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/MoAuthCustomerSetup.php, line 373
Contains form for customer setup.

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  global $base_url;
  $check_loggers = $base_url . '/admin/reports/dblog';
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $tab = isset($_GET['tab']) ? $_GET['tab'] : 'register';
  if ($tab == 'register') {
    $username = trim($form['mo_register_form']['Mo_auth_customer_register_username']['#value']);
    $phone = trim($form['mo_register_form']['Mo_auth_customer_register_phone']['#value']);
    $password = trim($form['mo_register_form']['Mo_auth_customer_register_password']['#value']['pass1']);
  }
  else {
    $username = trim($form['mo_login_form']['Mo_auth_customer_login_username']['#value']);
    $password = trim($form['mo_login_form']['Mo_auth_customer_login_password']['#value']);
    $phone = '';
  }
  $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
  $check_customer_response = $customer_config
    ->checkCustomer();
  $utilities = new MoAuthUtilities();
  if (is_object($check_customer_response) && $check_customer_response->status == 'CUSTOMER_NOT_FOUND') {
    if ($tab == 'login') {
      \Drupal::messenger()
        ->addError(t('The account with username<b>@username</b> does not exist.'), array(
        '@username' => $username,
      ));
      return;
    }

    // Create customer.
    // Store email and phone.
    $variables_and_values = array(
      'mo_auth_customer_admin_email' => $username,
      'mo_auth_customer_admin_phone' => $phone,
      'mo_auth_customer_admin_password' => $password,
    );
    $utilities
      ->miniOrange_set_get_configurations($variables_and_values, 'SET');
    $send_otp_response = $customer_config
      ->sendOtp();
    if ($send_otp_response->status == 'SUCCESS') {

      // Store txID.
      $variables_and_values_2 = array(
        'mo_auth_tx_id' => $send_otp_response->txId,
        'mo_auth_status' => 'VALIDATE_OTP',
      );
      $utilities
        ->miniOrange_set_get_configurations($variables_and_values_2, 'SET');
      \Drupal::messenger()
        ->addStatus(t('We have sent an OTP to <strong>@username</strong>. Please enter the OTP to verify your email.', array(
        '@username' => $username,
      )));
    }
    else {
      if ($send_otp_response->status == 'FAILED') {
        MoAuthUtilities::mo_add_loggers_for_failures($check_customer_response->message, 'error');
        \Drupal::messenger()
          ->addError(t('Failed to send an OTP. Please check your internet connection.') . ' <a href="' . $check_loggers . ' " target="_blank">' . t('Click here') . ' </a>' . t('for more details.'));
        return;
      }
    }
  }
  elseif (is_object($check_customer_response) && $check_customer_response->status == 'SUCCESS' && $check_customer_response->message == 'Customer already exists.') {

    // Customer exists. Retrieve keys.
    $customer_keys_response = $customer_config
      ->getCustomerKeys();
    if (json_last_error() == JSON_ERROR_NONE) {
      $this
        ->mo_auth_save_customer($user_id, $customer_keys_response, $username, $phone);
      \Drupal::messenger()
        ->addStatus(t('Your account has been retrieved successfully.'));
    }
    else {
      \Drupal::messenger()
        ->addError(t('Invalid credentials'));
      return;
    }
  }
  elseif (is_object($check_customer_response) && $check_customer_response->status == 'TRANSACTION_LIMIT_EXCEEDED') {
    MoAuthUtilities::mo_add_loggers_for_failures($check_customer_response->message, 'error');
    \Drupal::messenger()
      ->addError(t('Failed to send an OTP. Please check your internet connection.') . ' <a href="' . $check_loggers . ' " target="_blank">' . t('Click here') . ' </a>' . t('for more details.'));
    return;
  }
  elseif (is_object($check_customer_response) && $check_customer_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addError(t('cURL is not enabled. Please enable cURL'));
    return;
  }
  else {
    MoAuthUtilities::mo_add_loggers_for_failures(isset($check_customer_response->message) ? $check_customer_response->message : '', 'error');
    \Drupal::messenger()
      ->addError(t('Something went wrong, Please try again.') . ' <a href="' . $check_loggers . ' " target="_blank">' . t('Click here') . '</a>' . t('for more details.'));
    return;
  }
}