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 でこれをエレガントに処理する方法は確かにありますか?