【问题标题】:EasyAdmin 3 - Impersonate User in dashboardEasyAdmin 3 - 在仪表板中模拟用户
【发布时间】:2020-11-06 04:53:21
【问题描述】:

我尝试使用 easyAdmin3 快速创建管理员帐户,但是如何进行正确的模拟用户操作?

我已经尝试了很多东西,但最好的选择是自定义操作,所以这个链接出现在页面中,但它不能正常工作......

模拟有效,但仅在 url 中链接的页面上(如果页面更改,模拟已停止)并且用户在 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

点击模拟后,我有这个网址:

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

内容正常(用户 7 的页面帐户),但 Symfony Profiler 显示用户管理员而不是模拟用户:

Symfony profiler user logged

更改页面退出模拟...

Real Symfony impersonate 即使页面更改也会保持模拟,因为登录的分析器用户不同Symfony profiler user logged with impersonate directly in url

文档没有提到这个功能,EasyAdmin 的 Github 也和这个网站一样。

感谢您的帮助

【问题讨论】:

    标签: php symfony4 impersonation easyadmin


    【解决方案1】:

    解决了!

    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)
                ;
        }
    

    【讨论】:

    • 出于好奇:你为什么要在其中放一个?
    • 我忘了Symfony会自动添加?如果路由中不需要参数,我想手动构建url但它很愚蠢^^'
    • 自 EasyAdminBundle v3.2.0 起不再工作。
    【解决方案2】:

    对于 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;
    }
    

    【讨论】:

      【解决方案3】:

      我不喜欢使用硬编码规则,所以将 UrlGenerator 注入到我的 CrudController:

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

      【讨论】:

      • 我认为 EasyAdminBundle v3.2.0 及更高版本的最佳答案。
      猜你喜欢
      • 2011-12-29
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 2019-11-18
      • 2011-04-20
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多