Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
age_verification
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
age_verification
Commits
52c04eb5
Commit
52c04eb5
authored
2 years ago
by
Akshay Singh
Browse files
Options
Downloads
Patches
Plain Diff
#3332721
parent
53572471
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
age_verification.services.yml
+1
-1
1 addition, 1 deletion
age_verification.services.yml
src/EventSubscriber/PathGate.php
+62
-21
62 additions, 21 deletions
src/EventSubscriber/PathGate.php
src/Form/AgeVerificationForm.php
+28
-2
28 additions, 2 deletions
src/Form/AgeVerificationForm.php
with
91 additions
and
24 deletions
age_verification.services.yml
+
1
−
1
View file @
52c04eb5
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'
}
This diff is collapsed.
Click to expand it.
src/EventSubscriber/PathGate.php
+
62
−
21
View file @
52c04eb5
...
...
@@ -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
_m
atcher
* @param \Drupal\Core\Path\PathMatcherInterface $path
M
atcher
* 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
_m
atcher
,
AccountInterface
$current_user
)
{
$this
->
pathMatcher
=
$path
_m
atcher
;
public
function
__construct
(
PathMatcherInterface
$path
M
atcher
,
AccountInterface
$current_user
,
CurrentPathStack
$currentPath
,
RequestStack
$request
,
ConfigFactoryInterface
$configFactory
,
StateInterface
$state
)
{
$this
->
pathMatcher
=
$path
M
atcher
;
$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
::
r
equest
()
->
server
->
get
(
'HTTP_USER_AGENT'
);
$http_user_agent
=
$this
->
request
->
getCurrentR
equest
()
->
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
->
current
Path
->
getPath
();
// Check if paths don't match then redirect to age verification form.
$match
=
\Drupal
::
service
(
'
path
.m
atcher
'
)
->
matchPath
(
$request_path
,
$skipPaths
);
$is_front
=
\Drupal
::
service
(
'
path
.m
atcher
'
)
->
isFrontPage
();
$match
=
$this
->
path
M
atcher
->
matchPath
(
$request_path
,
$skipPaths
);
$is_front
=
$this
->
path
M
atcher
->
isFrontPage
();
// If not front page then append the path alias as a destination parameter.
if
(
$is_front
==
FALSE
)
{
$current_uri
=
\Drupal
::
r
equest
()
->
getRequestUri
();
$current_uri
=
$this
->
request
->
getCurrentR
equest
()
->
getRequestUri
();
$destination
=
'?destination='
.
$current_uri
;
}
else
{
...
...
This diff is collapsed.
Click to expand it.
src/Form/AgeVerificationForm.php
+
28
−
2
View file @
52c04eb5
...
...
@@ -2,14 +2,40 @@
namespace
Drupal\age_verification\Form
;
use
Drupal\Core\Form\FormBas
e
;
use
Symfony\Component\DependencyInjection\ContainerInterfac
e
;
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
::
r
equest
()
->
getSession
();
$session
=
$this
->
request
->
getCurrentR
equest
()
->
getSession
();
$session
->
set
(
'age_verified'
,
1
);
// Add a redirect to requested page. Using $form_state built in redirects.
$redirect
=
$session
->
get
(
'age_verification_path'
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment