You are here

function MoAuthCustomerSetup::mo_auth_save_customer 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::mo_auth_save_customer()
2 calls to MoAuthCustomerSetup::mo_auth_save_customer()
MoAuthCustomerSetup::mo_auth_validate_otp_submit in src/Form/MoAuthCustomerSetup.php
MoAuthCustomerSetup::submitForm in src/Form/MoAuthCustomerSetup.php
Form submission handler.

File

src/Form/MoAuthCustomerSetup.php, line 557
Contains form for customer setup.

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

function mo_auth_save_customer($user_id, $json, $username, $phone) {
  $utilities = new MoAuthUtilities();
  $variables_and_values = array(
    'mo_auth_customer_admin_email' => $username,
    'mo_auth_customer_admin_phone' => $phone,
    'mo_auth_customer_id' => isset($json->id) ? $json->id : '',
    'mo_auth_customer_api_key' => isset($json->apiKey) ? $json->apiKey : '',
    'mo_auth_customer_token_key' => isset($json->token) ? $json->token : '',
    'mo_auth_customer_app_secret' => isset($json->appSecret) ? $json->appSecret : '',
  );
  $utilities
    ->miniOrange_set_get_configurations($variables_and_values, 'SET');
  $auth_method = AuthenticationType::$EMAIL_VERIFICATION['code'];
  $available = $utilities::check_for_userID($user_id);
  $database = \Drupal::database();
  $fields = array(
    'uid' => $user_id,
    'configured_auth_methods' => $auth_method,
    'miniorange_registered_email' => $username,
  );
  if ($available == FALSE) {
    $database
      ->insert('UserAuthenticationType')
      ->fields($fields)
      ->execute();
  }
  elseif ($available == TRUE) {
    $database
      ->update('UserAuthenticationType')
      ->fields([
      'miniorange_registered_email' => $username,
    ])
      ->condition('uid', $user_id, '=')
      ->execute();
  }
  $utilities
    ->miniOrange_set_get_configurations(array(
    'mo_auth_status' => 'PLUGIN_CONFIGURATION',
  ), 'SET');

  // Update the customer second factor to OOB Email in miniOrange
  $customer = new MiniorangeCustomerProfile();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $username, '', '', AuthenticationType::$EMAIL_VERIFICATION['code']);
  $user_api_handler = new UsersAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $user_api_handler
    ->update($miniorange_user);
  $license_response = $user_api_handler
    ->fetchLicense();
  $license_type = 'DEMO';
  $license_plan = 'DEMO';
  $no_of_users = 1;
  if ($license_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addError(t('cURL is not enabled. Please enable cURL'));
    return;
  }
  elseif ($license_response->status == 'SUCCESS') {
    $license_type = $license_response->licenseType;
    if ($license_type == 'DRUPAL_2FA_PLUGIN' || $license_type == 'DRUPAL8_2FA_MODULE') {
      $license_plan = $license_response->licensePlan;
    }
    $no_of_users = $license_response->noOfUsers;
  }
  $mo_db_values = $utilities
    ->miniOrange_set_get_configurations(array(
    'mo_auth_enable_two_factor',
  ), 'GET');
  $variables_and_values_2 = array(
    'mo_auth_2fa_license_type' => $license_type,
    'mo_auth_2fa_license_plan' => $license_plan,
    'mo_auth_2fa_license_no_of_users' => $no_of_users,
    'mo_auth_2fa_ivr_remaining' => isset($license_response->ivrRemaining) ? $license_response->ivrRemaining : '-',
    'mo_auth_2fa_sms_remaining' => isset($license_response->smsRemaining) ? $license_response->smsRemaining : '-',
    'mo_auth_2fa_email_remaining' => isset($license_response->emailRemaining) ? $license_response->emailRemaining : '-',
    'mo_auth_2fa_license_expiry' => isset($license_response->licenseExpiry) ? date('Y-M-d H:i:s', strtotime($license_response->licenseExpiry)) : '-',
    'mo_auth_2fa_support_expiry' => isset($license_response->supportExpiry) ? date('Y-M-d H:i:s', strtotime($license_response->supportExpiry)) : '-',
    'mo_auth_enable_two_factor' => $mo_db_values['mo_auth_enable_two_factor'] == '' ? TRUE : $mo_db_values['mo_auth_enable_two_factor'],
    'mo_auth_enforce_inline_registration' => $license_type == 'DEMO' ? FALSE : TRUE,
  );
  $utilities
    ->miniOrange_set_get_configurations($variables_and_values_2, 'SET');
}