File tree 1 file changed +17
-0
lines changed
1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,23 @@ to show a link to exit impersonation:
86
86
</a>
87
87
<?php endif ?>
88
88
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
+ }
103
+ }
104
+ }
105
+
89
106
Of course, this feature needs to be made available to a small group of users.
90
107
By default, access is restricted to users having the ``ROLE_ALLOWED_TO_SWITCH ``
91
108
role. The name of this role can be modified via the ``role `` setting. For
You can’t perform that action at this time.
0 commit comments