[Security] Add argument $exceptionCode
to #[IsGranted]
#49721
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In my current project, we transform HttpExceptions into a custom JsonResponse with fields like
success: false, message: "No access to account.", exceptionCode: 10010
. To throw the exception, the IsGranted-attribute is used with a custom voter. The problem here is, that, in order to return a specific exception-code, we have to throw the Http-exception inside the voter, instead of just returningfalse
as intended.This is only viable in our case, since we have priority as our access decision strategy.
My suggestion here is to extend the properties of IsGranted by
exceptionCode
and throw a HttpException inside the IsGrantedListener, whenever the property is used. So the same behaviour as with thestatusCode
property.Example:
#[IsGranted(attribute: 'ROLE_ADMIN', message: 'No access to account.', statusCode: 403, exceptionCode: 10010)]