Skip to content

Commit b6a6201

Browse files
Maximilian Zumbansennicolas-grekas
authored andcommitted
[Security] Add argument $exceptionCode to #[IsGranted]
1 parent 296d05c commit b6a6201

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

Attribute/IsGranted.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function __construct(
4242
* If null, Security\Core's AccessDeniedException will be used.
4343
*/
4444
public ?int $statusCode = null,
45+
46+
/**
47+
* If set, will add the exception code to thrown exception.
48+
*/
49+
public ?int $exceptionCode = null,
4550
) {
4651
}
4752
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `RememberMeBadge` to `JsonLoginAuthenticator` and enable reading parameter in JSON request body
8+
* Add argument `$exceptionCode` to `#[IsGranted]`
89

910
6.2
1011
---

EventListener/IsGrantedAttributeListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)
6666
$message = $attribute->message ?: sprintf('Access Denied by #[IsGranted(%s)] on controller', $this->getIsGrantedString($attribute));
6767

6868
if ($statusCode = $attribute->statusCode) {
69-
throw new HttpException($statusCode, $message);
69+
throw new HttpException($statusCode, $message, code: $attribute->exceptionCode ?? 0);
7070
}
7171

72-
$accessDeniedException = new AccessDeniedException($message);
72+
$accessDeniedException = new AccessDeniedException($message, code: $attribute->exceptionCode ?? 403);
7373
$accessDeniedException->setAttributes($attribute->attribute);
7474
$accessDeniedException->setSubject($subject);
7575

Tests/EventListener/IsGrantedAttributeListenerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,50 @@ public function testIsGrantedWithRequestAsSubject()
384384
$listener = new IsGrantedAttributeListener($authChecker, new ExpressionLanguage());
385385
$listener->onKernelControllerArguments($event);
386386
}
387+
388+
public function testHttpExceptionWithExceptionCode()
389+
{
390+
$this->expectException(HttpException::class);
391+
$this->expectExceptionMessage('Exception Code');
392+
$this->expectExceptionCode(10010);
393+
394+
$authChecker = $this->createMock(AuthorizationCheckerInterface::class);
395+
$authChecker->expects($this->any())
396+
->method('isGranted')
397+
->willReturn(false);
398+
399+
$event = new ControllerArgumentsEvent(
400+
$this->createMock(HttpKernelInterface::class),
401+
[new IsGrantedAttributeMethodsController(), 'exceptionCodeInHttpException'],
402+
[],
403+
new Request(),
404+
null
405+
);
406+
407+
$listener = new IsGrantedAttributeListener($authChecker);
408+
$listener->onKernelControllerArguments($event);
409+
}
410+
411+
public function testAccessDeniedExceptionWithExceptionCode()
412+
{
413+
$this->expectException(AccessDeniedException::class);
414+
$this->expectExceptionMessage('Exception Code');
415+
$this->expectExceptionCode(10010);
416+
417+
$authChecker = $this->createMock(AuthorizationCheckerInterface::class);
418+
$authChecker->expects($this->any())
419+
->method('isGranted')
420+
->willReturn(false);
421+
422+
$event = new ControllerArgumentsEvent(
423+
$this->createMock(HttpKernelInterface::class),
424+
[new IsGrantedAttributeMethodsController(), 'exceptionCodeInAccessDeniedException'],
425+
[],
426+
new Request(),
427+
null
428+
);
429+
430+
$listener = new IsGrantedAttributeListener($authChecker);
431+
$listener->onKernelControllerArguments($event);
432+
}
387433
}

Tests/Fixtures/IsGrantedAttributeMethodsController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function notFound()
4545
{
4646
}
4747

48+
#[IsGranted(attribute: 'ROLE_ADMIN', message: 'Exception Code Http', statusCode: 404, exceptionCode: 10010)]
49+
public function exceptionCodeInHttpException()
50+
{
51+
}
52+
53+
#[IsGranted(attribute: 'ROLE_ADMIN', message: 'Exception Code Access Denied', exceptionCode: 10010)]
54+
public function exceptionCodeInAccessDeniedException()
55+
{
56+
}
57+
4858
#[IsGranted(attribute: new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'), subject: 'post')]
4959
public function withExpressionInAttribute($post)
5060
{

0 commit comments

Comments
 (0)