-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added an example about how to get the impersonating user object #5015
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should use the new service as well |
||
if ($role instanceof SwitchUserRole) { | ||
$impersonatingUser = $role->getSource()->getUser(); | ||
break; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after this, you can add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. Much better with the |
||
} | ||
} | ||
|
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be
'security.authorization_checker'
since'security.context'
is deprecatedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right ... but this doc is aimed for Symfony 2.3 and the
security.context
service was added in 2.6. I guess mergers should take care of this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #5015 (comment)