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
Related
I am trying to build a system where some user groups are allowed to switch to a certain number of users, and the admin group can switch to ANY user. My user manager is provided by FOSUserBundle I have set up security voters to achieve this, to check the user roles after the switch has occurred. I created a new voter attribute called 'IMPERSONATION' and called denyAccessUnlessGranted on the first line of the main (homepage) controller that is run after the user switching.
Inside my voter, I first get the user objects for both the impersonated and impersonator users. I try to get the impersonator user by doing the following (as described here).
foreach ($tokenStorage->getToken()->getRoles() as $role) {
if ($role instanceof SwitchUserRole) {
$impersonatorUser = $role->getSource()->getUser();
break;
}
Then, I want to return true (grant access) if the $impersonatorUser is a super admin:
if ($impersonatorUser->isSuperAdmin()) { return true; }
// ..
// ... other voter logic
However, when I try to switch user from a user that has the ROLE_SUPER_ADMIN role to a user that has a non-admin role, this line is never executed. When I do a var_dump, I found that $impersonatorUser->isSuperAdmin() returns false, and $impersonatorUser->getRoles shows that the user only has the ROLE_USER role. And since the logic after this line returns false, the voter denies access and the switch results in Access Denied error.
The user seems to have lost the ROLE_SUPER_ADMIN role after switching. Is this a bug with FosUserBundle?
My role hierarchies are set as follows:
role_hierarchy:
ROLE_ADMIN: ROLE_MENTOR
ROLE_SUPER_ADMIN: ROLE_ADMIN
The user switching option is set as follows:
switch_user: { role: ROLE_MENTOR }
How can I check for the roles of the user before switching, so I can control who can switch users?
FOSUserBundle only provides a user management system, it has nothing to do with Symfony Security and access management.
If you use symfony switch user feature to impersonate user, then you'll have an extra role ROLE_PREVIOUS_ADMIN on your impersonated user, which can be checked (see documentation)
I don't know the best solution to implement your feature, but perhaps this could help (Allow switching/impersonating only to particular users in Symfony2)
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
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.
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
i have installed correctly the FOSUserBundle
but my questions are 2
for logged with a admin?
and i not understand how set some user to admin
thx
Security in Symfony2 is mainly based on user roles, so if the user has admin roles as defined in your security configuration you should have 'admin' user. Maybe you should look into the docs.
http://symfony.com/doc/current/book/security.html#roles
In case you are using SonataAdminBundle check http://sonata-project.org/bundles/admin/master/doc/reference/security.html