我必须做几件事才能让它发挥作用。
上下文键: 如here 所述,我必须为两个防火墙提供相同的上下文。如果没有这个,管理员在尝试切换用户时会被带到登录页面。
在两个防火墙上配置:我必须将基本的switch_user 配置密钥添加到两个防火墙:
switch_user:
role: ROLE_ADMIN
如果我只是将配置放在main_site 防火墙上,当退出 模拟并进入管理页面时,管理员会收到拒绝访问消息。 (例如,路由 /admin/dashboard?_switch_user=_exit 会给出 403)。
Provider key在main_site的配置上:
main_site:
switch_user:
role: ROLE_ADMIN
provider: fos_userbundle
没有这个,我收到错误“切换用户失败 - 找不到 user@example.com”。深入代码,原来是admin用户提供者被使用了,当然使用那个提供者时也找不到正常用户。
(provider 密钥为switch_user 配置讨论here。)
或者,我可以将其添加为防火墙本身的提供程序密钥:
main_site:
switch_user:
role: ROLE_ADMIN
provider: fos_userbundle
您会从我的问题的配置中看到 fos_userbundle 仅被指定为 form_login 的提供者,而不是作为一个整体的 main_site 的提供者,这就是为什么直到我添加它才使用它.在任一位置(模拟配置或整个防火墙)添加它都可以解决问题。
以下是完整的相关配置:
admin:
provider: admin
# Have to put basic switch_user config on both firewalls
switch_user:
role: ROLE_ADMIN
# Both the admin and main_site firewalls have the same context, to allow
# cross-firewall impersonation
# https://stackoverflow.com/a/17991481/328817
context: boardworks
main_site:
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
switch_user:
role: ROLE_ADMIN
# Have to explicitly set the provider, otherwise the site will use the admin
# user provider when looking up the users whom admins are trying to impersonate
provider: fos_userbundle
# Rather than adding the provider above, I could have added it here:
#provider: fos_userbundle