登录fos用户束symfony后重定向 [英] redirect after login fos user bundle symfony

查看:75
本文介绍了登录fos用户束symfony后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展fos用户捆绑包和另一个捆绑包的捆绑包.
我希望用户通过身份验证后,根据其角色admin或简单用户将其重定向到不同的视图.
我的问题是我找不到重定向的登录控制器.

I have a bundle that extends the fos user bundle and an other bundle.
I want once a user is authenticated to redirect him depending on his role admin or simple user to different views.
My problem is that I can not find the controller of the login from where I will do the redirecting.

角色是来自数据库的User实体的属性.

The role is an attribute of the User entity which comes from the database.

推荐答案

您必须添加然后您可以在onAuthenticationSuccess()方法中按如下所示设置重定向逻辑,

You can then set your redirect logic within the onAuthenticationSuccess() method as follow,

namespace XXX\YourBundler\Handler;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{   
    protected $router;
    protected $security;

    public function __construct(Router $router, SecurityContext $security)
    {
        $this->router   = $router;
        $this->security = $security;
    }
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {   
        if ($this->security->isGranted('ROLE_XXXX_1'))
        {
            $response = new RedirectResponse($this->router->generate('route_1'));           
        }
        elseif ($this->security->isGranted('ROLE_XXXX_2'))
        {
            $response = new RedirectResponse($this->router->generate('route_2'));
        }
        // ...
    } 
}

您的处理程序还必须注册为服务

You handler must also be registered as a service,

parameters:
     security.authentication.success_handler.class: XXX\YourBundler\Handler\AuthenticationSuccessHandler

services:
    security.authentication.customized_success_handler:
        class: %security.authentication.success_handler.class%
        public: false
        arguments:  [@router, @security.context]

然后,将以下行添加到防火墙安全配置中,

You've then to add the following line to your firewall security configuration,

  success_handler: security.authentication.customized_success_handler

这篇关于登录fos用户束symfony后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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