使用FOSUserBundle跟随用户角色重定向到其他主页 [英] redirect to a different homepage following user role using FOSUserBundle

查看:83
本文介绍了使用FOSUserBundle跟随用户角色重定向到其他主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Symfony应用程序中,我正在使用FOSUserBundle.

In my Symfony application I am using the FOSUserBundle.

这是一个涉及用户两个主要角色的应用程序:工厂"为ROLE_FACTORY,客户"为ROLE_CUSTOMER.

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.

因此fos用户登录操作必须重定向到正确的页面.

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

ROLE_FACTORY必须重定向到factory_homepage路由. ROLE_CUSTOMER必须重定向到customer_homepage路由.

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

如何使用SymfonyFOSUSerBundle最佳实践中创建此行为.

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

推荐答案

首先,没有最佳实践.因此,我会保留根据您的需求选择其中一种选项的决定.

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.

在这种情况下,您必须实现EventListener.

You have to implement an EventListener in this case.

步骤1)

注册服务

<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)

您的EventListener本身

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);
    }
}

选项2:

您可以在以下位置修改供应商代码: FOSUserBundle \ Controller \ SecurityController 添加以下代码,使您的loginAction看起来像这样

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'));
    }
}

这篇关于使用FOSUserBundle跟随用户角色重定向到其他主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆