Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing SecurityManagerInterface for custom vote() implementations #48

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Component/Authorisation/SecurityManager.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @link https://github.com/codeconsortium/CCDNUserSecurityBundle
*
*/
class SecurityManager
class SecurityManager implements SecurityManagerInterface
{
/**
*
Expand Down Expand Up @@ -64,15 +64,10 @@ class SecurityManager
*/
protected $blockPages;

const ACCESS_ALLOWED = 0;
const ACCESS_DENIED_DEFER = 1;
const ACCESS_DENIED_BLOCK = 2;

/**
*
* @access public
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router
* @param \CCDNUser\SecurityBundle\Component\Authentication\Tracker\LoginFailureTracker $loginFailureTracker
* @param array $routeLogin
* @param array $forceAccountRecovery
Expand Down
42 changes: 42 additions & 0 deletions Component/Authorisation/SecurityManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the CCDNUser SecurityBundle
*
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
*
* Available on github <http://www.github.com/codeconsortium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CCDNUser\SecurityBundle\Component\Authorisation;

use Symfony\Component\HttpFoundation\RequestStack;
use CCDNUser\SecurityBundle\Component\Authentication\Tracker\LoginFailureTracker;

interface SecurityManagerInterface
{
const ACCESS_ALLOWED = 0;
const ACCESS_DENIED_DEFER = 1;
const ACCESS_DENIED_BLOCK = 2;

/**
* Constructor
*
* @access public
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \CCDNUser\SecurityBundle\Component\Authentication\Tracker\LoginFailureTracker $loginFailureTracker
* @param array $routeLogin
* @param array $forceAccountRecovery
* @param array $blockPages
*/
public function __construct(RequestStack $requestStack, LoginFailureTracker $loginFailureTracker, $routeLogin, $forceAccountRecovery, $blockPages);

/**
* @access public
* @return int
*/
public function vote();
}
7 changes: 4 additions & 3 deletions Component/Authorisation/Voter/ClientLoginVoter.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CCDNUser\SecurityBundle\Component\Authorisation\Voter;

use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

Expand All @@ -32,16 +33,16 @@ class ClientLoginVoter implements VoterInterface
/**
*
* @access protected
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
*/
protected $securityManager;

/**
*
* @access public
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $loginFailureTracker
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
*/
public function __construct($securityManager)
public function __construct(SecurityManagerInterface $securityManager)
{
$this->securityManager = $securityManager;
}
Expand Down
10 changes: 4 additions & 6 deletions Component/Listener/BlockingLoginListener.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

namespace CCDNUser\SecurityBundle\Component\Listener;

use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager;
use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
*
Expand All @@ -33,7 +32,7 @@ class BlockingLoginListener
/**
*
* @access protected
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
*/
protected $securityManager;

Expand All @@ -45,11 +44,10 @@ class BlockingLoginListener
/**
*
* @access public
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $loginFailureTracker
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
* @param \CCDNUser\SecurityBundle\Component\Listener\AccessDeniedExceptionFactoryInterface $exceptionFactory
*/
public function __construct(SecurityManager $securityManager, AccessDeniedExceptionFactoryInterface $exceptionFactory)
public function __construct(SecurityManagerInterface $securityManager, AccessDeniedExceptionFactoryInterface $exceptionFactory)
{
$this->securityManager = $securityManager;
$this->exceptionFactory = $exceptionFactory;
Expand Down
17 changes: 9 additions & 8 deletions Component/Listener/DeferLoginListener.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CCDNUser\SecurityBundle\Component\Listener;

use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager;
use CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -27,19 +27,19 @@ class DeferLoginListener
/**
*
* @access protected
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
* @var \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
*/
protected $securityManager;

/**
*
* @access public
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager $securityManager
* @param array $forceAccountRecovery
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \CCDNUser\SecurityBundle\Component\Authorisation\SecurityManagerInterface $securityManager
* @param array $forceAccountRecovery
*
*/
public function __construct(RouterInterface $router, SecurityManager $securityManager, array $forceAccountRecovery)
public function __construct(RouterInterface $router, SecurityManagerInterface $securityManager, array $forceAccountRecovery)
{
$this->router = $router;
$this->securityManager = $securityManager;
Expand All @@ -52,9 +52,10 @@ public function onKernelRequest(GetResponseEvent $event)
return;
}

$result = $this->securityManager->vote();
$securityManager = $this->securityManager; // Avoid the silly cryptic error 'T_PAAMAYIM_NEKUDOTAYIM'
$result = $securityManager->vote();

if ($result === SecurityManager::ACCESS_DENIED_DEFER) {
if ($result === $securityManager::ACCESS_DENIED_DEFER) {
$event->stopPropagation();

$redirectUrl = $this->router->generate(
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/CCDNUserSecurityExtension.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private function getComponentSection(ContainerBuilder $container, $config)
$container->setParameter('ccdn_user_security.component.authentication.handler.login_failure_handler.class', $config['component']['authentication']['handler']['login_failure_handler']['class']);
$container->setParameter('ccdn_user_security.component.authentication.tracker.login_failure_tracker.class', $config['component']['authentication']['tracker']['login_failure_tracker']['class']);

$container->setParameter('ccdn_user_security.component.authorisation.security_manager.class', $config['component']['authorisation']['security_manager']['class']);
$container->setParameter('ccdn_user_security.component.authorisation.voter.client_login_voter.class', $config['component']['authorisation']['voter']['client_login_voter']['class']);

$container->setParameter('ccdn_user_security.component.listener.blocking_login_listener.class', $config['component']['listener']['blocking_login_listener']['class']);
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ private function addComponentSection(ArrayNodeDefinition $node)
->addDefaultsIfNotSet()
->canBeUnset()
->children()
->arrayNode('security_manager')
->addDefaultsIfNotSet()
->canBeUnset()
->children()
->scalarNode('class')->defaultValue('CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager')->end()
->end()
->end()
->arrayNode('voter')
->addDefaultsIfNotSet()
->canBeUnset()
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services/components.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
# Access Decision Manager
#
ccdn_user_security.component.authorisation.security_manager:
class: 'CCDNUser\SecurityBundle\Component\Authorisation\SecurityManager'
class: %ccdn_user_security.component.authorisation.security_manager.class%
arguments:
- @request_stack
- @ccdn_user_security.component.authentication.tracker.login_failure_tracker
Expand Down