Question: collectionOperations / access_control
See original GitHub issueWhen trying to control access in Api Platform the documentation states you can do the following in order to limit Item Operations:
/**
* @ApiResource(
* attributes={"access_control"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get"={"method"="GET"},
* "post"={"method"="POST", "access_control"="is_granted('ROLE_ADMIN')"}
* },
* itemOperations={
* "get"={"method"="GET", "access_control"="is_granted('ROLE_USER') and object.owner == user"}
* }
* )
*/
There is however, no mention for how you would limit a collection by the current user. I tried the following but it fails, presumably because the collection is an array, with no direct access to the interior objects, so object.user doesn’t exist.
/**
* @ApiResource(
* collectionOperations={
* "get"={"method"="GET", "access_control"="is_granted('ROLE_USER') and object.getOwner() == user"}
* }
* )
*/
hydra:description: Unable to call method “owner” of object "ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator
I was wondering if there is a built-in way of getting around this?
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Problem Access_control on collectionOperations(GET) #602
I setted annotation on my entity but nothing happen. it seems that Access control annotation is not supported... I have access_control on ...
Read more >ApiResource access_control > API Platform Part 2: Security
Open up that entity: src/Entity/CheeseListing.php . We already have an itemOperations key, which we used to remove the delete operation and also to...
Read more >Api Platform access_control : adding role is not working
I'm trying to say that my access control is working perfectly at other places except in my API PLATFORM entities.
Read more >API Platform Crash Course Part 17: Role-based Access Control
ℹ This is a crash course on the API Platform which is intended to get you writing your own API's as quickly as...
Read more >Security - API Platform
The API Platform security layer is built on top of the Symfony Security component. All its features, including global access control directives are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I use extension to automaticaly filter collection results in addition to access filter based on the connected user https://api-platform.com/docs/core/extensions/#extensions
Thank you, I think I found a slightly clearer solution (but again I’m unsure if its best practice). In the doc’s example they do a check to see if the
$resourceClass == Offers
, however this ties it to that class. So instead of this I’ve got the follow which checks if the method exists dynamically:I hope this helps someone else at some point, and thank you @antograssiot for your help.