Skip to content

Commit b92365d

Browse files
committed
Merge pull request #23 from krizon/exception-factory
Introduce exception factory to allow customization of exception thrown when ip is blocked
2 parents 41f0774 + 4beb97c commit b92365d

6 files changed

Lines changed: 69 additions & 6 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser SecurityBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\SecurityBundle\Component\Listener;
15+
16+
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
18+
class AccessDeniedExceptionFactory implements AccessDeniedExceptionFactoryInterface
19+
{
20+
public function createAccessDeniedException()
21+
{
22+
return new HttpException(500, 'flood control - login blocked');
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser SecurityBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\SecurityBundle\Component\Listener;
15+
16+
interface AccessDeniedExceptionFactoryInterface
17+
{
18+
/**
19+
* Create exception thrown when a ip is blocked
20+
*
21+
* @return \Exception
22+
*/
23+
public function createAccessDeniedException();
24+
}

Component/Listener/BlockingLoginListener.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace CCDNUser\SecurityBundle\Component\Listener;
1515

16-
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager;
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1818
use Symfony\Component\HttpFoundation\RedirectResponse;
1919
use Symfony\Component\Routing\RouterInterface;
@@ -52,18 +52,25 @@ class BlockingLoginListener
5252
*/
5353
protected $securityManager;
5454

55+
/**
56+
* @var AccessDeniedExceptionFactoryInterface
57+
*/
58+
protected $exceptionFactory;
59+
5560
/**
5661
*
5762
* @access public
58-
* @param \Symfony\Component\Routing\RouterInterface $router
59-
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $loginFailureTracker
60-
* @param array $forceAccountRecovery
63+
* @param \Symfony\Component\Routing\RouterInterface $router
64+
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $loginFailureTracker
65+
* @param \CCDNUser\SecurityBundle\Component\Listener\AccessDeniedExceptionFactoryInterface $exceptionFactory
66+
* @param array $forceAccountRecovery
6167
*/
62-
public function __construct(RouterInterface $router, $securityManager, $forceAccountRecovery)
68+
public function __construct(RouterInterface $router, SecurityManager $securityManager, AccessDeniedExceptionFactoryInterface $exceptionFactory, $forceAccountRecovery)
6369
{
6470
$this->securityManager = $securityManager;
6571
$this->router = $router;
6672
$this->forceAccountRecovery = $forceAccountRecovery;
73+
$this->exceptionFactory = $exceptionFactory;
6774
}
6875

6976
/**
@@ -101,7 +108,7 @@ public function onKernelRequest(GetResponseEvent $event)
101108
if ($result == $securityManager::ACCESS_DENIED_BLOCK) {
102109
$event->stopPropagation();
103110

104-
throw new HttpException(500, 'flood control - login blocked');
111+
throw $this->exceptionFactory->createAccessDeniedException();
105112
}
106113
}
107114
}

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.access_denied_exception_factory.class', $config['component']['listener']['blocking_login_listener']['access_denied_exception_factory']);
169170

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

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ private function addComponentSection(ArrayNodeDefinition $node)
315315
->canBeUnset()
316316
->children()
317317
->scalarNode('class')->defaultValue('CCDNUser\SecurityBundle\Component\Listener\BlockingLoginListener')->end()
318+
->scalarNode('access_denied_exception_factory')->defaultValue('CCDNUser\SecurityBundle\Component\Listener\AccessDeniedExceptionFactory')->end()
318319
->end()
319320
->end()
320321
->end()

Resources/config/services/components.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,20 @@ services:
6161
tags:
6262
- { name: security.voter }
6363

64+
#
65+
# Blocking login Listener.
66+
#
6467
ccdn_user_security.component.listener.blocking_login_listener:
6568
class: %ccdn_user_security.component.listener.blocking_login_listener.class%
6669
arguments:
6770
- @router
6871
- @ccdn_user_security.component.authorisation.security_manager
72+
- @ccdn_user_security.component.access_denied_exception_factory
6973
- %ccdn_user_security.login_shield.force_account_recovery%
7074
tags:
7175
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
76+
ccdn_user_security.component.access_denied_exception_factory:
77+
class: %ccdn_user_security.component.access_denied_exception_factory.class%
7278

7379
#
7480
# Referer Listener.

0 commit comments

Comments
 (0)