6

Symfony2 (FOSUserBundle を使用しない) で独自のユーザー管理システムを構築しており、ユーザーにパスワードの変更を強制できるようにしたいと考えています。

イベントをリッスンするように EventListener をセットアップしてからkernal.request、リスナーでロジックを実行して、ユーザーがパスワードを変更する必要があるかどうかを判断します。その場合、「パスワードの変更」ルートにリダイレクトされます。

config.ymlでリッスンするサービスを my に追加しますkernal.request

password_change_listener:
    class: Acme\AdminBundle\EventListener\PasswordChangeListener
        arguments: [ @service_container ]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onMustChangepasswordEvent }

そしてリスナー:

public function onMustChangepasswordEvent(GetResponseEvent $event) {

  $securityContext = $this->container->get('security.context');

  // if not logged in, no need to change password
  if ( !$securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') )
    return;

  // If already on the change_password page, no need to change password
  $changePasswordRoute = 'change_password';
  $_route  = $event->getRequest()->get('_route');
  if ($changePasswordRoute == $_route)
    return;

  // Check the user object to see if user needs to change password
  $user = $this->getUser();
  if (!$user->getMustChangePassword())
    return;

  // If still here, redirect to the change password page
  $url = $this->container->get('router')->generate($changePasswordRoute);
  $response = new RedirectResponse($url);
  $event->setResponse($response);
}

私が抱えている問題は、開発モードでは、リスナーがプロファイラー バーとアセット リクエスト イベントもリダイレクトしていることです。アセットをダンプしてキャッシュをクリアし、サイトを本番モードで表示すると機能します。

アセット/プロファイラー バー/その他の内部コントローラーからのイベントを無視する方法はありますか? または、(ログイン成功時だけでなく) ユーザーを change_password ページにリダイレクトするより良い方法はありますか?

ワイルドなハック ソリューションを考えるのに夢中になっていますが、Symfony2 でこれをエレガントに処理する方法は確かにありますか?

4

1 に答える 1

1

これは、私が現在使用しているハックソリューションです。

  1. dev環境内かどうかを判断する
  2. その場合、すべてのルートの配列を取得します
  3. 追加したルートのみが残るようにルート配列をフィルタリングします
  4. 現在のルートをルートの配列と比較する
  5. 一致が見つかった場合、これはイベントが組み込みのコントローラーではなく、追加したものである必要があることを意味するため、リダイレクトを実行します。

そして、これはそれを機能させる狂気です:

// determine if in dev environment
if (($this->container->getParameter('kernel.environment') == 'dev'))
{

  // Get array of all routes that are not built in 
  // (i.e You have added them yourself in a routing.yml file).
  // Then get the current route, and check if it exists in the array 
  $myAppName = 'Acme';
  $routes = $this->getAllNonInternalRoutes($myAppName);
  $currentRoute = $event->getRequest()->get('_route');
  if(!in_array($currentRoute, $routes))
    return;
}

// If still here, success, you have ignored the assetic and 
// web profiler actions, and any other actions that you did not add
// yourself in a routing.yml file! Go ahead and redirect!
$url = $this->container->get('router')->generate('change_password_route');
$response = new RedirectResponse($url);
$event->setResponse($response);

そして、getAllNonInternalRoutes()それを機能させるクレイジーなハック機能 (これは、Qoop によってここで見つけたコードの変更です:

private function getAllNonInternalRoutes($app_name) {

  $router = $this->container->get('router');
  $collection = $router->getRouteCollection();
  $allRoutes = $collection->all();

  $routes = array();

  foreach ($allRoutes as $route => $params)
  {
    $defaults = $params->getDefaults();

    if (isset($defaults['_controller']))
    {
      $controllerAction = explode(':', $defaults['_controller']);
      $controller = $controllerAction[0];

      if ((strpos($controller, $app_name) === 0)) 
        $routes[]= $route;
    }
  }
  return $routes;
}
于 2013-10-17T14:27:50.643 に答える