Php 重定向到user.activate如果$user->;isDisabled()与Silex

Php 重定向到user.activate如果$user->;isDisabled()与Silex,php,symfony,silex,Php,Symfony,Silex,如果用户未激活,我尝试将用户重定向到激活页面(user.activate)。我主要想在登录时进行检查,但另一个好处是即使用户已登录也可以进行检查 我尝试扩展Symfony\Component\Security\Core\User\UserChecker,如果它捕获到DisabledException但不处理响应,则返回重定向响应 我尝试在AuthenticationEvents::AUTHENTICATION\u FAILURE和AuthenticationEvents::AUTHENTICAT

如果用户未激活,我尝试将用户重定向到激活页面(user.activate)。我主要想在登录时进行检查,但另一个好处是即使用户已登录也可以进行检查

我尝试扩展
Symfony\Component\Security\Core\User\UserChecker
,如果它捕获到DisabledException但不处理响应,则返回重定向响应


我尝试在
AuthenticationEvents::AUTHENTICATION\u FAILURE
AuthenticationEvents::AUTHENTICATION\u SUCCESS
事件中检查
if($user->isDisabled())
,但这也不能处理响应。

与其做与安全性相关的事情,不如用另一种方式做,使用请求侦听器

使用
oncorrequest
函数创建一个新类,并在事件分派器中为
内核.request
事件注册它。它将获得一个
Symfony\Component\HttpKernel\Event\GetResponseEvent
实例作为单个参数

在那里,您可以检查是否有用户登录以及是否已禁用,然后使用事件的
setResponse
事件执行重定向。

我添加了这个类

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $user = $token->getUser();

        if (!$user->isEnabled())
        {
            // Logout
            $this->app['user.manager']->logout();

            $response = $this->app->redirect($this->app['url_generator']->generate('user.activate', array('id' => $user->id)));
        } 
        else
        {
            // redirect the user to where they were before the login process begun.
            $referer_url = $request->headers->get('referer');

            $response = new RedirectResponse($referer_url);
        }

        return $response;
    }
}
然后加上这个,

$app['security.authentication.success_handler.secured_area'] = $app->share(function ($app) {
    return new \Kidtrips\Lib\LoginSuccessHandler($app);
});

我意识到让它对所有的请求都这样做并不是最好的选择。