Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyAdmin 3 - Impersonate User in dashboard

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&[email protected]

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

like image 891
Grandpere Avatar asked Oct 26 '25 01:10

Grandpere


1 Answers

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
        );
    });
like image 125
OskarStark Avatar answered Oct 28 '25 15:10

OskarStark