Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86abdde

Browse files
committed
feature#5015 Added an example about how to get the impersonating user 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 |#4179Commits-------e7c9605 Adding a break statement to improve the sample codef64b5b5 Added an example about how to get the impersonating user object
2 parentsc4027c5 +e7c9605 commit86abdde

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎cookbook/security/impersonating_user.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ to show a link to exit impersonation:
8686
</a>
8787
<?php endif ?>
8888

89+
In some cases you may need to get the object that represents the impersonating
90+
user rather than the impersonated user. Use the following snippet to iterate
91+
over user's roles until you get the ``SwitchUserRole`` related to the
92+
impersonating user::
93+
94+
use Symfony\Component\Security\Core\Role\SwitchUserRole;
95+
96+
$securityContext = $this->get('security.context');
97+
98+
if ($securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) {
99+
foreach ($securityContext->getToken()->getRoles() as $role) {
100+
if ($role instanceof SwitchUserRole) {
101+
$impersonatingUser = $role->getSource()->getUser();
102+
break;
103+
}
104+
}
105+
}
106+
89107
Of course, this feature needs to be made available to a small group of users.
90108
By default, access is restricted to users having the ``ROLE_ALLOWED_TO_SWITCH``
91109
role. The name of this role can be modified via the ``role`` setting. For

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp