redirect to a different homepage following user role using FOSUserBundle

倖福魔咒の 提交于 2019-12-12 03:27:15

问题


In my Symfony application I am using the FOSUserBundle.

It's an application involved two main Roles of User: Factory as ROLE_FACTORY and Custommer as ROLE_CUSTOMER.

I have a main page for non-connected user, it's the home page of the application.

But when User is going to be connected or is connected, following the role he has, the home page has to change.

So the fos user login action have to redirect to the right page.

ROLE_FACTORY have to be redirect to the factory_homepage route. ROLE_CUSTOMER have to be redirect to the customer_homepage route.

How can I create this behavior in the best practice using Symfony and FOSUSerBundle.


回答1:


First off there is no best practice for this. So I leave that decision of selecting one of these options to you whichever suits you according to your needs.

OPTION 1:

You have to implement an EventListener in this case.

STEP 1)

Register a service
<service id="acme_demo.listener.login" class="Acme\DemoBundle\EventListener\LoginListener" scope="request">
    <tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin"/>
    <argument type="service" id="router"/>
    <argument type="service" id="security.context"/>
    <argument type="service" id="event_dispatcher"/>
</service>

STEP 2)

Your EventListener itself
class LoginListener
{
    protected $router;
    protected $security;
    protected $dispatcher;

    public function __construct(Router $router, SecurityContext $security, EventDispatcher $dispatcher)
    {
        $this->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_FACTORY')) 
        {
            $response = new RedirectResponse($this->router->generate('factory_homepage'));
        } 
        elseif ($this->security->isGranted('ROLE_CUSTOMER')) 
        {
            $response = new RedirectResponse($this->router->generate('customer_homepage'));
        } 
        else 
        {
            $response = new RedirectResponse($this->router->generate('default_homepage'));
        }

        $event->setResponse($response);
    }
}

OPTION 2:

You can modify the vendor code at FOSUserBundle\Controller\SecurityController Add the following piece of code to make your loginAction look like this

public function loginAction(Request $request)
{
    $securityContext = $this->container->get('security.context');
    $router = $this->container->get('router');

    if ($securityContext->isGranted('ROLE_FACTORY')) 
    {
        return new RedirectResponse($router->generate('factory_homepage'));
    }
    elseif ($securityContext->isGranted('ROLE_CUSTOMER')) 
    {
        return new RedirectResponse($router->generate('customer_homepage'));
    }
    else
    {
        return new RedirectResponse($router->generate('default_homepage'));
    }
}


来源:https://stackoverflow.com/questions/34682205/redirect-to-a-different-homepage-following-user-role-using-fosuserbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!