php - EasyAdmin 3 - Impersonate User in dashboard
Get the solution ↓↓↓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 :
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
Answer
Solution:
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;
}
Answer
Solution:
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&[email protected]
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)
;
}
Answer
Solution:
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
);
});
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your system folder path does not appear to be set correctly. please open the following file and correct this: index.php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.