You are here

function MoAuthCustomerSetup::mo_auth_save_customer 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::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 446
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) {
  \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_id', isset($json->id) ? $json->id : '')
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_customer_api_key', isset($json->apiKey) ? $json->apiKey : '')
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_customer_token_key', isset($json->token) ? $json->token : '')
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_customer_app_secret', isset($json->appSecret) ? $json->appSecret : '')
    ->save();
  $auth_method = AuthenticationType::$EMAIL_VERIFICATION['code'];
  $utilities = new MoAuthUtilities();
  $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();
  }
  else {
    if ($available == TRUE) {
      $database
        ->update('UserAuthenticationType')
        ->fields([
        'miniorange_registered_email' => $username,
      ])
        ->condition('uid', $user_id, '=')
        ->execute();
    }
  }
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_status', 'PLUGIN_CONFIGURATION')
    ->save();

  // 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 == '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;
  }
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_2fa_license_type', $license_type)
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_2fa_license_plan', $license_plan)
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_2fa_license_no_of_users', $no_of_users)
    ->save();
  $mo_auth_enable_two_factor = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_enable_two_factor') == '' ? TRUE : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_enable_two_factor');
  $mo_auth_enforce_inline_registration = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_enforce_inline_registration') == '' ? FALSE : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_enforce_inline_registration');
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_enable_two_factor', $mo_auth_enable_two_factor)
    ->save();
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_auth_enforce_inline_registration', $mo_auth_enforce_inline_registration)
    ->save();
}