-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Security] Add $methods
support to #[IsGranted]
to restrict access by HTTP method
#61359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Conversation
* @param string|Expression|\Closure(IsGrantedContext, mixed $subject):bool $attribute The attribute that will be checked against a given authentication token and optional subject | ||
* @param array|string|Expression|\Closure(array<string,mixed>, Request):mixed|null $subject An optional subject - e.g. the current object being voted on | ||
* @param string|null $message A custom message when access is not granted | ||
* @param int|null $statusCode If set, will throw HttpKernel's HttpException with the given $statusCode; if null, Security\Core's AccessDeniedException will be used | ||
* @param int|null $exceptionCode If set, will add the exception code to thrown exception | ||
* @param string[]|string $methods HTTP methods to apply validation to. Empty array means all methods are allowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I ran php-cs-fixer
, it automatically updated the PHPDoc
blocks.
If it's preferred to handle those changes in a separate PR, just let me know 🙏
This is not a bug fix. |
66c7bd5
to
4cc3417
Compare
…s by HTTP method
4cc3417
to
739e36e
Compare
$methods = array_map('strtoupper', $attribute->methods); | ||
if ($methods && !\in_array($request->getMethod(), $methods, true)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$methods = array_map('strtoupper', $attribute->methods); | |
if ($methods && !\in_array($request->getMethod(), $methods, true)) { | |
if ($attribute->methods && !\in_array($request->getMethod(), array_map('strtoupper', $attribute->methods), true)) { |
@@ -24,19 +24,24 @@ | |||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)] | |||
final class IsGranted | |||
{ | |||
public readonly array $methods; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should have @var string[]
to document the exact type (especially as this is a public API)
Description
This PR adds support for restricting
#[IsGranted]
validation to specific HTTP methods via a new$methods
argument.What's New
You can now define access control per HTTP method directly in the
#[IsGranted]
attribute. This allows greater flexibility when securing controller actions that handle multiple HTTP verbs.This change aligns
#[IsGranted]
more closely with other HTTP-aware attributes like:#[IsCsrfTokenValid]
#[IsSignatureValid]
(currently under review)