2016-04-17 37 views
5

Chciałbym dostosować przekierowanie po zalogowaniu, zgodnie z rolą użytkownika.Symfony2/FOSUserBundle - Przekierowanie po zalogowaniu zgodnie z rolą

FYI: Używam symfony 2.8

tworzę tę klasę:

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

tworzę tę usługę:

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

I zmodyfikowane zaporę

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 

I dostałem ten błąd:

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

Co mi brakowało? Jakieś wskazówki lub porady?

Dzięki. Definicja usługi

+0

Nigdy nie należy wprowadzać argumentu $ security konstruktora klasy. Skąd chcesz? – chalasr

+0

Tak, widziałem to po. Wstrzyknąłem więc $ security i miałem ten sam błąd. W każdym razie to działa dzięki rozwiązaniu Vamsi :) – Letsrocks

Odpowiedz

3
<?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'] 

Zmiany zrobiłem:

  • Używaj Router zamiast RouterInterface
  • wstrzyknąć @security.authorization_checker do redirect.after.login usługi
+0

Dzięki. To działa dobrze :) – Letsrocks

+0

Interfejsy powinny być używane jak to tylko możliwe, aby wpisać podpowiedź do usługi. W tym przypadku Router i RouterInterface nie mają znaczenia, ale byłoby bardziej elastyczne korzystanie z interfejsu w przypadku niestandardowego routera. W przeciwnym razie rzeczą, której nie udzielił właściciel pytania, był "security.authorization_checker", więc działałoby dobrze z twoją odpowiedzią. – chalasr