Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cookbook/security/impersonating_user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ 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');
Copy link
Contributor

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 deprecated

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

yes

Copy link
Contributor

Choose a reason for hiding this comment

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


if ($securityContext->isGranted('ROLE_PREVIOUS_ADMIN')) {
foreach ($securityContext->getToken()->getRoles() as $role) {
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 use the new service as well

if ($role instanceof SwitchUserRole) {
$impersonatingUser = $role->getSource()->getUser();
}
Copy link
Member

Choose a reason for hiding this comment

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

after this, you can add a break, can't you?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. Much better with the break. Done in e7c9605.

}
}

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