代码之家  ›  专栏  ›  技术社区  ›  Vincent Decaux

Symfony 4-登录后重定向用户未激活

  •  0
  • Vincent Decaux  · 技术社区  · 7 年前

    我使用symfony4.1,如果用户不活跃,我想将其重定向到登录页面(mydb中的字段)。

    class UserChecker implements UserCheckerInterface
    {
        /**
         * @var RouterInterface
         */
        private $router;
    
        /**
         * UserChecker constructor.
         * @param RouterInterface $router
         */
        public function __construct(RouterInterface $router)
        {
            $this->router = $router;
        }
    
        /**
         * @param UserInterface $user
         * @return bool|void
         */
        public function checkPreAuth(UserInterface $user)
        {
            if (! $user instanceof User) {
                return;
            }
    
            // user is not active
            if ($user->getActive() == 0) {
                return new RedirectResponse($this->router->generate('login'));
                //throw new AccessDeniedHttpException("Account not active");
            }
        }
        ....
    

    但是,我的用户没有被重定向,异常工作,但我只想用自定义消息重定向他(我将使用Flash会话)。

    我该怎么做?Symfony文档只提供异常消息,但对我来说不够灵活。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ricard Espinàs Llovet    7 年前

        if ($user->isAccountLocked($maxMinutesLocked)) {
            $ex = new LockedException('User account is locked.');
            $ex->setUser($user);
            throw $ex;
        }
    
        if (!$user->isEnabled()) {
            $ex = new DisabledException();
            $ex->setUser($user);
            throw $ex;
        }
    

    所以,我猜你的 $user->getActive() == 0 会和我的一样 !$user->isEnabled()

    我想你只需要把 DisabledException 您将再次进入登录页面。