You are here

class MoAuthCustomerSetup 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

Customer setup form().

Hierarchy

Expanded class hierarchy of MoAuthCustomerSetup

1 string reference to 'MoAuthCustomerSetup'
miniorange_2fa.routing.yml in ./miniorange_2fa.routing.yml
miniorange_2fa.routing.yml

File

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

Namespace

Drupal\miniorange_2fa\Form
View source
class MoAuthCustomerSetup extends FormBase {
  public function getFormId() {
    return 'miniorange_2fa_customer_setup';
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    global $base_url;
    $user_obj = User::load(\Drupal::currentUser()
      ->id());
    $user_id = $user_obj
      ->id();
    $current_status = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_status');
    $form['markup_library'] = array(
      '#attached' => array(
        'library' => 'miniorange_2fa/miniorange_2fa.license',
      ),
    );
    if ($current_status == 'VALIDATE_OTP') {
      $form['markup_top_2'] = array(
        '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
      );
      $form['mo_auth_customer_otp_token'] = array(
        '#type' => 'textfield',
        '#title' => t('Please enter the OTP you received<span style="color: red">*</span>'),
        '#attributes' => array(
          'autofocus' => 'true',
        ),
        '#maxlength' => 6,
        '#suffix' => '<br>',
      );
      $form['mo_auth_customer_validate_otp_button'] = array(
        '#type' => 'submit',
        '#value' => t('Validate OTP'),
        '#submit' => array(
          '::mo_auth_validate_otp_submit',
        ),
      );
      $form['Mo_auth_customer_setup_resendotp'] = array(
        '#type' => 'submit',
        '#value' => t('Resend OTP'),
        '#submit' => array(
          '::mo_auth_resend_otp',
        ),
      );
      $form['Mo_auth_customer_setup_back'] = array(
        '#type' => 'submit',
        '#value' => t('Back'),
        '#submit' => array(
          '::mo_auth_back',
        ),
      );
      $form['main_layout_div_end'] = array(
        '#markup' => '<br><br><br><br>
                        </div>',
      );
      MoAuthUtilities::AddsupportTab($form, $form_state);
      return $form;
    }
    elseif ($current_status == 'PLUGIN_CONFIGURATION') {

      // Show customer configuration here.
      $utilities = new MoAuthUtilities();
      $custom_attribute = $utilities::get_users_custom_attribute($user_id);
      $user_email = $custom_attribute[0]->miniorange_registered_email;
      $customer = new MiniorangeCustomerProfile();
      $user_api_handler = new UsersAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $miniorange_user = new MiniorangeUser($customer
        ->getCustomerID(), $user_email, '', '', '');
      $response = $user_api_handler
        ->get($miniorange_user);
      $authType = AuthenticationType::getAuthType($response->authType);
      $form['markup_top_1'] = array(
        '#markup' => '<div class="mo_saml_table_layout_1"><div id="Register_Section" class="mo_saml_table_layout mo_saml_container">',
      );

      /* Show message if user creation limit exceeded */
      $mo_user_limit = \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->get('mo_user_limit_exceed');
      if (isset($mo_user_limit) && $mo_user_limit == TRUE) {
        $form['markup_top_2'] = array(
          '#markup' => '<div class="users_limit_exceeded_message">Your user creation limit has been completed. Please upgrade your license to add more users. Please ignore if already upgraded. </div>',
        );
      }
      $form['markup_top'] = array(
        '#markup' => '<div class="mo_saml_welcome_message">Thank you for registering with miniOrange</div><h4>Your Profile: </h4>',
      );
      $header = array(
        'attribute' => array(
          'data' => t('Attribute'),
        ),
        'value' => array(
          'data' => t('Value'),
        ),
      );
      $options = array();
      $options[0] = array(
        'attribute' => '2 Factor Registered Email',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_customer_admin_email'),
      );
      $options[1] = array(
        'attribute' => 'Activated 2nd Factor',
        'value' => $authType['name'],
      );
      $options[2] = array(
        'attribute' => 'Xecurify Registered Email',
        'value' => $user_email,
      );
      $options[3] = array(
        'attribute' => 'Customer ID',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_customer_id'),
      );
      $options[4] = array(
        'attribute' => 'API Key',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_customer_api_key'),
      );
      $options[5] = array(
        'attribute' => 'Token Key',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_customer_token_key'),
      );
      $options[6] = array(
        'attribute' => 'App Secret',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_customer_app_secret'),
      );
      $form['fieldset']['customerinfo'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $options,
      );
      $license_header = array(
        'attribute' => array(
          'data' => t('Attribute'),
        ),
        'value' => array(
          'data' => t('Value'),
        ),
      );
      $license_options = array();
      $license_options[0] = array(
        'attribute' => 'License Type',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_type') == '' ? 'DEMO' : \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_type'),
      );
      $license_options[1] = array(
        'attribute' => 'License Plan',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_plan') == '' ? 'DEMO' : \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_plan'),
      );
      $license_options[2] = array(
        'attribute' => 'No. of Users',
        'value' => \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_no_of_users') == '' ? 1 : \Drupal::config('miniorange_2fa.settings')
          ->get('mo_auth_2fa_license_no_of_users'),
      );
      $form['fieldset']['customer-license'] = array(
        '#theme' => 'table',
        '#header' => $license_header,
        '#rows' => $license_options,
        '#prefix' => '<div style="margin-top: 20px;"><h4>Your license info: </h4></div>',
      );
      $form['fecth_customer_license'] = array(
        '#type' => 'submit',
        '#value' => t('Check License'),
        '#submit' => array(
          '::mo_auth_fetch_customer_license',
        ),
      );
      $form['miniorange_saml_customer_Remove_Account_info'] = array(
        '#markup' => '<br><br><br><br><br/><h4>Remove Account:</h4><p>This section will help you to remove your current
                            logged in account. </p>',
      );
      $form['miniorange_saml_customer_Remove_Account'] = array(
        '#type' => 'link',
        '#title' => $this
          ->t('Remove Account'),
        '#url' => \Drupal\Core\Url::fromRoute('miniorange_2fa.modal_form'),
        '#attributes' => [
          'class' => [
            'use-ajax',
            'button',
          ],
        ],
        '#suffix' => '<br><br><hr><br><br></div>',
      );
      $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
      MoAuthUtilities::AddsupportTab($form, $form_state);
      return $form;
    }
    $url = $base_url . '/admin/config/people/miniorange_2fa/customer_setup';
    $tab = isset($_GET['tab']) && $_GET['tab'] == 'login' ? $_GET['tab'] : 'register';
    $form['markup_start'] = array(
      '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">
                         ',
    );
    if ($tab == 'register') {
      $form['markup_msg_1'] = array(
        '#markup' => '<h2>Register with mini<span class="mo_orange"><b>O</b></span>range</h2><hr><br>
                            <div class="mo_saml_highlight_background_note_1">Just complete the short registration below to configure the Two-Factor Module. Please enter a valid email ID that you have access to.
                              You will be able to move forward after verifying an OTP that we will send to this email.</div>
                        ',
      );
      $form['Mo_auth_customer_register_username'] = array(
        '#type' => 'textfield',
        '#id' => "email_id",
        '#title' => t('Email<span style="color: red">*</span>'),
        '#description' => t('<b>Note:</b> Use valid EmailId. (We discourage the use of disposable emails)'),
        '#attributes' => array(
          'autofocus' => 'true',
        ),
      );
      $form['Mo_auth_customer_register_phone'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone'),
        '#id' => 'query_phone',
        '#description' => t('<b>Note:</b> We will only call if you need support.'),
        '#attributes' => array(
          'class' => array(
            'query_phone',
          ),
        ),
      );
      $form['Mo_auth_customer_register_password'] = array(
        '#type' => 'password_confirm',
      );
      $form['Mo_auth_customer_register_button'] = array(
        '#type' => 'submit',
        '#value' => t('Register'),
        '#limit_validation_errors' => array(),
        '#prefix' => '<br><div class="ns_row"><div class="ns_name">',
        '#suffix' => '</div>',
      );
      $form['already_account_link'] = array(
        '#markup' => '<a href="' . $url . '/?tab=login" class="ns_display"><b>Already have an account ?</b></a>',
        '#prefix' => '<div class="ns_value">',
        '#suffix' => '</div></div><br><br></div>',
      );
    }
    else {
      $form['markup_15'] = array(
        '#markup' => '<h2>Login with mini<span class="mo_orange"><b>O</b></span>range</h2><hr>',
      );
      $form['markup_16'] = array(
        '#markup' => '<br><div class="mo_saml_highlight_background_note_2" style="width:35% !important;">Please login with your miniorange account.</b></div><br>',
      );
      $form['Mo_auth_customer_login_username'] = array(
        '#type' => 'email',
        '#title' => t('Email <span style="color: red">*</span>'),
        '#attributes' => array(
          'style' => 'width:50%',
        ),
      );
      $form['Mo_auth_customer_login_password'] = array(
        '#type' => 'password',
        '#title' => t('Password <span style="color: red">*</span>'),
        '#attributes' => array(
          'style' => 'width:50%',
        ),
      );
      $form['Mo_auth_customer_login_button'] = array(
        '#type' => 'submit',
        '#value' => t('Login'),
        '#limit_validation_errors' => array(),
        '#prefix' => '<br><div class="ns_row"><div class="ns_name">',
        '#suffix' => '</div>',
      );
      $form['register_link'] = array(
        '#markup' => '<a href="' . $url . '" class="ns_display"><b>Create an account?</b></a>',
        '#prefix' => '<div class="ns_value">',
        '#suffix' => '</div></div><br></div>',
      );
    }
    MoAuthUtilities::AddsupportTab($form, $form_state);
    return $form;
  }

  //Handle submit for customer setup.
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $user = User::load(\Drupal::currentUser()
      ->id());
    $user_id = $user
      ->id();
    $tab = isset($_GET['tab']) ? $_GET['tab'] : 'register';
    $phone = '';
    if ($tab == 'register') {
      $username = trim($form['Mo_auth_customer_register_username']['#value']);
      $phone = trim($form['Mo_auth_customer_register_phone']['#value']);
      $password = trim($form['Mo_auth_customer_register_password']['#value']['pass1']);
    }
    else {
      $username = trim($form['Mo_auth_customer_login_username']['#value']);
      $password = trim($form['Mo_auth_customer_login_password']['#value']);
    }
    if (empty($username) || empty($password)) {
      \Drupal::messenger()
        ->addMessage(t('The <b><u>Email</u></b> and <b><u>password</u></b> fields are mandatory.'), 'error');
      return;
    }
    elseif (!\Drupal::service('email.validator')
      ->isValid($username)) {
      \Drupal::messenger()
        ->addMessage(t('The email address <b>' . $username . '</b> is not valid.'), 'error');
      return;
    }
    $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
    $check_customer_response = $customer_config
      ->checkCustomer();
    if ($check_customer_response->status == 'CUSTOMER_NOT_FOUND') {
      if ($tab == 'login') {
        \Drupal::messenger()
          ->addMessage(t('The account with username <i>' . $username . '</i> does not exist.'), 'error');
        return;
      }

      // Create customer.
      // Store email and 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_admin_password', $password)
        ->save();
      $send_otp_response = $customer_config
        ->sendOtp();
      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::configFactory()
          ->getEditable('miniorange_2fa.settings')
          ->set('mo_auth_status', 'VALIDATE_OTP')
          ->save();
        \Drupal::messenger()
          ->addStatus(t('We have sent an OTP to <strong>@username</strong>. Please enter the OTP to verify your email.', array(
          '@username' => $username,
        )));
      }
      else {
        if ($send_otp_response->status == 'FAILED') {
          \Drupal::messenger()
            ->addMessage(t('Failed to send an OTP. Please check your internet connection'), 'error');
          return;
        }
      }
    }
    elseif ($check_customer_response->status == 'CURL_ERROR') {
      \Drupal::messenger()
        ->addMessage(t('cURL is not enabled. Please enable cURL'), 'error');
      return;
    }
    else {

      // Customer exists. Retrieve keys.
      $customer_keys_response = $customer_config
        ->getCustomerKeys();
      if (json_last_error() == JSON_ERROR_NONE) {
        $this
          ->mo_auth_save_customer($user_id, $customer_keys_response, $username, $phone);
        \Drupal::messenger()
          ->addMessage(t('Your account has been retrieved successfully. '));
      }
      else {
        \Drupal::messenger()
          ->addMessage(t('Invalid credentials'), 'error');
        return;
      }
    }
  }

  // Validate OTP.
  function mo_auth_validate_otp_submit(&$form, $form_state) {
    $user = User::load(\Drupal::currentUser()
      ->id());
    $user_id = $user
      ->id();
    $otp_token = $form['mo_auth_customer_otp_token']['#value'];
    if (empty($otp_token)) {
      \Drupal::messenger()
        ->addMessage(t('The <b>OTP</b> field is mandatory.'), 'error');
      return;
    }
    $username = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_email') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_email');
    $phone = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_phone') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_phone');
    $txId = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_tx_id') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_tx_id');
    $customerSetup = new MiniorangeCustomerSetup($username, $phone, NULL, $otp_token);

    // Validate OTP.
    $validate_otp_response = json_decode($customerSetup
      ->validate_otp_token($txId, $otp_token, MoAuthConstants::$DEFAULT_CUSTOMER_ID, MoAuthConstants::$DEFAULT_CUSTOMER_API_KEY));
    if ($validate_otp_response->status == 'SUCCESS') {

      // OTP Validated. Show Configuration page.
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_auth_status', 'PLUGIN_CONFIGURATION')
        ->save();
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->clear('mo_auth_tx_id')
        ->save();

      // OTP Validated. Create customer.
      $password = \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_customer_admin_password');
      $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
      $create_customer_response = $customer_config
        ->createCustomer();
      if ($create_customer_response->status == 'SUCCESS') {

        // Customer created.
        $this
          ->mo_auth_save_customer($user_id, $create_customer_response, $username, $phone);
        \Drupal::messenger()
          ->addMessage(t('Customer account created. Email Verification has been set as your default 2nd factor method.'));
      }
      else {
        if (trim($create_customer_response->message) == 'Email is not enterprise email.') {
          \Drupal::messenger()
            ->addMessage(t('There was an error creating an account for you.<br> You may have entered an invalid Email-Id
                <strong>(We discourage the use of disposable emails) </strong>
                <br>Please try again with a valid email.'), 'error');
          return;
        }
        else {
          \Drupal::messenger()
            ->addMessage(t('An error occured while creating your account. Please try again or contact us at <a href="mailto:info@xecurify.com">info@xecurify.com</a>.'), 'error');
          return;
        }
      }
    }
    else {
      \Drupal::messenger()
        ->addMessage(t('The OTP you have entered is incorrect. Please try again.'), 'error');
      return;
    }
  }

  // Resend OTP.
  function mo_auth_resend_otp(&$form, $form_state) {
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->clear('mo_auth_tx_id')
      ->save();
    $username = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_email');
    $phone = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_phone');
    $customer_config = new MiniorangeCustomerSetup($username, $phone, NULL, NULL);
    $send_otp_response = $customer_config
      ->sendOtp();
    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::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_auth_status', 'VALIDATE_OTP')
        ->save();
      \Drupal::messenger()
        ->addStatus(t('We have sent an OTP to <strong>@username</strong>. Please enter the OTP to verify your email.', array(
        '@username' => $username,
      )));
    }
  }

  /**
   * Handle back button submit for customer setup.
   */
  function mo_auth_back($form, &$form_state) {
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_status', 'CUSTOMER_SETUP')
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->clear('mo_auth_customer_admin_email')
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->clear('mo_auth_customer_admin_phone')
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->clear('mo_auth_tx_id')
      ->save();
    \Drupal::messenger()
      ->addMessage(t('Register/Login with your miniOrange Account'));
  }
  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();
  }
  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');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MoAuthCustomerSetup::buildForm public function Form constructor. Overrides FormInterface::buildForm
MoAuthCustomerSetup::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MoAuthCustomerSetup::mo_auth_back function Handle back button submit for customer setup.
MoAuthCustomerSetup::mo_auth_fetch_customer_license function
MoAuthCustomerSetup::mo_auth_resend_otp function
MoAuthCustomerSetup::mo_auth_save_customer function
MoAuthCustomerSetup::mo_auth_validate_otp_submit function
MoAuthCustomerSetup::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.