Skip to content

Commit

Permalink
Merge pull request #11 from TobyRyuk/master
Browse files Browse the repository at this point in the history
Removed deprecated calls
  • Loading branch information
mevdschee committed Mar 8, 2016
2 parents f94f307 + acf4b57 commit fa00ce8
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 313 deletions.
5 changes: 3 additions & 2 deletions Annotation/Secure.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Lsw\SecureControllerBundle\Annotation;

/**
* @Annotation
*/
class Secure
{
public $roles = "";
public $roles = '';

public function __construct(array $values)
{
Expand All @@ -16,4 +17,4 @@ public function __construct(array $values)

$this->roles = $values['roles'];
}
}
}
6 changes: 3 additions & 3 deletions DependencyInjection/LswSecureControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Symfony\Component\DependencyInjection\Loader;

/**
* {@inheritDoc}
* {@inheritdoc}
*/
class LswSecureControllerExtension extends Extension
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -22,7 +22,7 @@ public function load(array $configs, ContainerBuilder $container)
}

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function getAlias()
{
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ parameters:
services:
secure_controller.controller_listener:
class: %secure_controller.controller_listener.class%
arguments: [ "@annotation_reader", "@security.context" ]
arguments: [ "@annotation_reader", "@security.authorization_checker", "@security.token_storage" ]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
78 changes: 51 additions & 27 deletions Security/ControllerListener.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,91 @@
<?php

namespace Lsw\SecureControllerBundle\Security;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Util\ClassUtils;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Acl\Util\ClassUtils;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Doctrine\Common\Annotations\Reader;
use Lsw\SecureControllerBundle\Annotation\Secure;

class ControllerListener
{
/**
* @var Reader
*/
private $annotationReader;
private $securityContext;

public function __construct(Reader $annotationReader, SecurityContextInterface $securityContext)
{
$this->annotationReader = $annotationReader;
$this->securityContext = $securityContext;

/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;

/**
* @var TokenStorageInterface
*/
private $tokenStorage;

/**
* ControllerListener constructor.
*
* @param Reader $annotationReader
* @param AuthorizationCheckerInterface $authorizationChecker
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(
Reader $annotationReader,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage
) {
$this->annotationReader = $annotationReader;
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
}

public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();

list($object, $method) = $controller;

// the controller could be a proxy, e.g. when using the JMSSecurityExtraBundle or JMSDiExtraBundle
$className = ClassUtils::getRealClass($object);

$reflectionClass = new \ReflectionClass($className);
$reflectionMethod = $reflectionClass->getMethod($method);

$classAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass);
$methodsAnnotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);

$allAnnotations = array_merge($classAnnotations,$methodsAnnotations);
$secureAnnotations = array_filter($allAnnotations, function($annotation) {
$allAnnotations = array_merge($classAnnotations, $methodsAnnotations);

$secureAnnotations = array_filter($allAnnotations, function ($annotation) {
return $annotation instanceof Secure;
});

foreach ($secureAnnotations as $secureAnnotation) {
if (!$this->securityContext->getToken()) {
$filename = $reflectionClass->getFileName();
throw new AuthenticationCredentialsNotFoundException(
if (!$this->tokenStorage->getToken()) {
$filename = $reflectionClass->getFileName();
throw new AuthenticationCredentialsNotFoundException(
'@Secure(...) annotation found without firewall on "'.$method.'" in "'.$filename.'"'
);
}
$roles = explode(',',$secureAnnotation->roles);
foreach ($roles as $role)
{
$roles = explode(',', $secureAnnotation->roles);
foreach ($roles as $role) {
$role = trim($role);

if (!$role) continue;
if (!$this->securityContext->isGranted($role)) {

if (!$role) {
continue;
}
if (!$this->authorizationChecker->isGranted($role)) {
throw new AccessDeniedException(
'Current user is not granted required role "'.$role.'".'
);
}
}

}
}

}
9 changes: 6 additions & 3 deletions Tests/Fixtures/Entity/ClassForClassAnnotation.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

namespace Lsw\SecureControllerBundle\Tests\Fixtures\Entity;

use Lsw\SecureControllerBundle\Annotation\Secure;

/**
* Class ClassForClassAnnotation
* Note that any annotation needed for tests is coming from a mock. So these are only visualise what's going on behind.
*
* @Secure(roles="ROLE_USER_EDIT")
* @package Lsw\SecureControllerBundle\Tests\Fixtures\Entity
*
* @author Grégoire Hébert <[email protected]>
*/
class ClassForClassAnnotation
{
public function stubMethod(){
public function stubMethod()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

namespace Lsw\SecureControllerBundle\Tests\Fixtures\Entity;

use Lsw\SecureControllerBundle\Annotation\Secure;

/**
* Class ClassForClassAnnotationsComaSeparated
* Note that any annotation needed for tests is coming from a mock. So these are only visualise what's going on behind.
*
* @Secure(roles="ROLE_USER_EDIT,ROLE_USER_ADD")
* @package Lsw\SecureControllerBundle\Tests\Fixtures\Entity
*
* @author Grégoire Hébert <[email protected]>
*/
class ClassForClassAnnotationsComaSeparated
{
public function stubMethod(){
public function stubMethod()
{
}
}
}
9 changes: 6 additions & 3 deletions Tests/Fixtures/Entity/ClassForClassAnnotationsMultiLines.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php

namespace Lsw\SecureControllerBundle\Tests\Fixtures\Entity;

use Lsw\SecureControllerBundle\Annotation\Secure;

/**
* Class ClassForClassAnnotationsMultiLines
* Note that any annotation needed for tests is coming from a mock. So these are only visualise what's going on behind.
*
* @Secure(roles="ROLE_USER_EDIT")
* @Secure(roles="ROLE_USER_ADD")
* @package Lsw\SecureControllerBundle\Tests\Fixtures\Entity
*
* @author Grégoire Hébert <[email protected]>
*/
class ClassForClassAnnotationsMultiLines
{
public function stubMethod(){
public function stubMethod()
{
}
}
}
9 changes: 7 additions & 2 deletions Tests/Fixtures/Entity/ClassForMethodsAnnotations.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php

namespace Lsw\SecureControllerBundle\Tests\Fixtures\Entity;

use Lsw\SecureControllerBundle\Annotation\Secure;

/**
* Class ClassForMethodsAnnotations
* Note that any annotation needed for tests is coming from a mock. So these are only visualise what's going on behind.
* @package Lsw\SecureControllerBundle\Tests\Fixtures\Entity
*
* @author Grégoire Hébert <[email protected]>
*/
class ClassForMethodsAnnotations
{
/**
* @Secure(roles="ROLE_USER_EDIT")
*
* @return bool
*/
public function UniqueRole()
Expand All @@ -23,6 +25,7 @@ public function UniqueRole()
/**
* @Secure(roles="ROLE_USER_ADD")
* @Secure(roles="ROLE_USER_REMOVE")
*
* @return bool
*/
public function MultiRolesMultiLines()
Expand All @@ -32,6 +35,7 @@ public function MultiRolesMultiLines()

/**
* @Secure(roles="ROLE_USER_DUPLICATE,ROLE_USER_ACCESS")
*
* @return bool
*/
public function MultiRolesComaSeparated()
Expand All @@ -41,10 +45,11 @@ public function MultiRolesComaSeparated()

/**
* @Secure(roles="ROLE_USER_LOGIN")
*
* @return bool
*/
public function RoleWithoutFireWall()
{
return true;
}
}
}
Loading

0 comments on commit fa00ce8

Please sign in to comment.