2017-03-15 10 views
0

ユーザのログイン後にファイルにログインする方法を教えてください。Symfony - FOSUserBundle - ログイン後にファイルに書き込む

私はこれがあります。

<?php 
namespace Web\AdminBundle\EventListener; 

use FOS\UserBundle\Model\UserManagerInterface; 
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 

class LoginListener { 

    protected $userManager; 

    public function __construct(UserManagerInterface $userManager){ 
     $this->userManager = $userManager; 
    } 

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) 
    { 
     $user = $event->getAuthenticationToken()->getUser(); 
     $myfile = fopen("login.txt", "w"); 
     fwrite($myfile, 'some-text'); 
     fclose($myfile); 
    } 
} 

その後:

login_listener: 
    class: Web\AdminBundle\EventListener\LoginListener 
    arguments: 
     userManager: "@fos_user.user_manager" 
    tags: 
     - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin } 

しかし、これは動作しませんが、エラーはありません。私はちょうどそれが働いていることを確認するために死ぬ( 'some-text')と呼ばれるようにしました、ボットは何もしませんでした。

答えて

1

In Symfony 3.2別の方法でこれを行う必要があります。

//..src/YourBundle/EventListener/LoginListener.php 

namespace YourBundle\EventListener; 

use Symfony\Component\EventDispatcher\EventDispatcherInterface; 
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 
use Symfony\Component\HttpKernel\KernelEvents; 
use Symfony\Component\Routing\Router; 
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; 
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; 

class LoginListener 
{ 
/** @var Router */ 
protected $router; 

/** @var TokenStorage */ 
protected $token; 

/** @var EventDispatcherInterface */ 
protected $dispatcher; 

/** 
* @param Router $router 
* @param TokenStorage $token 
* @param EventDispatcherInterface $dispatcher 
*/ 
public function __construct(Router $router, TokenStorage $token, EventDispatcherInterface $dispatcher) 
{ 
    $this->router  = $router; 
    $this->token  = $token; 
    $this->dispatcher = $dispatcher; 
} 

/** 
* @param InteractiveLoginEvent $event 
*/ 
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) 
{ 
    $this->dispatcher->addListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']); 
} 

/** 
* @param FilterResponseEvent $event 
*/ 
public function onKernelResponse(FilterResponseEvent $event) 
{ 
    $user = $this->token->getToken()->getUser(); // here you have your user 


    //Let's write some thing to the file 
    $myfile = fopen("login.txt", "w"); 
    fwrite($myfile, 'some-text'); 
    fclose($myfile); 

    $event->getResponse()->headers->set('Location', $route); 
} 

`

そして、あなたはおかげで、私はsymfonyのを使用している今、あなたは

1

私は最近、同様の問題に直面しました。すべてのログイン時にデータベース数を増やす必要がありました。 (...それはあなたが転がる方法だ場合またはXML)

class LoginSubscriber implements EventSubscriberInterface 
{ 
    public function handleLogin(InteractiveLoginEvent $event) 
    { 
     // Inser logic here 
    } 

    /** 
    * @return array 
    */ 
    public static function getSubscribedEvents() 
    { 
     return [ 
      SecurityEvents::INTERACTIVE_LOGIN => 'handleLogin' 
     ]; 
    } 

} 

そして、あなたのservices.ymlでイベントをカーネルに耳を傾けサービスとして加入者を登録する:私が選んだルートは、このような加入者を登録することでした:

login.subscriber: 
    class: Your\Namespace\LoginSubscriber 
    tags: 
     - {name: kernel.event_subscriber } 

これはすべきことです。

ハッピーコーディング!

PS:

このアプローチは、最近のバージョン間で正常に動作する必要がありますが、私はちょうど私が使用した特定のバージョンを指摘してみましょう:

  • friendsofsymfony /ユーザー・バンドルv1.3.6
  • のsymfony/symfony v2.8.7
+0

onKernelResponse()方法に別のロジックを追加することができますservices.ymlに

app.login.listener: class: YourBundle\EventListener\LoginListener arguments: - "@router" - "@security.token_storage" - "@event_dispatcher" tags: - { name: kernel.event_listener, method: onSecurityInteractiveLogin, event: security.interactive_login} 

をこのイベントリスナーを登録する必要があります3.2。今は違うと思う...しかし、私は試してみる。 そしてfos 2.0 –

+0

それはあってはいけません。私たちの2つのアプローチの唯一の市長の違いは、リスナーとサブスクライバの使用です。 – Jan

+0

困った場合は、正しいものとしてマークしてください – Jan

関連する問題