Skip to content

Commit

Permalink
bug #5450 Fixing "Undefined method" error in code example (nebkam)
Browse files Browse the repository at this point in the history
This PR was submitted for the 2.7 branch but it was merged into the 2.6 branch instead (closes #5450).

Discussion
----------

Fixing "Undefined method" error in code example

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.7
| Fixed tickets | -

The code example tries to call `getToken` on `Symfony\Component\Security\Core\Authorization\AuthorizationChecker`, which fails on Symfony 2.7 because the method is undefined.
This change utilizes the TokenStorage class, introduced in Symfony 2.6

Commits
-------

fa3936b Fixing "Undefined method" error on Symfony 2.7
  • Loading branch information
xabbuh committed Jun 28, 2015
2 parents 58ca209 + fa3936b commit 9dafd45
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cookbook/security/impersonating_user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ over the user's roles until you find one that a ``SwitchUserRole`` object::
use Symfony\Component\Security\Core\Role\SwitchUserRole;

$authChecker = $this->get('security.authorization_checker');
$tokenStorage = $this->get('security.token_storage');

if ($authChecker->isGranted('ROLE_PREVIOUS_ADMIN')) {
foreach ($authChecker->getToken()->getRoles() as $role) {
foreach ($tokenStorage->getToken()->getRoles() as $role) {
if ($role instanceof SwitchUserRole) {
$impersonatingUser = $role->getSource()->getUser();
break;
Expand Down

0 comments on commit 9dafd45

Please sign in to comment.