我有一个登录成功处理程序,它将一些用户重定向到一个特殊的表单。无论如何,AuthenticationSuccessHandlerInterface 需要返回一个响应并且除了一种情况外运行良好。如果用户首先填写了错误的凭据并再次被重定向到登录页面,则处理程序会在正确登录后将他再次重定向到登录页面。
如果我只是使用选项 use_referer: true 它工作正常。所以我可以把逻辑放到控制器而不是事件上,但也许你们中的某个人有我的解决方案。
谢谢你
防火墙
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
success_handler: applypie_userbundle.login.handler
#default_target_path: applypie_user_dashboard
use_referer: true
logout: true
anonymous: true
事件
namespace Applypie\Bundle\UserBundle\EventListener;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
class NewUserListener implements AuthenticationSuccessHandlerInterface
{
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
if(!$user->getApplicant() && !count($user->getCompanies())) {
return new RedirectResponse($this->router->generate('applypie_user_applicant_create'));
}
return new RedirectResponse($request->headers->get('referer'));
}
}
服务
applypie_userbundle.login.handler:
class: Applypie\Bundle\UserBundle\EventListener\NewUserListener
arguments: ["@router"]