Symfony2/FOSUserBundle-根据角色登录后重定向 [英] Symfony2 / FOSUserBundle - Redirect after login according to the role

查看:86
本文介绍了Symfony2/FOSUserBundle-根据角色登录后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户角色自定义登录后的重定向.

I would like to customize the redirection after login according to the user's role.

仅供参考:我使用symfony 2.8

我创建了此类:

<?php

namespace Users\UsersBundle\Redirection;

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

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

  /**
   * AfterLoginRedirection constructor.
   * @param Router $router
   * @param Security $security
  */
   public function __construct(Router $router, Security $security)
  {
    $this->router = $router;
    $this->security = $security;
  }

    /**
     * @param Request $request
     * @param TokenInterface $token
     * @return RedirectResponse
     */
     public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  {
    if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
        $response = new RedirectResponse($this->router->generate('_homepage_admin'));
    } else {
        $referer_url = $request->headers->get('referer');

        $response = new RedirectResponse($referer_url);
    }
    return $response;
 }
}

我创建了此服务:

services:
redirect.after.login:
    class: Users\UsersBundle\Redirection\AfterLoginRedirection
    arguments: [@router]

我修改了防火墙

    firewalls:
    main:
        pattern: ^/
        form_login:
            login_path: fos_user_security_login
            check_path: fos_user_security_check
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            success_handler: redirect.after.login
        logout:
            path:   /users/logout
            target: /
        anonymous:    true

我得到了这个错误:

可捕获的致命错误:参数1传递给 Users \ UsersBundle \ Redirection \ AfterLoginRedirection :: __ construct() 必须是Users \ UsersBundle \ Redirection \ Router的实例,该实例 给定的Symfony \ Bundle \ FrameworkBundle \ Routing \ Router的名称,在 C:\ wamp \ www \ eCommerce \ app \ cache \ dev \ appDevDebugProjectContainer.php在 第2060行并已定义

Catchable Fatal Error: Argument 1 passed to Users\UsersBundle\Redirection\AfterLoginRedirection::__construct() must be an instance of Users\UsersBundle\Redirection\Router, instance of Symfony\Bundle\FrameworkBundle\Routing\Router given, called in C:\wamp\www\eCommerce\app\cache\dev\appDevDebugProjectContainer.php on line 2060 and defined

我错过了什么?有任何线索或建议吗?

What does i missed ? Any clues or advices ?

谢谢.

推荐答案

<?php

namespace Users\UsersBundle\Redirection;

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

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

    /**
     * AfterLoginRedirection constructor.
     * @param Router $router
     * @param AuthorizationChecker $security
     */
    public function __construct(Router $router, AuthorizationChecker $security)
    {
        $this->router = $router;
        $this->security = $security;
    }


    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
            $response = new RedirectResponse($this->router->generate('_homepage_admin'));
        } else {
            $referer_url = $request->headers->get('referer');

            $response = new RedirectResponse($referer_url);
        }
        return $response;
    }
}

服务定义:

redirect.after.login:
        class: Users\UsersBundle\Redirection\AfterLoginRedirection
        arguments: ['@router','@security.authorization_checker']

我所做的更改:

  • 使用Router代替RouterInterface
  • @security.authorization_checker注入到 redirect.after.login服务
  • Use Router instead of RouterInterface
  • Inject @security.authorization_checker to the redirect.after.login service

这篇关于Symfony2/FOSUserBundle-根据角色登录后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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