You are here

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

File

src/Form/MoAuthCustomerSetup.php, line 628
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, $triggered_element = 'FORM') {
  global $base_url;
  $check_loggers = $base_url . '/admin/reports/dblog';
  $utilities = new MoAuthUtilities();
  $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;
    $variables_and_values = 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($response->ivrRemaining) ? $response->ivrRemaining : '-',
      'mo_auth_2fa_sms_remaining' => isset($response->smsRemaining) ? $response->smsRemaining : '-',
      'mo_auth_2fa_email_remaining' => isset($response->emailRemaining) ? $response->emailRemaining : '-',
      'mo_auth_2fa_license_expiry' => isset($response->licenseExpiry) ? date('Y-M-d H:i:s', strtotime($response->licenseExpiry)) : '-',
      'mo_auth_2fa_support_expiry' => isset($response->supportExpiry) ? date('Y-M-d H:i:s', strtotime($response->supportExpiry)) : '-',
    );
    $utilities
      ->miniOrange_set_get_configurations($variables_and_values, 'SET');

    /**
     * Enable Inline registration if license type is premium
     */
    $moEnableInlineRegistration = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_enforce_inline_registration');
    if ($license_type !== 'DEMO' && $triggered_element !== 'CRON' && !isset($moEnableInlineRegistration)) {
      $utilities
        ->miniOrange_set_get_configurations(array(
        'mo_auth_enforce_inline_registration' => TRUE,
      ), 'SET');
    }
    $all_users = $user_api_handler
      ->getall($no_of_users);
    if ($no_of_users == 1) {
      $utilities
        ->miniOrange_set_get_configurations(array(
        'mo_user_limit_exceed' => TRUE,
      ), 'SET');
    }
    if ($all_users->status == 'CURL_ERROR') {
      \Drupal::messenger()
        ->addError(t('cURL is not enabled. Please enable cURL'));
      return;
    }
    elseif ($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) {
            $utilities
              ->miniOrange_set_get_configurations(array(
              'mo_user_limit_exceed' => TRUE,
            ), 'SET');
          }
          else {
            $utilities
              ->miniOrange_set_get_configurations(array(
              'mo_user_limit_exceed' => TRUE,
            ), 'CLEAR');
          }
        }
      }
      else {
        $utilities
          ->miniOrange_set_get_configurations(array(
          'mo_user_limit_exceed' => TRUE,
        ), 'CLEAR');
      }
    }
    drupal_flush_all_caches();
    if ($triggered_element === 'FORM') {
      \Drupal::messenger()
        ->addStatus(t('License fetched successfully.'));
    }
    return;
  }
  MoAuthUtilities::mo_add_loggers_for_failures($response->message, 'error');
  if ($triggered_element === 'FORM') {
    \Drupal::messenger()
      ->addError(t('Something went wrong, Please try again.') . ' <a href="' . $check_loggers . ' " target="_blank">' . t('Click here') . '</a>' . t('for more details.'));
  }
}