AccessFilterMiddleware.php in Access Filter 8
File
src/AccessFilterMiddleware.php
View source
<?php
namespace Drupal\access_filter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Site\Settings;
use Drupal\access_filter\Entity\Filter;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class AccessFilterMiddleware implements HttpKernelInterface {
protected $httpKernel;
protected $moduleHandler;
public function __construct(HttpKernelInterface $http_kernel, ModuleHandlerInterface $module_handler) {
$this->httpKernel = $http_kernel;
$this->moduleHandler = $module_handler;
}
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);
}
}