You are here

function miniorange_2fa_form_alter in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 miniorange_2fa.module \miniorange_2fa_form_alter()

File

./miniorange_2fa.module, line 27
Module file for miniOrange 2FA Module.

Code

function miniorange_2fa_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $variables_and_values = array(
    'mo_auth_enable_login_with_email',
    'mo_auth_enable_login_with_phone',
    'mo_auth_override_login_labels',
    'mo_auth_username_title',
    'mo_auth_username_description',
    'mo_auth_enable_two_factor',
    'mo_auth_use_only_2nd_factor',
    'mo_auth_2fa_use_pass',
    'mo_auth_2fa_license_expiry',
    'mo_auth_2fa_license_type',
  );
  $mo_db_values = MoAuthUtilities::miniOrange_set_get_configurations($variables_and_values, 'GET');

  /**
   * Show admin dashboard notification about license renewal
   */
  $is_admin = \Drupal::service('router.admin_context')
    ->isAdminRoute();
  $isLicenseExpired = MoAuthUtilities::getIsLicenseExpired($mo_db_values['mo_auth_2fa_license_expiry']);
  $updateLicense = Markup::create('</span><a target="_blank" href=" ' . MoAuthUtilities::getUpgradeURL(MoAuthConstants::$RENEW_SUBSCRIPTION_PLAN) . ' ">here</a>');
  if ($is_admin && isset($mo_db_values['mo_auth_2fa_license_expiry']) && $mo_db_values['mo_auth_2fa_license_type'] != 'DEMO' && $isLicenseExpired['LicenseGoingToExpire']) {
    if ($isLicenseExpired['LicenseAlreadyExpired']) {
      \Drupal::messenger()
        ->addError(t('Your license for Drupal 2FA module was expired on ' . $mo_db_values['mo_auth_2fa_license_expiry'] . '. Due to which 2FA service stopped on the same date! Click ' . $updateLicense . ' to renew your license.'));
    }
    else {
      \Drupal::messenger()
        ->addWarning(t('Your license for Drupal 2FA module will expire on ' . $mo_db_values['mo_auth_2fa_license_expiry'] . '. After which 2FA service will stop! Click ' . $updateLicense . ' to renew your license.'));
    }
  }

  /**
   * Disable few of the radio buttons from radio button group.
   */
  if ($form_id == 'miniorange_2fa_setup_two_factor') {
    $form['mo_setup_second_factor']['mo_auth_method']['WHATSAPP'] = [
      '#disabled' => TRUE,
    ];
  }
  if ($form_id == 'user_login_block' || $form_id == 'user_login_form' || $form_id == 'user_profile_form') {
    switch ($form_id) {
      case 'user_login_form':

        /**
         * Check if login with email or phone enabled if yes call function miniorange_2fa_form_extra_validate.
         */
        if ($mo_db_values['mo_auth_enable_login_with_email'] || $mo_db_values['mo_auth_enable_login_with_phone']) {
          array_unshift($form['#validate'], 'miniorange_2fa_form_extra_validate');
        }

        /**
         * Check for settings to override login form username title and description.
         */
        if ($mo_db_values['mo_auth_override_login_labels']) {
          $form['name']['#title'] = t($mo_db_values['mo_auth_username_title']);
          $form['name']['#description'] = t($mo_db_values['mo_auth_username_description']);
        }
        break;
    }
    if (MoAuthUtilities::isCustomerRegistered()) {
      $loginSettings = $mo_db_values['mo_auth_enable_two_factor'];
      if ($loginSettings && !MoAuthUtilities::check_white_IPs()) {
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        $only_2nd_factor = $mo_db_values['mo_auth_use_only_2nd_factor'];
        $use_pass = $mo_db_values['mo_auth_2fa_use_pass'];
        if ($only_2nd_factor and !isset($use_pass)) {
          $output = array_diff_key($form, array_flip((array) [
            'pass',
          ]));
          $output1 = array_diff_key($output, array_flip((array) [
            'actions',
          ]));
          $output2 = array_diff_key($output1, array_flip((array) [
            '#validate',
          ]));
          $form = $output2;
          $form['minorange_login_tfa'] = array(
            '#type' => 'submit',
            '#value' => t('Login with 2nd Factor'),
            '#submit' => array(
              'miniorange_2fa_form_alter_submit',
            ),
            '#prefix' => '<br><br><br>',
          );
        }
        else {
          array_unshift($form['#submit'], 'miniorange_2fa_form_alter_submit');
          \Drupal::configFactory()
            ->getEditable('miniorange_2fa.settings')
            ->clear('mo_auth_2fa_use_pass')
            ->save();
        }
      }
    }
  }
}