0

私は Silex を使用するプロジェクトを持っています。私はこの symfony フレームワークを使用するまったく新しい人です。Silex にはセキュリティ プラグインが付属しており、symfony ライブラリと同じだと思います。したがって、問題は、以下の例のように、login_path ファイアウォール構成で使用されるルート パラメータを使用する必要があることです。

このようにルートを設定しました.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にいくつかのコードパッチを入れました。クラス、

   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.

4

1 に答える 1

0

このようなコードの変更が機能する場合は、変更したクラスを拡張したいだけかもしれません。は、既存のサービスをオンザフライで再定義または拡張できるサービス コンテナSilex\Applicationのインスタンスです。\Pimple私はこれを自分で試したことはありませんが、ファイアウォール (および変更した他のクラス) のエントリ ポイントを置き換えることができる可能性は十分にあります。

于 2013-01-16T12:47:15.123 に答える