From 99387cf85eaffc8de39071ae9786d92413b8c3ed Mon Sep 17 00:00:00 2001 From: ornicar Date: Fri, 25 Feb 2011 18:08:48 -0800 Subject: [PATCH] Add interactive_login listener to update User.lastLogin --- DependencyInjection/FOSUserExtension.php | 3 +- FOSUserBundle.php | 4 +++ Resources/config/listener.xml | 17 +++++++++++ Security/InteractiveLoginListener.php | 39 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Resources/config/listener.xml create mode 100644 Security/InteractiveLoginListener.php diff --git a/DependencyInjection/FOSUserExtension.php b/DependencyInjection/FOSUserExtension.php index 9b1875614b..f9339b1d44 100644 --- a/DependencyInjection/FOSUserExtension.php +++ b/DependencyInjection/FOSUserExtension.php @@ -24,8 +24,7 @@ public function load(array $configs, ContainerBuilder $container) } $loader->load(sprintf('%s.xml', $config['db_driver'])); - // load all service configuration files (the db_driver first) - foreach (array('controller', 'templating', 'twig', 'form', 'validator', 'security', 'util') as $basename) { + foreach (array('controller', 'templating', 'twig', 'form', 'validator', 'security', 'util', 'listener') as $basename) { $loader->load(sprintf('%s.xml', $basename)); } diff --git a/FOSUserBundle.php b/FOSUserBundle.php index 9e02296e7e..8742b1c2a6 100644 --- a/FOSUserBundle.php +++ b/FOSUserBundle.php @@ -14,4 +14,8 @@ class FOSUserBundle extends Bundle { + public function boot() + { + $this->container->get('fos_user.security.interactive_login_listener')->register($this->container->get('event_dispatcher')); + } } diff --git a/Resources/config/listener.xml b/Resources/config/listener.xml new file mode 100644 index 0000000000..2298542875 --- /dev/null +++ b/Resources/config/listener.xml @@ -0,0 +1,17 @@ + + + + + + FOS\UserBundle\Security\InteractiveLoginListener + + + + + + + diff --git a/Security/InteractiveLoginListener.php b/Security/InteractiveLoginListener.php new file mode 100644 index 0000000000..b73d9bda64 --- /dev/null +++ b/Security/InteractiveLoginListener.php @@ -0,0 +1,39 @@ +userManager = $userManager; + } + + public function register(EventDispatcherInterface $dispatcher) + { + $dispatcher->connect('security.interactive_login', array($this, 'listenToInteractiveLogin')); + } + + public function unregister(EventDispatcherInterface $dispatcher) + { + } + + public function listenToInteractiveLogin(Event $event) + { + $user = $event->get('token')->getUser(); + + if ($user instanceof User) { + $user->setLastLogin(new DateTime()); + $this->userManager->updateUser($user); + } + } +}