Skip to content

Commit aecaa5a

Browse files
committed
Merge pull request #41 from ricbra/fix_block
Refactored BlockingLoginListener into DeferLoginListener and BlockingLoginListener
2 parents 6486b65 + 8f9b00a commit aecaa5a

8 files changed

Lines changed: 108 additions & 36 deletions

File tree

Component/Listener/BlockingLoginListener.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager;
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1818
use Symfony\Component\HttpFoundation\RedirectResponse;
19-
use Symfony\Component\Routing\RouterInterface;
2019

2120
/**
2221
*
@@ -31,20 +30,6 @@
3130
*/
3231
class BlockingLoginListener
3332
{
34-
/**
35-
*
36-
* @access protected
37-
* @var \Symfony\Component\Routing\RouterInterface $router
38-
*/
39-
protected $router;
40-
41-
/**
42-
*
43-
* @access protected
44-
* @var array $forceAccountRecovery
45-
*/
46-
protected $forceAccountRecovery;
47-
4833
/**
4934
*
5035
* @access protected
@@ -63,13 +48,10 @@ class BlockingLoginListener
6348
* @param \Symfony\Component\Routing\RouterInterface $router
6449
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $loginFailureTracker
6550
* @param \CCDNUser\SecurityBundle\Component\Listener\AccessDeniedExceptionFactoryInterface $exceptionFactory
66-
* @param array $forceAccountRecovery
6751
*/
68-
public function __construct(RouterInterface $router, SecurityManager $securityManager, AccessDeniedExceptionFactoryInterface $exceptionFactory, $forceAccountRecovery)
52+
public function __construct(SecurityManager $securityManager, AccessDeniedExceptionFactoryInterface $exceptionFactory)
6953
{
7054
$this->securityManager = $securityManager;
71-
$this->router = $router;
72-
$this->forceAccountRecovery = $forceAccountRecovery;
7355
$this->exceptionFactory = $exceptionFactory;
7456
}
7557

@@ -94,17 +76,6 @@ public function onKernelRequest(GetResponseEvent $event)
9476
return;
9577
}
9678

97-
if ($result == $securityManager::ACCESS_DENIED_DEFER) {
98-
$event->stopPropagation();
99-
100-
$redirectUrl = $this->router->generate(
101-
$this->forceAccountRecovery['route_recover_account']['name'],
102-
$this->forceAccountRecovery['route_recover_account']['params']
103-
);
104-
105-
$event->setResponse(new RedirectResponse($redirectUrl));
106-
}
107-
10879
if ($result == $securityManager::ACCESS_DENIED_BLOCK) {
10980
$event->stopPropagation();
11081

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace CCDNUser\SecurityBundle\Component\Listener;
4+
5+
use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager;
6+
use Symfony\Component\HttpFoundation\RedirectResponse;
7+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8+
use Symfony\Component\HttpKernel\HttpKernelInterface;
9+
use Symfony\Component\Routing\RouterInterface;
10+
11+
class DeferLoginListener
12+
{
13+
/**
14+
*
15+
* @access protected
16+
* @var \Symfony\Component\Routing\RouterInterface $router
17+
*/
18+
protected $router;
19+
20+
/**
21+
*
22+
* @access protected
23+
* @var array $forceAccountRecovery
24+
*/
25+
protected $forceAccountRecovery;
26+
27+
/**
28+
*
29+
* @access protected
30+
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
31+
*/
32+
protected $securityManager;
33+
34+
/**
35+
*
36+
* @access public
37+
* @param \Symfony\Component\Routing\RouterInterface $router
38+
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
39+
* @param array $forceAccountRecovery
40+
*
41+
*/
42+
public function __construct(RouterInterface $router, SecurityManager $securityManager, array $forceAccountRecovery)
43+
{
44+
$this->router = $router;
45+
$this->securityManager = $securityManager;
46+
$this->forceAccountRecovery = $forceAccountRecovery;
47+
}
48+
49+
public function onKernelRequest(GetResponseEvent $event)
50+
{
51+
if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
52+
return;
53+
}
54+
55+
$result = $this->securityManager->vote();
56+
57+
if ($result === SecurityManager::ACCESS_DENIED_DEFER) {
58+
$event->stopPropagation();
59+
60+
$redirectUrl = $this->router->generate(
61+
$this->forceAccountRecovery['route_recover_account']['name'],
62+
$this->forceAccountRecovery['route_recover_account']['params']
63+
);
64+
65+
$event->setResponse(new RedirectResponse($redirectUrl));
66+
}
67+
}
68+
}

DependencyInjection/CCDNUserSecurityExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ private function getComponentSection(ContainerBuilder $container, $config)
166166

167167
$container->setParameter('ccdn_user_security.component.listener.route_referer_listener.class', $config['component']['listener']['route_referer_listener']['class']);
168168
$container->setParameter('ccdn_user_security.component.listener.blocking_login_listener.class', $config['component']['listener']['blocking_login_listener']['class']);
169+
$container->setParameter('ccdn_user_security.component.listener.defer_login_listener.class', $config['component']['listener']['defer_login_listener']['class']);
169170
$container->setParameter('ccdn_user_security.component.access_denied_exception_factory.class', $config['component']['listener']['blocking_login_listener']['access_denied_exception_factory']);
170171

171172
$container->setParameter('ccdn_user_security.component.route_referer_ignore.chain.class', $config['component']['route_referer_ignore']['chain']['class']);

DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ private function addComponentSection(ArrayNodeDefinition $node)
310310
->scalarNode('class')->defaultValue('CCDNUser\SecurityBundle\Component\Listener\RouteRefererListener')->end()
311311
->end()
312312
->end()
313+
->arrayNode('defer_login_listener')
314+
->addDefaultsIfNotSet()
315+
->canBeUnset()
316+
->children()
317+
->scalarNode('class')->defaultValue('CCDNUser\SecurityBundle\Component\Listener\DeferLoginListener')->end()
318+
->end()
319+
->end()
313320
->arrayNode('blocking_login_listener')
314321
->addDefaultsIfNotSet()
315322
->canBeUnset()

Resources/config/services/components.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,25 @@ services:
6767
ccdn_user_security.component.listener.blocking_login_listener:
6868
class: %ccdn_user_security.component.listener.blocking_login_listener.class%
6969
arguments:
70-
- @router
7170
- @ccdn_user_security.component.authorisation.security_manager
7271
- @ccdn_user_security.component.access_denied_exception_factory
73-
- %ccdn_user_security.login_shield.force_account_recovery%
7472
tags:
75-
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
73+
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 9 }
7674
ccdn_user_security.component.access_denied_exception_factory:
7775
class: %ccdn_user_security.component.access_denied_exception_factory.class%
7876

77+
#
78+
# Defer login Listener.
79+
#
80+
ccdn_user_security.component.listener.defer_login_listener:
81+
class: %ccdn_user_security.component.listener.defer_login_listener.class%
82+
arguments:
83+
- @router
84+
- @ccdn_user_security.component.authorisation.security_manager
85+
- %ccdn_user_security.login_shield.force_account_recovery%
86+
tags:
87+
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
88+
7989
#
8090
# Referer Listener.
8191
#

Resources/views/home.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<p>Hi on this test home page</p>
4+
</body>
5+
</html>

Tests/Functional/app/config/routing.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ fos_user_change_password:
2121
ccdn_user_security_circumvent_login:
2222
pattern: /circumvent_login
2323
defaults: { _controller: CCDNUserSecurityBundle:TestLogin:circumvent }
24+
25+
home:
26+
path: /
27+
defaults:
28+
_controller: FrameworkBundle:Template:template
29+
template: 'CCDNUserSecurityBundle::home.html.twig'

features/user_login.feature

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Feature: Check Blocking Functionalities
3737
And I circumvent login with "user1@foo.com" and "wrongpass"
3838
And I should not be logged in
3939
And I circumvent login with "user1@foo.com" and "wrongpass"
40-
And I should not be logged in
41-
And I should be blocked
42-
40+
Then I should be blocked
41+
And I go to "/login"
42+
Then I should be blocked
43+
And I circumvent login with "user1@foo.com" and "root"
44+
Then I should be blocked
45+
And I go to "/"
46+
Then I should not be logged in

0 commit comments

Comments
 (0)