Skip to content

Commit a45ccd0

Browse files
committed
feature #19473 [Security] Expose the required roles in AccessDeniedException (Nicofuma)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Security] Expose the required roles in AccessDeniedException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Nowadays it is more and more common to protect some sensitive actions and part of a website using 2FA or some re-authentication mechanism (per example, on Github you have to enter your password again when you add an ssh key). But currently, in Symfony, it is really hard to implement without having to duplicate the logic, provide an explicit list of URLs to protect or hack into the security component. A good way to achieve that would be to add a special role (like IS_AUTHENTICATED_FULLY) and use it in the access map. But it requires us to be able to have a custom logic in an ExceptionListener depending on the roles behind an AccessDeniedException. With this patch we could write an ExceptionListener of this kind (a similar logic could also be used in an AccessDeniedHandler): ```php public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); do { if ($exception instanceof AccessDeniedException) { foreach ($exception->getAttributes() as $role) { if ($role === 'IS_AUTHENTICATED_2FA' && !$this->accessDecisionManager->decide($this->tokenStorage->getToken(), $role, $exception->getObject())) { // Start 2FA } } } } while (null !== $exception = $exception->getPrevious()); } ``` Replaces #18661 Commits ------- 6618c18 [Security] Expose the required roles in AccessDeniedException
2 parents 43e1aa7 + 603f26b commit a45ccd0

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Controller/Controller.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ protected function isGranted($attributes, $object = null)
192192
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
193193
{
194194
if (!$this->isGranted($attributes, $object)) {
195-
throw $this->createAccessDeniedException($message);
195+
$exception = $this->createAccessDeniedException($message);
196+
$exception->setAttributes($attributes);
197+
$exception->setObject($object);
198+
199+
throw $exception;
196200
}
197201
}
198202

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"symfony/filesystem": "~2.8|~3.0",
3030
"symfony/finder": "~2.8|~3.0",
3131
"symfony/routing": "~3.0",
32-
"symfony/security-core": "~2.8|~3.0",
32+
"symfony/security-core": "~3.2",
3333
"symfony/security-csrf": "~2.8|~3.0",
3434
"symfony/stopwatch": "~2.8|~3.0",
3535
"symfony/templating": "~2.8|~3.0",

0 commit comments

Comments
 (0)