You are here

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

Same name and namespace in other branches
  1. 8.2 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 304
Contains form for customer setup.

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $tab = isset($_GET['tab']) ? $_GET['tab'] : 'register';
  $phone = '';
  if ($tab == 'register') {
    $username = trim($form['Mo_auth_customer_register_username']['#value']);
    $phone = trim($form['Mo_auth_customer_register_phone']['#value']);
    $password = trim($form['Mo_auth_customer_register_password']['#value']['pass1']);
  }
  else {
    $username = trim($form['Mo_auth_customer_login_username']['#value']);
    $password = trim($form['Mo_auth_customer_login_password']['#value']);
  }
  if (empty($username) || empty($password)) {
    \Drupal::messenger()
      ->addMessage(t('The <b><u>Email</u></b> and <b><u>password</u></b> fields are mandatory.'), 'error');
    return;
  }
  elseif (!\Drupal::service('email.validator')
    ->isValid($username)) {
    \Drupal::messenger()
      ->addMessage(t('The email address <b>' . $username . '</b> is not valid.'), 'error');
    return;
  }
  $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
  $check_customer_response = $customer_config
    ->checkCustomer();
  if ($check_customer_response->status == 'CUSTOMER_NOT_FOUND') {
    if ($tab == 'login') {
      \Drupal::messenger()
        ->addMessage(t('The account with username <i>' . $username . '</i> does not exist.'), 'error');
      return;
    }

    // Create customer.
    // Store email and phone.
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_customer_admin_email', $username)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_customer_admin_phone', $phone)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_customer_admin_password', $password)
      ->save();
    $send_otp_response = $customer_config
      ->sendOtp();
    if ($send_otp_response->status == 'SUCCESS') {

      // Store txID.
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_auth_tx_id', $send_otp_response->txId)
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_auth_status', 'VALIDATE_OTP')
        ->save();
      \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') {
        \Drupal::messenger()
          ->addMessage(t('Failed to send an OTP. Please check your internet connection'), 'error');
        return;
      }
    }
  }
  elseif ($check_customer_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addMessage(t('cURL is not enabled. Please enable cURL'), 'error');
    return;
  }
  else {

    // 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()
        ->addMessage(t('Your account has been retrieved successfully. '));
    }
    else {
      \Drupal::messenger()
        ->addMessage(t('Invalid credentials'), 'error');
      return;
    }
  }
}