You are here

login_disable.module in Login Disable 2.0.x

Same filename and directory in other branches
  1. 8 login_disable.module
  2. 6 login_disable.module
  3. 7 login_disable.module

Control who can log in to a Drupal site.

File

login_disable.module
View source
<?php

/**
 * @file
 * Control who can log in to a Drupal site.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\UserInterface;

/**
 * Implements hook_help().
 */
function login_disable_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.login_diable':
      $text = file_get_contents(dirname(__FILE__) . '/README.md');
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
  }
  return NULL;
}

/**
 * Login_disable_form_user_login_block_alter function.
 */
function login_disable_form_user_login_block_alter(&$form, FormStateInterface $form_state) {
  _login_disable_form_user_login_alter($form);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function login_disable_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _login_disable_form_user_login_alter($form);
}

/**
 * Login_disable_form_user_login_alter function.
 */
function _login_disable_form_user_login_alter(&$form) {
  if (\Drupal::config('login_disable.settings')
    ->get('login_disable_is_active')) {
    $key = \Drupal::config('login_disable.settings')
      ->get('login_disable_key');

    // If the key is set, block people from logging in if it was not passed as
    // an argument in the URL.
    if (!empty($key) && !array_key_exists($key, $_GET)) {
      $form['name']['#disabled'] = 'disabled';
      $form['pass']['#disabled'] = 'disabled';
      unset($form['#validate']);
      unset($form['#submit']);
      unset($form['actions']);
    }
    \Drupal::messenger()
      ->addWarning(\Drupal::config('login_disable.settings')
      ->get('login_disable_message'), FALSE);
  }
}

/**
 * Implements hook_user_login().
 */
function login_disable_user_login(UserInterface $account) {
  if (\Drupal::config('login_disable.settings')
    ->get('login_disable_is_active')) {

    // If user is not allowed to bypass then log them out.
    if (\Drupal::currentUser()
      ->hasPermission('bypass disabled login') == FALSE) {
      \Drupal::messenger()
        ->addWarning(\Drupal::config('login_disable.settings')
        ->get('login_disable_message'));
      user_logout();
    }
  }
}

Functions

Namesort descending Description
login_disable_form_user_login_block_alter Login_disable_form_user_login_block_alter function.
login_disable_form_user_login_form_alter Implements hook_form_FORM_ID_alter().
login_disable_help Implements hook_help().
login_disable_user_login Implements hook_user_login().
_login_disable_form_user_login_alter Login_disable_form_user_login_alter function.