Symfony2 - Setting user access restriction to a specific entity
-
20-12-2019 - |
Pregunta
I'm working a project where the main page is a portal, and each link is a client's blog page.
My question, is how would I setup access restriction for each client to their specific blog page?
e.g. Main page portal
- Link1 -> blog1 page
- Link2 -> blog2 page
- Link3 -> blog3 page
I need to figure out a way to limit CRUD access per user to their specific blog only.
For example:
User1 -> access restricted to Blog1 page
User2 -> access restricted to Blog2 page
User3 -> access restricted to Blog3 page
For setup: I started going with 1 category entity and linking to post entity, but now I'm thinking that there would be no way to limit access to just one category entity/post entity.
Any ideas on how to best handle this? (use different category entities and post entities or is there a bundle that takes care of what I'm trying to do?)
Solución
You could try and leverage the existing SF2 access control feature: Roles
A user that only is allowed to read Blog1 could have the role ROLE_USER_BLOG1
.
Another one that is allowed to create articles might have ROLE_ADMIN_BLOG1
.
And a third one that is allowed to read all blogs could have ROLE_USER_BLOG_ALL
.
Together with a custom voter an implementation for your system should be straightforward. ;-)