2017-03-02 5 views
0

私はFOSUserBundleを統合したSymfony 2.5プロジェクトを持っています。私は、自動的にログアウトされてサインインページにリダイレクトされるように、アプリのXアイドル時間のXをログインしたユーザーに欲しいです。symfony 2.5 - 一定期間使用しないと自動リダイレクト

これまでのところ、私はそれがここにHow to log users off automatically after a period of inactivity?

<?php 
namespace MyProject\UserBundle\EventListener; 

use Symfony\Component\Routing\RouterInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpFoundation\Session\SessionInterface; 
use Symfony\Component\Security\Core\SecurityContextInterface; 
use Symfony\Component\HttpKernel\HttpKernelInterface; 
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; 


class UserInactivityListener 
{ 

    protected $session; 
    protected $securityContext; 
    protected $router; 
    protected $maxIdleTime; 

    public function __construct(
     SessionInterface $session, 
     SecurityContextInterface $securityContext, 
     RouterInterface $router, 
     $maxIdleTime = 0) 
    { 
     $this->session = $session; 
     $this->securityContext = $securityContext; 
     $this->router = $router; 
     $this->maxIdleTime = $maxIdleTime; 
    } 

    /** 
    * user will be logged out after 30 minutes of inactivity 
    * 
    * @param FilterControllerEvent $event 
    * @return type 
    */ 
    public function onKernelController(FilterControllerEvent $event) 
    { 
     if (HttpKernelInterface::MASTER_REQUEST != $event- >getRequestType()) { 
      return; 
     } 

     if ($this->maxIdleTime > 0) { 
      try { 
       $this->session->start(); 
       $lapse = time() - $this->session->getMetadataBag()->getLastUsed(); 
       $isFullyAuthenticated = $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY'); 

       if (($lapse > $this->maxIdleTime) && $isFullyAuthenticated == true) { 
        $this->securityContext->setToken(null); 
        $url = $this->router->generate('fos_user_security_login'); 

        $event->setController(function() use ($url) { 
         return new RedirectResponse($url); 

        }); 
       } 
      } catch (\Exception $ex) { 
       return; 
      } 
     } 
    } 
} 

問題は、このイベントがトリガされ、ユーザーがしようとしたときにのみリダイレクトが起こるだろうということです上の提案と非常によく似て、ソリューションを実装しました彼のアイドルタイムの後にアプリ内に何かをロードする。私が望むのは、ユーザーが何もやりとりすることなく、アプリケーションがサインアップページに自動的にリダイレクトされることです。

リフレッシュメタタグHow to auto redirect a user in Symfony after a session time out?を使用するよう提案されましたが、リフレッシュイベントを毎時発生させる別のより良い方法があるかどうか疑問に思っていましたか?

答えて

1

定期的なページの再読み込みによってユーザーが迷惑を掛けてもメタリフレッシュは問題ありません。おそらく最も信頼できるでしょう。また、のようなJavaScriptの/ jQueryのスニペットを実装できるようにpingを繰り返し、サーバーその:この場合

setTimeout(checkIdleTime, 2000);

ごと2000ミリ秒または2秒ごと。

function checkIdleTime() { 
    $.get('/idle', function()) { 
     // If HTTP_OK response, do nothing, otherwise handle error 
    } 
} 

「アイドル」をチェックするアクションにURLをマップします。/idleその行動はもちろん、それ自体への要求を無視しなければならないだろう。アイドルでない場合は成功を返し、そうでない場合はユーザーを強制的にログアウトして何らかのエラーを返します。 jQueryレスポンスのエラーを処理します。おそらく単純なリダイレクトは次のようになります。FOSJsRoutingBundleをチェックして、jQueryからsymfonyのルートをうまく使用してURLをハードコードする必要がないようにしてください。FOSJsRoutingBundleをチェックしてください:FOSJsRoutingBundle

+1

ありがとう@nurikable。ソリューションが機能する。 –

関連する問題