You are here

function configure_otp_over_sms_and_email::mo_auth_configure_otp_over_sms_and_email_submit in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 src/Form/configure_otp_over_sms_and_email.php \Drupal\miniorange_2fa\form\configure_otp_over_sms_and_email::mo_auth_configure_otp_over_sms_and_email_submit()

File

src/Form/configure_otp_over_sms_and_email.php, line 200

Class

configure_otp_over_sms_and_email

Namespace

Drupal\miniorange_2fa\form

Code

function mo_auth_configure_otp_over_sms_and_email_submit(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setRebuild();
  $form_values = $form_state
    ->getValues();
  $customer = new MiniorangeCustomerProfile();
  $custID = $customer
    ->getCustomerID();
  $api_key = $customer
    ->getAPIKey();
  $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;
  $phone_number = isset($form_values['miniorange_phone']) ? str_replace(' ', '', $form_values['miniorange_phone']) : '';
  if ($form_values['authTypeCode'] === AuthenticationType::$OTP_OVER_EMAIL['code']) {
    $currentMethod = "OTP_OVER_EMAIL";
    $params = array(
      'email' => $user_email,
    );
    $mo_status_message = "We have sent an OTP to <strong>{$user_email}</strong>. Please enter the OTP to verify your email.";
  }
  elseif ($form_values['authTypeCode'] === AuthenticationType::$SMS['code']) {
    $currentMethod = "OTP_OVER_SMS";
    $params = array(
      'phone' => $phone_number,
    );
    $mo_status_message = "We have sent an OTP to <strong>{$phone_number}</strong>. Please enter the OTP to verify your phone number.";
  }
  elseif ($form_values['authTypeCode'] === AuthenticationType::$SMS_AND_EMAIL['code']) {
    $currentMethod = "OTP_OVER_SMS_AND_EMAIL";
    $params = array(
      'phone' => $phone_number,
      'email' => $user_email,
    );
    $mo_status_message = "We have sent an OTP to <strong>{$user_email}</strong> and <strong>{$phone_number}</strong>. Please enter the OTP to verify your email and phone number.";
  }
  elseif ($form_values['authTypeCode'] === AuthenticationType::$OTP_OVER_PHONE['code']) {
    $currentMethod = "PHONE_VERIFICATION";
    $params = array(
      'phone' => $phone_number,
    );
    $mo_status_message = "You will receive phone call on <strong>{$phone_number}</strong> shortly, which prompts OTP. Please enter the OTP to verify your phone number.";
  }
  $customer_config = new MiniorangeCustomerSetup($user_email, $phone_number, NULL, NULL);
  $send_otp_response = $customer_config
    ->send_otp_token($params, $currentMethod, $custID, $api_key);
  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::messenger()
      ->addStatus(t($mo_status_message));
    return;
  }
  elseif ($send_otp_response->status == 'FAILED') {
    $utilities::mo_add_loggers_for_failures($send_otp_response->message, 'error');
    \Drupal::messenger()
      ->addError(t('Something went wrong. Please click') . ' <a target="_blank" href="' . $utilities::get_mo_tab_url('LOGS') . '">' . t('here') . '</a> ' . t('for more details.'));
    return;
  }
  elseif ($send_otp_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addError(t('cURL is not enabled. Please enable cURL'));
    return;
  }
}