FOSUserBundle admin area not accessible after login

后端 未结 2 2011
清歌不尽
清歌不尽 2021-01-07 17:05

I am using FOSUserBundle for admin section as well as frontend by following the instructions given at:

https://github.com/FriendsOfSymfony/FOSUserBundle/issu         


        
2条回答
  •  [愿得一人]
    2021-01-07 18:01

    @neeraj, as an answer to your comment here FOSUserBundle admin area not accessible after login as i know it's not possible to do it only with security.yml, but you can go with listener, not much to do.

    create folder EventListener in your Bundle, then create SecurityListener.php

    router = $router;
            $this->security = $security;
            $this->dispatcher = $dispatcher;
        }
    
        public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
        {
            $this->dispatcher->addListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse'));
        }
    
        public function onKernelResponse(FilterResponseEvent $event)
        {
            if ($this->security->isGranted('ROLE_ADMIN')) {
                $response = new RedirectResponse($this->router->generate('YOURCoreBundle_adminpage'));
            } elseif ($this->security->isGranted('ROLE_USER')) {
                $response = new RedirectResponse($this->router->generate('YOURBundle_userpage'));
            } else {
                $response = new RedirectResponse($this->router->generate('YOURCoreBundle_homepage'));
            }
    
            $event->setResponse($response);
        }
    }
    

    and in services.xml add

    
        Your\NameBundle\EventListener\SecurityListener
    
    
    
        
            
            
            
            
        
    
    

提交回复
热议问题