You are here

function MoAuthCustomerSetup::mo_auth_fetch_customer_license 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_fetch_customer_license()

File

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

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

function mo_auth_fetch_customer_license($form, &$form_state) {
  $customer = new MiniorangeCustomerProfile();
  $user_api_handler = new UsersAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $user_api_handler
    ->fetchLicense();
  if ($response->status == 'SUCCESS') {
    $license_type = $response->licenseType;
    $license_plan = $license_type == 'DRUPAL_2FA_PLUGIN' || $license_type == 'DRUPAL8_2FA_MODULE' ? $response->licensePlan : 'DEMO';
    $no_of_users = $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();
    $all_users = $user_api_handler
      ->getall($no_of_users);
    if ($no_of_users == 1) {
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_user_limit_exceed', TRUE)
        ->save();
    }
    if ($all_users->status == 'SUCCESS') {
      if (isset($all_users->fetchedCount) && $all_users->fetchedCount == 1) {
        $fetch_first_user = $user_api_handler
          ->getall(1);
        if ($fetch_first_user->status == 'SUCCESS' && isset($fetch_first_user->fetchedCount) && $fetch_first_user->fetchedCount == 1) {
          if ($fetch_first_user->users[0]->username != $all_users->users[0]->username) {
            \Drupal::configFactory()
              ->getEditable('miniorange_2fa.settings')
              ->set('mo_user_limit_exceed', TRUE)
              ->save();
          }
          else {
            \Drupal::configFactory()
              ->getEditable('miniorange_2fa.settings')
              ->clear('mo_user_limit_exceed')
              ->save();
          }
        }
      }
      else {
        \Drupal::configFactory()
          ->getEditable('miniorange_2fa.settings')
          ->clear('mo_user_limit_exceed')
          ->save();
      }
    }
    drupal_flush_all_caches();
    \Drupal::messenger()
      ->addMessage(t('License fetched successfully.'), 'status');
  }
  else {
    \Drupal::messenger()
      ->addMessage(t('Something is not right. Try after some time'), 'error');
  }
}