Sonata Admin: Let ROLE_SONATA_ADMIN edit entities of all users (ACL) - symfony

In SonataAdmin I have granted user a role: ROLE_SONATA_ADMIN
Also I have granted him: ROLE_SONATA_ADMIN_CITY_ADMIN
Unfortunately this user cannot edit City entities created by other users. Any guess how to fix this?

Related

How to secure admin specific area to specific role using SonataAdmin

I have a working SonataAdminBundle installation, meanings my admin is well secured and cannot be accessed by simple users.
- { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
I have created some admin sections in the backoffice.
But, I don't want all of these sections to be available to each admin users. To explain a little bit, my website will allow to build custom websites. So I don't want a website admin to be able to access all websites informations. So I need to restrict some parts of the admin to super admin only.
In order to do that, I tried to limit the access to one of the section to Super admin only
- { path: ^/admin/app/site/, role: [ROLE_SUPER_ADMIN]}
Here is my role hierarchy
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
SONATA:
- ROLE_SONATA_PAGE_ADMIN_PAGE_EDI
The problem is, if I create a new user with my super admin account and give him the ROLE_ADMIN.
Then this user is still able to access my admin/app/site/* section so obviously it's not working and I can't figure it why.
EDIT: I have the feelings this can't be done without using ACL...but I don't really need ACL that much...

Issue with cascading roles in symfony3 and sonata user bundle

I have my roles defined as follows:
security:
role_hierarchy:
ROLE_PROFESSIONAL_SERVICES_MANAGER: [ROLE_USER, ROLE_SONATA_ADMIN, ROLE_BRANCH_ASSISTANT]
When I try to check for permissions in my controller as below:
if($securityContext->isGranted('ROLE_PROFESSIONAL_SERVICES_MANAGER'))
The returns false because the in the profiler, the permissions appear to have been saved as shown below therefore:
"ROLE_PROFESSIONAL_SERVICES_MANAGER: ROLE_USER, ROLE_SONATA_ADMIN, ROLE_BRANCH_ASSISTANT"
Anyone know how I can resolve this?
Roles are for users not the app.
With your code you are defining that a user with role ROLE_PROFFESSIONAL_SERVICES_MANAGER will have the roles ROLE_USER, ROLE_SONATA_ADMIN and ROLE_BRANCH_ASSISTANT.
But now you need to assign that role to a user and sign that user in.
When you call $securityContext->isGranted() what it does is get the token from your current logged in user (wich you can see in the profiler) and check the roles of that user from the token.
So you need to have a user with the proper role assigned signed in.
If your already using SonatAUserBundle or FOSUserBundle use that command to create a user easily: bin/console fos:user:create
Then assign it the proper roles:
app/console fos:user:promote nameOfYourUser ROLE_PROFFESSIONAL_SERVICES_MANAGER
If your app has no user management system have a look here: https://sonata-project.org/bundles/admin/master/doc/reference/security.html#user-management

Sonata Admin restrict action if user type XXX

I'm using Sonata Admin for a project and i have a problem.
I'm using FosUserBundle and i have several type of users, ex Admin and Superviser.
I have two entities User and Document.
An account type Admin can manage Document and User, but I want a Superviser can only manage Document.
How can I do that ?
When I connect with a Superviser, I don't want to have the choice to create edit etc... a user
Thanks !
Sonata Admin Docs - Security
Looks like your situation can be handled with Roles:
Incorporate FOSUserBundle into Sonata Admin with SonataUserBundle (you may already have done this)
Set up appropriate roles in security.yml. These are standard Symfony Roles, but following a special Sonata Admin naming convention, e.g. (adapted from docs with your Entities):
app/config/security.yml
security:
...
role_hierarchy:
#collect specific Sonata permissions together for convenience
ROLE_SONATA_USER:
- ROLE_SONATA_ADMIN_DEMO_USER_LIST
- ROLE_SONATA_ADMIN_DEMO_USER_VIEW
- ROLE_SONATA_ADMIN_DEMO_USER_CREATE
- ROLE_SONATA_ADMIN_DEMO_USER_EDIT
- ROLE_SONATA_ADMIN_DEMO_USER_DELETE
ROLE_SONATA_DOCUMENT:
- ROLE_SONATA_ADMIN_DEMO_DOCUMENT_LIST
- ROLE_SONATA_ADMIN_DEMO_DOCUMENT_VIEW
- ROLE_SONATA_ADMIN_DEMO_DOCUMENT_CREATE
- ROLE_SONATA_ADMIN_DEMO_DOCUMENT_EDIT
- ROLE_SONATA_ADMIN_DEMO_DOCUMENT_DELETE
#roles actually assigned to users
ROLE_SUPERVISER: [ROLE_USER, ROLE_SONATA_DOCUMENT]
ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_DOCUMENT, ROLE_SONATA_USER]
Assign ROLE_SUPERVISER and ROLE_ADMIN to users (via FOSUserBundle commands or $user->addRole)
These are then used by Sonata to limit access to functions, and e.g. to show which panels are appropriate on the main dashboard, and you yourself can test them to customised your own views etc

User with multiple roles, access always denied

Users may have multiple roles, e.g. ROLE_USER, ROLE_SUBSCRIBTION_FOO, ROLE_SUBSCRIBTION_BAR.
Based on their role I define an access control list:
- { path: ^/admin/helpdesk/foo, roles: ROLE_SUBSCRIPTION_FOO }
- { path: ^/admin/helpdesk/index, roles: ROLE_ADMIN }
The role hierarchy
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUBSCRIBTION_FOO: ROLE_ADMIN
ROLE_SUPER_ADMIN: ROLE_ADMIN
The problem now is, when user has the role ROLE_SUBSCRIBTION_FOO and accesses /admin/helpdesk/foo access is denied. The user has both ROLE_ADMIN and ROLE_SUBSCRIBTION_FOO. However when I have
- { path: ^/admin/helpdesk/foo, roles: ROLE_ADMIN }
it works but I need it to be
- { path: ^/admin/helpdesk/foo, roles: ROLE_SUBSCRIPTION_FOO }
which does not work, howeve the user does have the role? This is kinda weired. Any ideas where the problem is?
It looks like a typo to me. You defined ROLE_SUBSCRIBTION_FOO in your hierarchy (with a B) but you're wanting to restrict the path with ROLE_SUBSCRIPTION_FOO (with a P).
Looking at the setup everything seems right.
I want to change user roles dynamically from admin area of my application. So e.g. I give the FOO role to user BOB and expect the changes to take effect immediately.
But this does not work. The currently open session of user BOB is not refreshed. He has to reauthenticate himself. After reauthentication (logout and login again) symfonys security system will compare the role correctly with the given access list.
So I expected the user session to be updated automatically, but this is not possible with the default security system of symfony. I think it needs to be extended with database based session management. This way you could refresh the user session.

The Symfony2 application automatically logs out a user which has more than one role defined

I have a Symfony2 application that uses roles to manage authorization. It all works fine when I login with a user that only has one role associated with him, but when I try with one that has more than one role, the application logs him out automatically.
I really don't know what code sample to provide, so any question or suggestion is welcome.
Why don't you define ROLE HIERARCHY ?
so you can have only 1 role, but stacking the permission of role under it.
Something like :
security.yml :
role_hierarchy:
ROLE_SUPER_ADMIN: ROLE_ADMIN
ROLE_ADMIN: ROLE_MANAGER
ROLE_MANAGER: ROLE_USER
The ROLE_SUPER_ADMIN will have all the role under (admin / manager / user).
The documentation for the hierachical role is here : http://symfony.com/doc/current/book/security.html#hierarchical-roles

Resources