You are here

class MoAuthLoginSettings in Google Authenticator / 2 Factor Authentication - 2FA 8

Same name and namespace in other branches
  1. 8.2 src/Form/MoAuthLoginSettings.php \Drupal\miniorange_2fa\Form\MoAuthLoginSettings

Hierarchy

Expanded class hierarchy of MoAuthLoginSettings

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

File

src/Form/MoAuthLoginSettings.php, line 13
Contains support form for miniOrange 2FA Login Module.

Namespace

Drupal\miniorange_2fa\Form
View source
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');
  }

}

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.
MoAuthLoginSettings::buildForm public function Form constructor. Overrides FormInterface::buildForm
MoAuthLoginSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MoAuthLoginSettings::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.