Skip to content

Add example for using a voter to restrict switch_user #9353

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

Closed
wants to merge 4 commits into from
Closed

Add example for using a voter to restrict switch_user #9353

wants to merge 4 commits into from

Conversation

jwmickey
Copy link
Contributor

No description provided.

@jwmickey jwmickey changed the title [WIP] add example for using a voter to restrict switch_user Add example for using a voter to restrict switch_user Mar 10, 2018
@javiereguiluz javiereguiluz added this to the 4.1 milestone Mar 27, 2018

First, create the voter class::

namespace AppBundle\Security\Voter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace App\Security\Voter;to be coherent with SF4 structure


if (in_array('ROLE_CUSTOMER', $subject->getRoles())
&& $this->hasSwitchToCustomerRole($token)) {
return self::ACCESS_GRANTED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be return true; no?


// Explicitly configure the service
$container->getDefinition(SwitchToCustomerVoter::class)
->setArgument('$roleHierarchy', '@security.role_hierarchy');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be new Reference('security.role_hierarchy')

@jwmickey
Copy link
Contributor Author

jwmickey commented Apr 4, 2018

Thank you for reviewing this PR @atailouloute, I have added your recommendations.


private function hasSwitchToCustomerRole(TokenInterface $token)
{
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave out the role hierarchy here. It IMO adds more complexity than needed to understand the example.

@xabbuh
Copy link
Member

xabbuh commented Apr 10, 2018

I think we should add a headline before the new section to make it stand out more. And we should then also add a versionadded directive.

Copy link
Contributor

@ndench ndench left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. This is a cool idea, at my old job we just gave ROLE_SUPER_ADMIN to all the employees, which always felt gross. If only we'd thought of doing this!

@xabbuh xabbuh changed the base branch from master to 4.1 May 8, 2018 08:42
@@ -187,6 +187,179 @@ setting:
),
));

If you need more control over user switching, but don't require the complexity
of a full ACL implementation, you can use a security voter. For example, you
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces should be one voter. For example


Notice that when checking for the ``ROLE_CUSTOMER`` role on the target user, only the roles
explicitly assigned to the user are checked rather than checking all reachable roles from
the role hierarchy. The reason for this is to avoid accidentally granting access to an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double spaces: hierarchy. The reason

),
));

Thanks to autowiring, we only need to configure the role hierarchy argument when registering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only need to configure. Correct me if I'm wrong, but I believe the Symfony docs point towards "you", rather than "we".

Copy link
Contributor

@ndench ndench left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet example.

- removed role_heirarchy from switch user voter to simplify
- removed double-spaces between sentences.
- added versionadded
- added header
javiereguiluz added a commit that referenced this pull request Jan 4, 2019
…wmickey, javiereguiluz)

This PR was merged into the 4.1 branch.

Discussion
----------

Add example for using a voter to restrict switch_user

This recreates #9353, which has too many conflicts when merging it.

Commits
-------

7853ec0 Minor tweaks
876257b implemented community suggestions
e6c569b Fix recommendations from community review
cf3bd00 [WIP] add example for using a voter to restrict switch_user
@javiereguiluz
Copy link
Member

@jwmickey thanks a lot for contributing this nice example (and thanks to all reviewers!). I apologize for having taken so long to merge it.

I tried to merge but there were lots of conflicts ... so I created #10842 to replace this and that is now merged. Of course I kept your original commits so you'll get full credit of your contribution.

So, let's close this now that #10842 is merged. Thanks!

@jwmickey
Copy link
Contributor Author

jwmickey commented Jan 4, 2019

Thanks @javiereguiluz, glad this was able to be included!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants