You are here

MoAuthLoginSettings.php in Google Authenticator / 2 Factor Authentication - 2FA 8

Same filename and directory in other branches
  1. 8.2 src/Form/MoAuthLoginSettings.php

Contains support form for miniOrange 2FA Login Module.

File

src/Form/MoAuthLoginSettings.php
View source
<?php

/**
 * @file
 * Contains support form for miniOrange 2FA Login Module.
 */
namespace Drupal\miniorange_2fa\Form;

use Drupal\Core\Form\FormBase;
use Drupal\miniorange_2fa\MoAuthUtilities;

/* Showing LoginSetting form info. */
class MoAuthLoginSettings extends FormBase {
  public function getFormId() {
    return 'miniorange_2fa_login_settings';
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    global $base_url;
    $url = $base_url . '/admin/config/people/miniorange_2fa/licensing';
    $form['markup_top_2'] = array(
      '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
    );
    $disabled = False;
    if (!MoAuthUtilities::isCustomerRegistered()) {
      $form['header'] = array(
        '#markup' => '<div class="mo_saml_register_message"><p>You need to <a href="' . $base_url . '/admin/config/people/miniorange_2fa/customer_setup">Register/Login</a> with miniOrange before using this module.</p></div><br>',
      );
      $disabled = True;
    }
    $form['markup_library'] = array(
      '#attached' => array(
        'library' => array(
          "miniorange_2fa/miniorange_2fa.admin",
          "miniorange_2fa/miniorange_2fa.license",
        ),
      ),
    );
    $form['mo_auth_enable_two_factor'] = array(
      '#type' => 'checkbox',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_enable_two_factor'),
      '#prefix' => '<div class="mo2f-setup-header mo2f-setup-header-top-left">Enable Two-Factor module</div><div class="mo2f-info">',
      '#suffix' => '</div><br>',
      '#title' => t('Enable Two-Factor module. <span style="color:red;">(If you disable this checkbox, Second-Factor authentication will not be invoked for any user during login.)</span>'),
      '#disabled' => $disabled,
    );
    $license_type = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_2fa_license_type') == '' ? 'DEMO' : \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_2fa_license_type');
    $is_free = $license_type == 'DRUPAL_2FA_PLUGIN' || $license_type == 'PREMIUM' || $license_type == 'DRUPAL8_2FA_MODULE' ? FALSE : TRUE;
    $is_free_class = $is_free ? " mo2f-grayed-out" : "";
    $form['mo_auth_enforce_inline_registration'] = array(
      '#type' => 'checkbox',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_enforce_inline_registration'),
      '#prefix' => '<div class="mo2f-setup-header mo2f-setup-header-top-left">Invoke Inline Registration to setup 2nd factor for users <a href= "' . $url . '"><b>[PREMIUM]</b></a></span></div><div class="mo2f-info ' . $is_free_class . '">',
      '#suffix' => '</div><br>',
      '#disabled' => $is_free,
      '#title' => t('Enforce 2 Factor registration for users at login time.'),
    );
    $form['markup_idp_login_note'] = array(
      '#markup' => '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Select Login Screen Options <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info"><div class="mo_saml_highlight_background_note"><b>Note: </b>By default 2nd Factor is enabled after password authentication.
             If you do not want to remember passwords anymore and just login with 2nd Factor, please select below option.</div>',
    );
    $form['mo_auth_two_factor_instead_password'] = array(
      '#type' => 'checkbox',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_use_only_2nd_factor'),
      '#prefix' => '',
      '#suffix' => '</div></div><br>',
      '#disabled' => $is_free,
      '#title' => t('Login with 2nd Factor only (No password required.)'),
      '#description' => '<strong>Note:</strong> To use this feature make sure you have enabled the "ENFORCE 2 FACTOR REGISTRATION FOR USERS AT LOGIN TIME" feature.',
    );
    $form['mo_auth_two_factor_invoke_2fa_depending_upon_IP'] = array(
      '#type' => 'checkbox',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_enable_whitelist_IPs'),
      '#prefix' => '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Whitelist IP addresses <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info">',
      '#disabled' => $is_free,
      '#title' => t('Whitelist IP addresses'),
      '#description' => '<strong>Note:</strong> If you whitelist the IPs then 2FA will not be invoked for those IPs.',
    );
    $form['mo_auth_two_factor_whitelist_IP'] = array(
      '#type' => 'textarea',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_whitelisted_IP_address'),
      '#disabled' => $is_free,
      '#attributes' => array(
        'placeholder' => 'Enter semicolon(;) separated IP addresses ( Format for range: lower_range - upper_range )',
      ),
      '#states' => array(
        // Only show this field when the checkbox is enabled.
        'visible' => array(
          ':input[name="mo_auth_two_factor_invoke_2fa_depending_upon_IP"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#suffix' => '</div></div>',
    );
    $form['mo_auth_two_factor_enable_role_based_2fa'] = array(
      '#type' => 'checkbox',
      '#default_value' => \Drupal::config('miniorange_2fa.settings')
        ->get('mo_auth_enable_role_based_2fa'),
      '#prefix' => '<br><div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Role based 2FA <a href= "' . $url . '"><b>[PREMIUM]</b></a></div></div><div class="mo2f-info ' . $is_free_class . '"><div class="mo2f-info">
                        <div class="mo_saml_highlight_background_note"><b>Note: </b>If you have enabled "LOGIN WITH 2ND FACTOR ONLY" feature, Second-Factor authentication will invoke for all roles.</div>',
      '#disabled' => $is_free,
      '#title' => t('Enable role based 2FA'),
      '#description' => "<strong>Note:</strong> If you want to enable 2FA for specific roles then, enable this checkbox and select appropriate roles.</br></br>",
    );
    $roles_arr = MoAuthUtilities::get_Existing_Drupal_Roles();
    $selected_roles = (array) json_decode(\Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_role_based_2fa_roles'));
    foreach ($roles_arr as $sysName => $displayName) {
      $form['mo_auth_two_factor_role_based_2fa_roles_' . strtolower($sysName)] = array(
        '#type' => 'checkbox',
        '#default_value' => array_key_exists($sysName, $selected_roles) ? TRUE : FALSE,
        '#states' => array(
          // Only show this field when the checkbox is enabled.
          'visible' => array(
            ':input[name="mo_auth_two_factor_enable_role_based_2fa"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
        '#title' => t($displayName),
      );
    }
    $form['Submit_LoginSettings_form'] = array(
      '#type' => 'submit',
      '#value' => t('Save Settings'),
      '#disabled' => $disabled,
      '#prefix' => '</div></div><br><br>',
      '#suffix' => '<br><br><br><br><br><br><br></div>',
    );
    MoAuthUtilities::AddsupportTab($form, $form_state);
    return $form;
  }
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $two_factor = $form['mo_auth_enable_two_factor']['#value'];
    $inline_registration = $form['mo_auth_enforce_inline_registration']['#value'];
    $use_only_2nd_factor = $form['mo_auth_two_factor_instead_password']['#value'];
    $mo_enable_whitelist_IPs = $form['mo_auth_two_factor_invoke_2fa_depending_upon_IP']['#value'];
    $mo_whitelist_IPs = preg_replace('/\\s+/', '', $form['mo_auth_two_factor_whitelist_IP']['#value']);
    MoAuthUtilities::check_for_valid_IPs($mo_whitelist_IPs);
    $mo_enable_role_based_2fa = $form['mo_auth_two_factor_enable_role_based_2fa']['#value'];
    $roles_arr = MoAuthUtilities::get_Existing_Drupal_Roles();
    $mo_role_based_2fa_roles = array();
    foreach ($roles_arr as $sysName => $displayName) {
      if ($form['mo_auth_two_factor_role_based_2fa_roles_' . strtolower($sysName)]['#value'] == 1) {
        $mo_role_based_2fa_roles[$sysName] = $form['mo_auth_two_factor_role_based_2fa_roles_' . strtolower($sysName)]['#value'];
      }
    }
    $mo_role_based_2fa_roles_value = !empty($mo_role_based_2fa_roles) ? json_encode($mo_role_based_2fa_roles) : '';
    $enforce_only_2nd_factor = $use_only_2nd_factor == 1 ? TRUE : FALSE;
    $enable2Factor = $two_factor == 1 ? TRUE : FALSE;
    $enforceInlineRegistration = $inline_registration == 1 || $enforce_only_2nd_factor ? TRUE : FALSE;
    $mo_enable_whitelist_IPs_value = $mo_enable_whitelist_IPs == 1 && !empty(is_null($mo_whitelist_IPs) || !empty($mo_whitelist_IPs)) ? TRUE : FALSE;
    $mo_enable_role_based_2fa_value = $mo_enable_role_based_2fa == 1 && !empty($mo_role_based_2fa_roles) ? TRUE : FALSE;
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_use_only_2nd_factor', $enforce_only_2nd_factor)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_enable_two_factor', $enable2Factor)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_enforce_inline_registration', $enforceInlineRegistration)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_enable_whitelist_IPs', $mo_enable_whitelist_IPs_value)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_whitelisted_IP_address', $mo_whitelist_IPs)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_enable_role_based_2fa', $mo_enable_role_based_2fa_value)
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_role_based_2fa_roles', $mo_role_based_2fa_roles_value)
      ->save();
    drupal_flush_all_caches();
    \Drupal::messenger()
      ->addMessage(t("Login settings updated."), 'status');
  }

}

Classes