Symfony2 / FOSUserBundle - Redirect after login according to the role

半城伤御伤魂 提交于 2019-12-10 14:24:23

问题


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

FYI : I use symfony 2.8

I create this class :

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

I Create this service :

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

I modified the firewall

    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

And I got this error :

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 ?

Thanks.


回答1:


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

service definition:

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

Changes I have made :

  • Use Router instead of RouterInterface
  • Inject @security.authorization_checker to the redirect.after.login service


来源:https://stackoverflow.com/questions/36676041/symfony2-fosuserbundle-redirect-after-login-according-to-the-role

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