2014-06-23 2 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가 가리키는 서비스은 사용자가 리소스에 액세스 할 수 unsufficient 권한이있는 경우라고합니다. 사용자가 모두 인증되지 않은 경우 access_dened_handler는 호출되지 않습니다. 실제로 그래서이와 결국

문제를 해결 않았다 security.yml에 entry_point하는 서비스를 제공하는 것은 :

#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 
다음

내가 바로 logout: ~ 줄 끝에서, 내 security.yml이 서비스를 추가 (초기 참조 질문) :

:

entry_point: art_cube_shop.security.entry_point_handler 

그리고 서비스를 생성 0

// 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는 사용자가 리소스에 액세스 할 수있는 권한이 충분하지 않거나 전혀 인증되지 않은 경우에만 호출됩니다. 하지만 답은 해결책으로 이어집니다. 감사합니다! – mpbzh

관련 문제