-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from TobyRyuk/master
Removed deprecated calls
- Loading branch information
Showing
10 changed files
with
274 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'".' | ||
); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
Tests/Fixtures/Entity/ClassForClassAnnotationsComaSeparated.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
@@ -23,6 +25,7 @@ public function UniqueRole() | |
/** | ||
* @Secure(roles="ROLE_USER_ADD") | ||
* @Secure(roles="ROLE_USER_REMOVE") | ||
* | ||
* @return bool | ||
*/ | ||
public function MultiRolesMultiLines() | ||
|
@@ -32,6 +35,7 @@ public function MultiRolesMultiLines() | |
|
||
/** | ||
* @Secure(roles="ROLE_USER_DUPLICATE,ROLE_USER_ACCESS") | ||
* | ||
* @return bool | ||
*/ | ||
public function MultiRolesComaSeparated() | ||
|
@@ -41,10 +45,11 @@ public function MultiRolesComaSeparated() | |
|
||
/** | ||
* @Secure(roles="ROLE_USER_LOGIN") | ||
* | ||
* @return bool | ||
*/ | ||
public function RoleWithoutFireWall() | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.