为了让它发挥作用,我必须做几件事。
上下文键:如上所述here https://stackoverflow.com/a/17991481/328817,我必须为两个防火墙提供相同的上下文。如果没有这个,管理员在尝试切换用户时会被带到登录页面。
两个防火墙上的配置:我必须添加基本的switch_user
配置键both防火墙:
switch_user:
role: ROLE_ADMIN
如果我只是将配置放在main_site
防火墙,管理员收到访问被拒绝消息时exiting模拟并进入管理页面。 (例如,路线/admin/dashboard?_switch_user=_exit
会给出 403)。
提供商密钥 on the main_site
的配置:
main_site:
switch_user:
role: ROLE_ADMIN
provider: fos_userbundle
如果没有这个,我会收到错误“切换用户失败 -[电子邮件受保护] /cdn-cgi/l/email-protection没有找到”。深入研究代码,结果发现admin
正在使用用户提供程序,当然在使用该提供程序时找不到普通用户。
(provider
关键为switch_user
配置讨论here https://stackoverflow.com/q/12916510/328817.)
或者,我可以将其添加为防火墙本身的提供程序密钥:
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