Skip to content

Commit

Permalink
feature #5015 Added an example about how to get the impersonating use…
Browse files Browse the repository at this point in the history
…r object (javiereguiluz)

This PR was merged into the 2.3 branch.

Discussion
----------

Added an example about how to get the impersonating user object

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes
| Applies to    | 2.3+
| Fixed tickets | #4179

Commits
-------

e7c9605 Adding a break statement to improve the sample code
f64b5b5 Added an example about how to get the impersonating user object
  • Loading branch information
weaverryan committed Mar 14, 2015
2 parents c4027c5 + e7c9605 commit 86abdde
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cookbook/security/impersonating_user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ to show a link to exit impersonation:
</a>
<?php endif ?>

In some cases you may need to get the object that represents the impersonating
user rather than the impersonated user. Use the following snippet to iterate
over user's roles until you get the ``SwitchUserRole`` related to the
impersonating user::

use Symfony\Component\Security\Core\Role\SwitchUserRole;

$securityContext = $this->get('security.context');

if ($securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) {
foreach ($securityContext->getToken()->getRoles() as $role) {
if ($role instanceof SwitchUserRole) {
$impersonatingUser = $role->getSource()->getUser();
break;
}
}
}

Of course, this feature needs to be made available to a small group of users.
By default, access is restricted to users having the ``ROLE_ALLOWED_TO_SWITCH``
role. The name of this role can be modified via the ``role`` setting. For
Expand Down

0 comments on commit 86abdde

Please sign in to comment.