Symfony,检查用户是否接受了新的T& C.

时间:2018-05-24 08:44:05

标签: symfony symfony4

在执行任何控制器之前,我需要检查用户是否已接受最新的隐私策略。像这样:

if($user->getAcceptedTnc() < 2) // unless I'm in some specific routes...
{
    return $this->render('app/privacyPolicy.html.twig');

    // or alternatively do AppController::privacyPolicyAction()
}

这可以在哪里完成? 我已经考虑过将所有用户注销并将其放入一些身份验证监听器......

1 个答案:

答案 0 :(得分:0)

我用onKernelController监听器解决了,所以我在每次加载页面时都进行了检查:

public function onKernelController(FilterControllerEvent $event)
{
    // [ return on some conditions (ajax calls, specific controller/routes, ...) ]

    /** @var User $user */
    $user = $this->token ? $this->token->getUser() : null;

    if($user && $user !== 'anon.' && $user->getLastAcceptedTerms() < Utils::CURRENT_TERMS_VERSION) {

        // [ return if the user is non-EU, has specific roles, etc...]

        // The user must accept the new Terms. Show him AppController:privacyPolicyAction
        $request = new Request();
        $request->attributes->set('_controller', 'App\Controller\AppController:privacyPolicyAction');
        $event->setController($this->controllerResolver->getController($request));
    }
}