2016-04-17 18 views
5

Ich möchte die Umleitung nach der Anmeldung nach der Rolle des Benutzers anpassen.Symfony2/FOSUserBundle - Redirect nach Login nach der Rolle

FYI: Ich benutze symfony 2.8

Ich schaffe diese Klasse:

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

ich diesen Dienst erstellen:

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

ich die 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 
modifizierte

Und ich habe diesen Fehler:

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

Was habe ich verpasst? Irgendwelche Hinweise oder Ratschläge?

Danke.

+0

Sie injizieren nie das $ -Sicherheitsargument des Klassenkonstruktors. Von wo willst du es kommen? – chalasr

+0

Ja, ich sah das nach. Also spritzte ich die $ Sicherheit und ich hatte den gleichen Fehler .. Wie auch immer, es ist Arbeit dank Vamsi Lösung :) – Letsrocks

Antwort

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

Service-Definition:

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

Änderungen ich gemacht habe:

  • Verwenden Router statt RouterInterface
  • Inject @security.authorization_checker zum redirect.after.login Service
+0

Danke. Es funktioniert gut :) – Letsrocks

+0

Schnittstellen sollten verwendet werden, um die Argumente eines Dienstes einzutippen. Hier macht Router und RouterInterface keinen Unterschied, aber es wäre flexibler, die Schnittstelle im Falle eines benutzerdefinierten Routers zu verwenden. Sonst ist das, was der Besitzer der Frage verpasst hat, der 'security.authorization_checker', also würde es mit Ihrer Antwort gut funktionieren. – chalasr