You are here

restrict_ip.module in Restrict IP 8

File

restrict_ip.module
View source
<?php

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\block\BlockInterface;

/**
 * Implementation of hook_block_access()
 *
 * Blocks access to all blocks except the system_main_block
 * if the user does not have access to the current page.
 */
function restrict_ip_block_access(BlockInterface $block, $operation, AccountInterface $account) {
  if ($operation == 'view') {
    if (\Drupal::service('restrict_ip.service')
      ->userIsBlocked()) {
      if ($block
        ->getPluginId() != 'system_main_block') {
        return AccessResult::forbidden();
      }
    }
  }
  return AccessResult::neutral();
}

/**
 * Override of template_preprocess_page()
 *
 * This function unsets tabs and various
 * other page elements for blocked users
 * so they are not rendered
 */
function restrict_ip_preprocess_page(&$build) {
  if (\Drupal::service('restrict_ip.service')
    ->userIsBlocked()) {
    $regions = system_region_list(\Drupal::service('theme.manager')
      ->getActiveTheme()
      ->getName(), REGIONS_ALL);
    unset($regions['content']);
    $whitelisted_regions = Drupal::service('module_handler')
      ->invokeAll('restrict_ip_whitelisted_regions');
    foreach ($whitelisted_regions as $wr) {
      unset($regions[$wr]);
    }
    foreach (array_keys($regions) as $region) {
      if (isset($build['page'][$region])) {
        unset($build['page'][$region]);
      }
    }
    if (isset($build['tabs'])) {
      if (isset($build['tabs']['#primary'])) {
        $build['tabs']['#primary'] = [];
      }
      if (isset($build['tabs']['#secondary'])) {
        $build['tabs']['#primary'] = [];
      }
    }
    $build['title_prefix'] = [];
    $build['title_suffix'] = [];
    $build['main_menu'] = [];
    $build['secondary_menu'] = [];
    $build['action_links'] = [];
  }
}

/**
 * Override of template_preprocess_html()
 *
 * This function unsets $page_top and $page_bottom
 * so that they are not passed to html.tpl.php,
 * preventing these regions from being rendered
 */
function restrict_ip_preprocess_html(&$build) {
  if (\Drupal::service('restrict_ip.service')
    ->userIsBlocked()) {
    if (isset($build['page_top'])) {
      unset($build['page_top']);
    }
    if (isset($build['page_top'])) {
      unset($build['page_bottom']);
    }
  }
}

/**
 * Implementation of hook_js_alter()
 *
 * This function removes all javascript from the page
 * with the exception of jQuery and any javascript
 * provided by this module.
 */
function restrict_ip_js_alter(&$javascript, AttachedAssetsInterface $assets) {
  if (\Drupal::service('restrict_ip.service')
    ->userIsBlocked()) {
    $whitelisted_js_keys = [
      'core/assets/vendor/jquery/jquery.min.js',
      'core/assets/vendor/jquery/jquery.js',
      drupal_get_path('module', 'restrict_ip') . '/js/mail_fix.js',
    ];
    $whitelisted_js_keys += Drupal::service('module_handler')
      ->invokeAll('restrict_ip_whitelisted_js_keys');
    foreach (array_keys($javascript) as $key) {
      if (!in_array($key, $whitelisted_js_keys)) {
        unset($javascript[$key]);
      }
    }
  }
}