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.
Related
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...
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 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
This is how my security.yml looks like for access control list:
access_control:
- { path: ^/admin, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/admin, roles: ROLE_ADMIN }
What I want to do is that user must have both roles (ROLE_ADMIN and IS_AUTHENTICATED_FULLY) in order to access the path as defined. But with above rules, if the user has any one of the role, the user can access the path as defined which i dont want. I also tried giving rule as follow with no success:
- { path: ^/admin, roles:[ROLE_ADMIN,IS_AUTHENTICATED_FULLY] }
How can I add rule that requires user to have both roles in order to access the path defined ?
IS_AUTHENTICATED_FULLY
returns true when ever a user is actually authenticated.
Anonymous users are technically authenticated, meaning that the
isAuthenticated() method of an anonymous user object will return true.
To check if your user is actually authenticated, check for the
IS_AUTHENTICATED_FULLY role.
So if a user has a role ROLE_ADMIN and is logged in, he is fully authenticated. As a result there is no need to set this requirement:
- { path: ^/admin, roles: IS_AUTHENTICATED_FULLY }
because you have (see below) which includes beeing fully authenticated
- { path: ^/admin, roles: ROLE_ADMIN }
And
- { path: ^/admin, roles: IS_AUTHENTICATED_FULLY }
will allow any user to see the admin section.
Read: http://symfony.com/doc/current/book/security.html
Looking at the problem itself, not at your specific situation.
If you need user to have all specified roles to access some path, this needs more configuration, as default RoleVoter grants access if current security token has at least one of specified roles.
RoleVoter grants access if token has at least one of passed roles, but Security component passes each of specified roles individually to each of the voters. So to change OR behaviour to AND behaviour all you need to do is to change decition manager strategy:
# app/config/security.yml
security:
access_decision_manager:
# strategy can be: affirmative (default one), unanimous or consensus
strategy: unanimous # if any voter returns ACCESS_DENIED, access is denied
If i didn't get you wrong , i think hierarchical roles
is a better approach http://symfony.com/doc/current/book/security.html#hierarchical-roles) .
#Hierarchical Roles
Instead of associating many roles to users, you can define role inheritance rules by creating a role hierarchy:
YAML
app/config/security.yml
security:
...
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
ROLE_BOTH_ROLE_TOGETHER: [IS_AUTHENTICATED_FULLY,ROLE_ADMIN]
And them oyu can check for the hierarchy.
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