douban5644 2014-12-03 19:04
浏览 20

FOSUserBundle - 首次登录后强制更改密码

In a Symfony2 application using FOSUserBundle for user management, the user table has been filled through an import script from a csv file and the password generated from a combination of data.

I would like to force the user to change his password at the first login.

When the event FOSUserEvents::SECURITY_IMPLICIT_LOGIN occurs, redirect to the route fos_user_change_password if the field last_login is NULL.

My idea was rewriting the method onImplicitLogin(UserEvent $event) of the class AGI\UserBundle\EventListener\LastLoginListener like this but the class is not overwritten:

public function onImplicitLogin(UserEvent $event) {
    $user = $event->getUser ();

    if ($user->getLastLogin () === null) {
        $user->setLastLogin ( new \DateTime () );
        $this->userManager->updateUser ( $user );
        $response = new RedirectResponse ( $this->router->generate ( 'fos_user_change_password' ) );
        $this->session->getFlashBag ()->add ( 'notice', 'Please change your password' );
        $event->setResponse ( $response );
    }
}

I already have a bundle overwriting FOSUserBundle and it works for controllers, forms, etc but It looks like it is not the way to do it with eventListeners.

How can I force the user to change the password after the first login?

  • 写回答

1条回答 默认 最新

  • duanou9758 2014-12-03 20:47
    关注

    With the help of the precious hint from @sjagr about fos_user.security.implicit_login that drove me to fos_user.security.implicit_login and an external topic about doing stuff right after login, I got a working solution.

    AGI\UserBundle\Resources\config\services.yml

    login_listener:
        class: 'AGI\UserBundle\EventListener\LoginListener'
        arguments: ['@security.context', '@router', '@event_dispatcher']
        tags:
            - { name: 'kernel.event_listener', event: 'security.interactive_login', method: onSecurityInteractiveLogin }
    

    AGI\UserBundle\EventListener\LoginListener.php

    <?php
    
    namespace AGI\UserBundle\EventListener;
    
    use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Symfony\Component\Security\Core\SecurityContext;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Bundle\FrameworkBundle\Routing\Router;
    use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
    use Symfony\Component\EventDispatcher\EventDispatcherInterface;
    
    class LoginListener {
    
        private $securityContext;
        private $router;
        private $dispatcher;
    
        public function __construct(SecurityContext $securityContext, Router $router, EventDispatcherInterface $dispatcher) {
            $this->securityContext = $securityContext;
            $this->router = $router;
            $this->dispatcher = $dispatcher;
        }
        public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {
            if ($this->securityContext->isGranted ( 'IS_AUTHENTICATED_FULLY' )) {
                $user = $event->getAuthenticationToken ()->getUser ();
    
                if ($user->getLastLogin () === null) {
                    $this->dispatcher->addListener ( KernelEvents::RESPONSE, array (
                            $this,
                            'onKernelResponse' 
                    ) );
                }
            }
        }
        public function onKernelResponse(FilterResponseEvent $event) {
            $response = new RedirectResponse ( $this->router->generate ( 'fos_user_change_password' ) );
            $event->setResponse ( $response );
        }
    }
    

    Thank you

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题