Skip to content
Snippets Groups Projects
Commit 52c04eb5 authored by Akshay Singh's avatar Akshay Singh
Browse files
parent 53572471
No related branches found
No related tags found
No related merge requests found
services:
age_verification:
class: \Drupal\age_verification\EventSubscriber\PathGate
arguments: ['@path.matcher', '@current_user']
arguments: ['@path.matcher', '@current_user', '@path.current', '@request_stack', '@config.factory', '@state']
tags:
- { name: 'event_subscriber' }
......@@ -2,13 +2,17 @@
namespace Drupal\age_verification\EventSubscriber;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Path\PathMatcherInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Http\RequestStack;
/**
* Event Subscriber PathGate.
......@@ -28,26 +32,64 @@ class PathGate implements EventSubscriberInterface {
*/
protected $currentUser;
/**
* The current patch.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
/**
* The request method.
*
* @var \Drupal\Core\Http\RequestStack
*/
protected $request;
/**
* Config property.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The state property.
*
* @var Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructs a new Redirect404Subscriber.
*
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
* @param \Drupal\Core\Path\PathMatcherInterface $pathMatcher
* The path matcher service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
* @param \Drupal\Core\Path\CurrentPathStack $currentPath
* The current patch.
* @param \Drupal\Core\Http\RequestStack $request
* The request property.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config property.
* @param \Drupal\Core\State\StateInterface $state
* The stateInterface property.
*/
public function __construct(PathMatcherInterface $path_matcher, AccountInterface $current_user) {
$this->pathMatcher = $path_matcher;
public function __construct(PathMatcherInterface $pathMatcher, AccountInterface $current_user, CurrentPathStack $currentPath, RequestStack $request, ConfigFactoryInterface $configFactory, StateInterface $state) {
$this->pathMatcher = $pathMatcher;
$this->currentUser = $current_user;
$this->currentPath = $currentPath;
$this->request = $request;
$this->configFactory = $configFactory;
$this->state = $state;
}
/**
* Code that should be triggered on event specified.
*/
public function onRespond(FilterResponseEvent $event) {
// The response event occurs when a response is created for replying.
// You can override or add extra HTTP headers in here.
$session = \Drupal::request()->getSession();
$session = $this->request->getCurrentRequest()->getSession();
$age_verified = $session->get('age_verified');
......@@ -60,7 +102,7 @@ class PathGate implements EventSubscriberInterface {
return;
}
// Don't run when site is in maintenance mode.
if (\Drupal::state()->get('system.maintenance_mode')) {
if ($this->state->get('system.maintenance_mode')) {
return;
}
// Ignore non index.php requests(like cron).
......@@ -68,15 +110,14 @@ class PathGate implements EventSubscriberInterface {
return;
}
// Get saved settings and other needed objects.
$config = \Drupal::config('age_verification.settings');
$config = $this->configFactory->get('age_verification.settings');
if ($config->get('age_verification_user_agents') !== NULL) {
// Exploding the age_verification_user_agents field to separate lines.
$user_agents = explode("\n", $config->get('age_verification_user_agents'));
$http_user_agent = \Drupal::request()->server->get('HTTP_USER_AGENT');
$http_user_agent = $this->request->getCurrentRequest()->server->get('HTTP_USER_AGENT');
// Performing cleanup, trim white space and empty lines.
foreach ($user_agents as $key => $user_agent) {
foreach ($user_agents as $user_agent) {
// To be sure we match proper string, we need to trim it.
$user_agent = !empty($user_agent) ? trim($user_agent) : "";
if ($http_user_agent == $user_agent) {
......@@ -96,15 +137,15 @@ class PathGate implements EventSubscriberInterface {
// Append the urls to skips with some hardcoded urls.
$skipPaths = $skip_urls_config . "\r\n" . implode("\r\n", $skip_urls);
$request_path = \Drupal::service('path.current')->getPath();
$request_path = $this->currentPath->getPath();
// Check if paths don't match then redirect to age verification form.
$match = \Drupal::service('path.matcher')->matchPath($request_path, $skipPaths);
$is_front = \Drupal::service('path.matcher')->isFrontPage();
$match = $this->pathMatcher->matchPath($request_path, $skipPaths);
$is_front = $this->pathMatcher->isFrontPage();
// If not front page then append the path alias as a destination parameter.
if ($is_front == FALSE) {
$current_uri = \Drupal::request()->getRequestUri();
$current_uri = $this->request->getCurrentRequest()->getRequestUri();
$destination = '?destination=' . $current_uri;
}
else {
......
......@@ -2,14 +2,40 @@
namespace Drupal\age_verification\Form;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Http\RequestStack;
use Drupal\Core\Form\FormBase;
/**
* Class of Age Verification Form.
*/
class AgeVerificationForm extends FormBase {
/**
* The request method.
*
* @var \Drupal\Core\Http\RequestStack
*/
protected $request;
/**
* Constructs a new request property.
*
* @param \Drupal\Core\Http\RequestStack $request
* The request property.
*/
public function __construct(RequestStack $request) {
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('request_stack'));
}
/**
* {@inheritdoc}
*/
......@@ -96,7 +122,7 @@ class AgeVerificationForm extends FormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Add TRUE to session age_verified.
$session = \Drupal::request()->getSession();
$session = $this->request->getCurrentRequest()->getSession();
$session->set('age_verified', 1);
// Add a redirect to requested page. Using $form_state built in redirects.
$redirect = $session->get('age_verification_path');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment