You are here

class AccessFilterMiddleware in Access Filter 8

Provides a HTTP middleware to implement access filtering.

Hierarchy

  • class \Drupal\access_filter\AccessFilterMiddleware implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of AccessFilterMiddleware

1 string reference to 'AccessFilterMiddleware'
access_filter.services.yml in ./access_filter.services.yml
access_filter.services.yml
1 service uses AccessFilterMiddleware
access_filter.middleware in ./access_filter.services.yml
Drupal\access_filter\AccessFilterMiddleware

File

src/AccessFilterMiddleware.php, line 16

Namespace

Drupal\access_filter
View source
class AccessFilterMiddleware implements HttpKernelInterface {

  /**
   * The decorated kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new AccessFilterMiddleware object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The decorated kernel.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(HttpKernelInterface $http_kernel, ModuleHandlerInterface $module_handler) {
    $this->httpKernel = $http_kernel;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    if (!Settings::get('access_filter_disabled')) {
      $this->moduleHandler
        ->loadAll();
      $filters = Filter::loadMultiple();
      foreach ($filters as $filter) {
        if ($filter
          ->status()) {
          $filter
            ->parse();
          if (!$filter
            ->isAllowed($request)) {
            $code = $filter
              ->get('parsedResponse')['code'];
            if (in_array($code, [
              301,
              302,
            ])) {
              return new RedirectResponse($filter
                ->get('parsedResponse')['redirect_url'], $code);
            }
            else {
              return new Response($filter
                ->get('parsedResponse')['body'], $code);
            }
          }
        }
      }
    }
    return $this->httpKernel
      ->handle($request, $type, $catch);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessFilterMiddleware::$httpKernel protected property The decorated kernel.
AccessFilterMiddleware::$moduleHandler protected property The module handler.
AccessFilterMiddleware::handle public function
AccessFilterMiddleware::__construct public function Constructs a new AccessFilterMiddleware object.