2013-01-14 86 views
0

我有一个使用Silex的项目,而且我真的是这个symfony框架的全新人物。 Silex带有安全插件,我相信它与symfony库相同。 因此,然后问题是我需要使用一些路由参数在login_path防火墙配置上使用,例如下面如何在login_path/login_check上使用Symfony安全路由参数

我设置我的路线是这样的,cname将路由变量,我试图替换{ CNAME}从secured_area模式CNAME变量,

$app->match('/{cname}/dashboard/users','Dashboard\Controller\DashboardController::userAction')->method('GET|POST')->bind('dashboard_users'); 

的想法是与路径变量替换 “{} CNAME” ..

$app->register(new Silex\Provider\SecurityServiceProvider()); 

$app['security.firewalls'] = array(
           'secured_area' => array(
            'pattern' => '^/(\w+)/dashboard/', 
            'form' => array('login_path' => '/{cname}/login', 
                'check_path' => '/{cname}/dashboard/login_check', 
                'default_target_path' => '/{cname}/dashboard/home' 
                ), 
            'users' => $app->share(function() use ($app) { 
                return new Dashboard\Service\UserProvider($app['db']); 
               }), 

            'logout' => array('logout_path' => '/{cname}/dashboard/logout', 
                 'target' => '/'), 

           ), 
          ); 

我已经试过了,并且它不工作,然后我在4个核心文件中放了一些代码补丁AbstractAuthenticationListener,FormAuthenticationEntryPoint,DefaultAuthenticationSuccessHandler,LogoutListener 所以我把代码中的几行只是为了替换“{CNAME}”中的一些方法,这些类,

on AbstractAuthenticationListener class, 
    protected function requiresAuthentication(Request $request) 
     { 
      /* PATCH */ 
      if($route_params = $request->attributes->get("_route_params")){ 
       foreach($route_params as $key => $val){ 
        $this->options['check_path'] = str_replace("{".$key."}", $val, $this->options['check_path']); 
       } 
      } 
      /**/ 
      return $this->httpUtils->checkRequestPath($request, $this->options['check_path']); 
     } 

    On FormAuthenticationEntryPoint Class 
    public function start(Request $request, AuthenticationException $authException = null) 
     { 
      /* PATCH */ 
      if($route_params = $request->attributes->get("_route_params")){ 
       foreach($route_params as $key => $val){ 
        $this->loginPath = str_replace("{".$key."}", $val, $this->loginPath); 
       } 
      } 
      /**/ 


      if ($this->useForward) { 
       $subRequest = $this->httpUtils->createRequest($request, $this->loginPath); 

       return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST); 
      } 

      return $this->httpUtils->createRedirectResponse($request, $this->loginPath); 
     } 

    On DefaultAuthenticationSuccessHandler 
    protected function determineTargetUrl(Request $request) 
     { 
      /* PATCH */ 
      if($route_params = $request->attributes->get("_route_params")){ 
       foreach($route_params as $key => $val){ 
        $this->options['default_target_path'] = str_replace("{".$key."}", $val, $this->options['default_target_path']); 
        $this->options['login_path'] = str_replace("{".$key."}", $val, $this->options['login_path']); 
       } 
      } 
      /**/ 

      if ($this->options['always_use_default_target_path']) { 
       return $this->options['default_target_path']; 
      } 

      if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) { 
       return $targetUrl; 
      } 

      if (null !== $this->providerKey && $targetUrl = $request->getSession()->get('_security.'.$this->providerKey.'.target_path')) { 
       $request->getSession()->remove('_security.'.$this->providerKey.'.target_path'); 

       return $targetUrl; 
      } 

      if ($this->options['use_referer'] && ($targetUrl = $request->headers->get('Referer')) && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) { 
       return $targetUrl; 
      } 

      return $this->options['default_target_path']; 
     } 

on LogoutListener 
protected function requiresLogout(Request $request) 
    { 
     /* PATCH */ 
     if($route_params = $request->attributes->get("_route_params")){ 
      foreach($route_params as $key => $val){ 
       $this->options['logout_path'] = str_replace("{".$key."}", $val, $this->options['logout_path']); 
      } 
     } 
     /**/ 

     return $this->httpUtils->checkRequestPath($request, $this->options['logout_path']); 
    } 

我知道这是不是这样做的正确方法。 如果有任何正确的方法来做到这一点,我真的很期待它, thx。

回答

0

如果修改这样的代码有效,您可能只想扩展修改后的类。 Silex\Application是一个服务容器\Pimple的实例,它允许您即时重新定义或扩展现有服务。我自己并没有尝试过这种方法,但是您可以很好地替换防火墙(以及其他已更改的类)的入口点。