How to get user roles before impersonation? - symfony

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)

Related

Sonata Admin - assign permissions to roles

I have lack of knowledge how Symfony ACL works, especially using Sonata...
Sonata has some permissions like:
LIST - view the list of objects
VIEW - view the detail of one object
CREATE - create a new object
EDIT - update an existing object
DELETE - delete an existing object
EXPORT - (for the native Sonata export links)
ALL- grants LIST, VIEW, CREATE, EDIT, DELETE and EXPORT
I also assigned my own created permissions for my own custom actions:
protected $accessMapping = array(
'VERIFY' => 'EDIT',
'UNVALIDATE' => 'EDIT',
'CLOSE' => 'EDIT'
);
As I understand I just need to grant user EDIT permission to be able to VERIFY, UNVALIDATE and CLOSE.
Admin users can have 3 roles:
security:
role_hierarchy:
ROLE_ADMIN: ROLE_SONATA_ADMIN
ROLE_SUPER_ADMIN: ROLE_ADMIN
Now depending on a role user should be able to have certain permissions.
E.g. ROLE_ADMIN should have LIST, VIEW, EDIT, EXPORT and ROLE_SUPER_ADMIN should be granted ALL.
How could I manage it?
P.S. I am NOT using SonataUserBundle!
What i usually do it just use SecurityVoters
http://www.branchbit.be/blog/using-custom-voters-in-sonata-admin
This gives you a simple way of checking roles, permissions, and object properties, and decide if a specific action, is allowed on a specififc object, by a specific user.

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

Symfony2: Roles, Users and Grants from Database

I'm beginning to use Symfony2 and I'm really enjoying it!
I have some questions for sure that you will help me easily!
When we use the security layer, the file security.yml we set the property access_control, usually something like this:
{Path: ^ / admin roles: ROLE_ADMIN}
Traditionally using php, my rule of access to the system I use 3 tables:
User - Users Table
Role - Roles Table
Resource - Resource Table
Permission - Grant Tables
Where, User has a role, and a permission is related to a role and a resource. To check whether the user has access to a resource, check the table permission.
Bringing Symfony2, the property "path" would be a resource and ROLE_ADMIN would be the role of the user.
How to do that security.yml, load the settings from the database. I searched the official documentation and found nothing.
For now, thanks
Actually, the way to "read" the path (in the security.yml file) is:
- { path: ^/this/(path|regex|here)$, roles: {CAN_BE_ACCESED_ONLY, BY_THESE_ROLES} }
now, from where do you know which user has which role?
From wherever you load your users.
e.g.:
public function getRoles()
{
return array('ROLE_USER');
}
Maybe you will find your answers here. It descripes how to load users from Database:
How to Load Security Users from the Database (the Entity Provider)
I would recommend to use FOSUserBundle. It is very easy to handle and helps you managing your Security in Symfony2
FOSUserBundle
Regards!

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