-
-
Notifications
You must be signed in to change notification settings - Fork 921
Allow to configure auth access from the resource class #938
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
Conversation
24cdab2
to
d120dcc
Compare
Great feature! |
@desmax good idea. |
(Old suggestion: #234) I think we should use the Security expression language: "access_control"="is_granted('ROLE_ADMIN')" Basically to have the same effect as http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html |
And this feature must be disabled when the Symfony Security component is not present. (Additionally, the Symfony Expression component too, if we go with that.) |
PR updated:
|
} | ||
|
||
if (null === $this->authorizationChecker) { | ||
throw new \LogicException('The "symfony/security" library must be installed to use the "is_granted" attribute.'); |
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.
Does it worth it to add something like "on property X on class Y" ? to ease debug ? (same for next exception)
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.
done
|
||
foreach ($isGranted as $expression) { | ||
if ($this->authorizationChecker->isGranted(new Expression($expression), $data)) { | ||
return; |
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.
This logic is a bit weird. If at least one expression is valid, then it's OK.
What if I want an and
between all my expressions? And In order to avoid dealing with this kind of decission, why not allowing only one is_granted
?
(Actually, I don't know, I'm just asking)
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.
Indeed, this is a rest of the old logic (without expression language) but it's better to only allow one expression now.
I'll update the PR.
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.
fixed
13f4922
to
ab0a07f
Compare
@dunglas Thanks for your work. This feature will be released on next version or do we need to use dev-master ? |
It will be in 2.1. |
Hi
|
@piotrbrzezina define your operation smth like this |
I have implemented mine as
I needed the extra check that if the logged user is an admin, they will be allowed to access the item operation. |
@desmax Thanks for the info. It seems that using it the way you suggest makes it possible to go through the custom security voter, however the Any suggestion how we can prevent this becoming a Expression? //EDIT It seems the DenyAccessListener class does this at the end: if (!$this->authorizationChecker->isGranted(new Expression($isGranted), $request->attributes->get('data'))) {
throw new AccessDeniedException();
} Perhaps we should find a way to check whether the |
Also, there's some useful stuff at https://api-platform.com/docs/core/extensions. |
I implementing complex authorization, i have 4 level configuration:
My solutions is creating custom event subscriber, then define "authorization" class on which @ApiResource definition. I create specific class for each entity who need custom authorization:
And inject class into custom event subscriber class via addMethod call:
it work great and flexible. I think we can implement this into core system |
@larkarvin What do you think about: /**
* @ApiResource(
* attributes={"is_granted"="has_role('ROLE_ADMIN')"},
* itemOperations={
* "get"={"method"="GET", "is_granted"="has_role('ROLE_ADMIN') || object.getOwner()
== user"}
* }
* )
* @ORM\Entity
*/
class Book
{
} |
Allow to configure auth access from the resource class
Edit: now with expression language support
This PR allows to setup authorization controls directly from the resource.
In the following exemple, the only logged in admins can access all operations related to the
Secured
resource. However, regular users owning a specific resource can also access to it.All variables available in Symfony's access control expressions are available. The
object
variable value is the current resource (or collection of resources).