2013-04-15 102 views
1

我有一個擴展fos用戶包和其他包的包。
我希望用戶通過身份驗證後,根據他的角色管理員或簡單用戶將他重定向到不同的視圖。
我的問題是,我無法找到從哪裏進行重定向的登錄控制器。登錄後重定向fos用戶包symfony

該角色是來自數據庫的User實體的屬性。

回答

10

您已經添加LoginSuccessHandler其實現也必須註冊爲服務AuthenticationSuccessHandler Interface,

然後,您可以onAuthenticationSuccess()方法如下內設置您的重定向邏輯,

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')); 
     } 
     // ... 
    } 
} 

你處理,

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] 

然後,您已經將以下行添加到您的防火牆安全配置

success_handler: security.authentication.customized_success_handler 
+0

我應該插入LoginSuccessHandler在擴展的FOS用戶捆綁或另一種捆綁? @Ahmed Siouani – user2269869

+0

我如何影響用戶的角色? @Ahmed Siouani – user2269869

+0

因爲它被定義爲一項服務,所以你可以把它放在任何地方。要從數據庫加載用戶及其組(角色),請深入瞭解[如何從數據庫(實體提供程序)部分加載安全用戶](http://symfony.com/doc/current/cookbook/security/ entity_provider.html)的文檔。 –

0

您可以覆蓋renderLogin功能如下:

class SecurityController extends BaseController 
{ 
/** 
* Renders the login template with the given parameters. Overwrite this function in 
* an extended controller to provide additional data for the login template. 
* 
* @param array $data 
* 
* @return \Symfony\Component\HttpFoundation\Response 
*/ 
protected function renderLogin(array $data) 
{ 
    $securityContext = $this->get('security.context'); 
    $router = $this->container->get('router'); 
//  if ($this->get('security.context')->getToken()->getUser() instanceof \FOS\UserBundle\Propel\User) { 
//   $url = $this->container->get('router')->generate('dashboard'); 
//   return new RedirectResponse($url); 
//  } 
    if ($securityContext->isGranted('ROLE_ADMIN')) { 
     return new RedirectResponse($router->generate('dashboard'), 307); 
    } 

    if ($securityContext->isGranted('ROLE_USER')) { 
     return new RedirectResponse($router->generate('front_page_home'), 307); 
    } 

    $requestAttributes = $this->container->get('request')->attributes; 
    if ($requestAttributes->get('_route') == 'admin_fos_user_security_login') { 
     $template = sprintf('FOSUserBundle:Security:login.html.twig'); 
     $data['admin'] = true; 
    } else { 
     $template = sprintf('FOSUserBundle:Security:login.html.twig'); 
     $data['admin'] = false; 
    } 

    return $this->container->get('templating')->renderResponse($template, $data); 
} 


}