我曾尝试使用easyAdmin3快速创建管理员帐户,但如何进行适当的模拟用户操作?
我已经尝试了很多东西,但最好的选择是自定义动作,所以这个链接出现在页面上,但它不能正常工作…
模拟工作,但只在链接在网址的页面(模拟已停止,如果页面更改)和用户不更改Symfony工具栏...
我的自定义操作:
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)
;
}结果:Dashboard link for each user
点击impersonate后,我得到了这个url:
https://blog-community.wip/account/7?eaContext=37a8719&?_switch_user=user7@user.com内容正常(用户7的页面帐户),但Symfony Profiler显示用户admin而不是冒充的用户:
更改页面退出模拟...
Real Symfony impersonate保留模拟,即使页面更改,因为profiler用户记录的是不同的Symfony profiler user logged with impersonate directly in url
文档没有提到这个功能,EasyAdmin的Github问题也在这个网站上。
感谢你的帮助
发布于 2020-07-17 19:42:39
解决了!
EasyAdmin会自动在url中添加一些参数,因此"?“已经在这里了,但我也在我的自定义操作中添加了它...
示例:
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)
;
}发布于 2021-01-10 12:59:34
对于EasyAdmin 3.2.x,以前的解决方案停止工作。这对我来说很有效:
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;
}发布于 2021-06-23 14:53:35
我不太喜欢使用硬编码规则,所以在我的CrudController中注入了UrlGenerator:
$impersonate = Action::new('impersonate', 'Impersonate')
->linkToUrl(function (User $user): string {
return $this->urlGenerator->generate(
Routes::DASHBOARD,
['_switch_user' => $user->getEmail()],
UrlGeneratorInterface::ABSOLUTE_URL
);
});https://stackoverflow.com/questions/62936077
复制相似问题