-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DX] Provide an easy way to check if a user has a security role #14048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
providing it where ? |
@stof quite honestly I have no idea :) That's why I was so vague in the solution details 8000 . Let's wait for the Symfony community to think about some amazing solution. |
It's a need I had in several situations, so 👍 for this helper. As to where to put it, the first place I'd look for such a method would be directly in A |
Given that we already have an |
Isn't this just something that should be handled by a voter via |
@Triiistan it cannot be in the UserInterface. your user cannot be aware of the role hierarchy (and it would force any user implementation to duplicate the logic). @iltar it is already handled by the voter (RoleVoter or RoleHierarchyVoter depending on whether you configure the hierarchy or no). this is why I'm asking where it should be provided |
I was confronted with the same issue a while back. I needed to query all users from the database with a specific role (not for anything security related), but since this role could be assigned trough inheritance I could not simply query the database for users having the role. Instead I created an inverted variant of the RoleHierarchy. It returns all roles that lead to a specific role. You can then query the database for users having one of these roles. Don't know whether it is useful but just in case, here is a gist: https://gist.github.com/Sander-Toonen/1e03b4e729bc1d3c982d Example: |
Something like a |
What about flattening the roles instead when logging in? That would make it run-time even better. In my custom authentication implementation, I've created a |
👍 for @iltar's idea |
/ping @weaverryan what do you think about what I propose here, would that be something you could easily implement in your Guard Authenticator? |
I think this is the current idea, just wanted to be sure: wouldn't the easiest (from a DX pov) solution be to just extend $o->isGranted("ROLE_ADMIN", null);
$o->isGrantedOnUser("ROLE_ADMIN", null, $user);
// or even just
$o->isGranted("ROLE_ADMIN", null, $someUser); Where in the case of |
I second @apfelbox's thoughts. And this is already possible via If you want to know if a user has a role, I think you should always go through the security system. If you use the My vote is to do nothing: use the |
Since #15870 was accepted to 2.8, you will now be able to use the class SomeVoter extends AbstractVoter
{
private $decisionManager;
// inject the security.access.decision_manager service
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function voteOnAttribute($attribute, $object, TokenInterface $token)
{
if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
return true;
}
}
} So, this is a documentation issue now - see symfony/symfony-docs#4389 @javiereguiluz I'm closing this issue - re-open it if you disagree. |
The problem
As many of you know, the
getRoles()
method of theUserInterface
doesn't take into account the role hierarchy that you may have defined in your application.That's why usually you must end up with a code similar to the following:
The solution
The obvious solution would be to provide a method such as
->hasRole(string $role_name)
. What do you think?The text was updated successfully, but these errors were encountered: