2014-06-23 44 views
2

我正在基於Symfony2開發一個簡單的商店(用於編譯商品)。Symfony2防火牆:重定向到註冊表單而不是登錄

將商品添加到購物車後,用戶可以繼續提供其商品的摘要,然後請求編譯的商品。

security: 
firewalls: 
    secured_area: 
     pattern: ^/ 
     anonymous: ~ 
     provider: default 
     form_login: 
      login_path: acme_security_login_route 
      check_path: acme_security_login_check_route 
      csrf_provider: form.csrf_provider 
     logout: ~ 

    default: 
     anonymous: ~ 

access_control: 
    - { path: ^/request-offer, roles: ROLE_CLIENT } 

providers: 
    default: 
     entity: { class: AcmeShopBundle:User } 

encoders: 
    Symfony\Component\Security\Core\User\User: plaintext 
    Acme\ShopBundle\Entity\User: 
     algorithm: bcrypt 
     cost:  15 

這意味着,如果客戶端登錄,他會直接去總結,如果沒有,他會被重定向到登錄頁面:

摘要頁由以下防火牆保護。

現在,由於客戶更有可能成爲新客戶,我希望重定向到註冊表單。

SecurityBundle Configuration Reference中描述的選項不允許這樣做。 當然,改變login_path也不是解決方案。

什麼是最好的解決方案?

回答

1

Nextar's answer將我引向解決方案。

報價this question

由access_denied_handler指出該服務,如果用戶有非充分的權限訪問該資源時纔會調用。如果用戶未完全通過身份驗證,則始終不會調用access_dened_handler。提供服務的security.yml到入口點沒有實際解決問題

所以我結束了這一點:

#services.yml 
acme.security.entry_point.class: ArtCube\ShopBundle\Service\EntryPointHandler 

services: 
    acme.security.entry_point_handler: 
     class: %acme.security.entry_point.class% 
     arguments: 
      router:  @router 

然後我說這個服務我security.yml,之後logout: ~線(見初始問題):

entry_point: art_cube_shop.security.entry_point_handler 

,並創建了服務:

// Acme/ShopBundle/Service/EntryPointHandler.php 

use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; 
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; 

class EntryPointHandler implements AuthenticationEntryPointInterface { 

    protected $router; 

    public function __construct(Router $router) 
    { 
     $this->router = $router; 
    } 

    public function start(Request $request, AuthenticationException $authException = null) 
    { 
     if($authException instanceof InsufficientAuthenticationException) 
     { 
      return new RedirectResponse($this->router->generate('acme_security_registration_route')); 
     } 
     else 
     { 
      return new RedirectResponse($this->router->generate('acme_security_login_route')); 
     } 
    } 
} 
1

在我看來,一個不錯的解決方案是添加一個自己的AccessDeniedExceptionHandler,如何做到這一點在這裏解釋。

Using Symfony2's AccessDeniedHandlerInterface

更進一步,你可以通過做配置組件配置的服務,讓您傳遞作爲參數來重定向路徑。

http://symfony.com/doc/current/components/config/definition.html

如果你這樣做,你可以改變的,如果你有更多的用戶,要回重定向沒有編輯任何類到登錄頁面。

+0

僅當用戶訪問資源的權限不足時纔會調用access_denied_handler,但如果他根本沒有進行身份驗證,則僅調用access_denied_handler。 但是你的回答讓我想到解決方案,謝謝! – mpbzh

相關問題