2

I have tried to used easyAdmin3 for making an admin account quickly, but how do you make a proper impersonate user action ?

I have tried a lot of things but the best option are made custom action so this link appear in page but it's don't works properly...

Impersonate works but on only page linked in url (impersonate has stopped if page change) and User don't change in Symfony Toolbar...

My custom Action :

    public function configureActions(Actions $actions): Actions
    {
        $impersonate = Action::new('impersonate', 'Impersonate')
            ->linkToRoute('web_account_index', function (User $entity) {
               return [
                    'id' => $entity->getId(),
                   '?_switch_user' => $entity->getEmail()
               ];
            })
        ;
        return parent::configureActions($actions)
            ->add(Crud::PAGE_INDEX, Action::DETAIL)
            ->add(Crud::PAGE_INDEX, $impersonate)
            ;
    }

Result : Dashboard link for each user

After click on impersonate, I have this url :

https://blog-community.wip/account/7?eaContext=37a8719&?_switch_user=user7@user.com

Content are ok (page account for user 7) but Symfony Profiler show User admin instead of impersonated User :

Symfony profiler user logged

Change page exit impersonate...

Real Symfony impersonate keep impersonation even if page changes because profiler user logged are different Symfony profiler user logged with impersonate directly in url

documentation not refer this functionality, EasyAdmin's Github issues too ans this website too.

Thanks for help

Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
Grandpere
  • 51
  • 9

3 Answers3

2

I am not a big fan of using hardcoded rules, so injected the UrlGenerator to my CrudController:

$impersonate = Action::new('impersonate', 'Impersonate')
    ->linkToUrl(function (User $user): string {
        return $this->urlGenerator->generate(
            Routes::DASHBOARD,
            ['_switch_user' => $user->getEmail()],
            UrlGeneratorInterface::ABSOLUTE_URL
        );
    });
OskarStark
  • 384
  • 1
  • 11
1

Solved !

EasyAdmin add automatically some parameters in url so "?" are already here but I added it too in my custom action...

Example :

https://blog-community.wip/account/7?eaContext=37a8719&?_switch_user=user7@user.com
    public function configureActions(Actions $actions): Actions
    {
        $impersonate = Action::new('impersonate', 'Impersonate')
            ->linkToRoute('web_account_index', function (User $entity) {
               return [
                    'id' => $entity->getId(),
                   '_switch_user' => $entity->getEmail() 
                   // removed ? before _switch_user
               ];
            })
        ;
        return parent::configureActions($actions)
            ->add(Crud::PAGE_INDEX, Action::DETAIL)
            ->add(Crud::PAGE_INDEX, $impersonate)
            ;
    }
Grandpere
  • 51
  • 9
1

For EasyAdmin 3.2.x the prior solution stopped working. This is working for me now:

public function configureActions(Actions $actions): Actions
{
    $impersonate = Action::new('impersonate', false, 'fa fa-fw fa-user-lock')
        //changed from linkToRoute to linkToUrl. note that linkToUrl has only one parameter.
        //"admin/.. can be adjusted to another URL"
        ->linkToUrl(function (User $entity) {
            return 'admin/?_switch_user='.$entity->getUsername();
        })
    ;

    $actions = parent::configureActions($actions);
    $actions->add(Crud::PAGE_INDEX, $impersonate);

    return $actions;
}
mvetter
  • 133
  • 2
  • 16