Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add interactive_login listener to update User.lastLogin

  • Loading branch information...
commit 99387cf85eaffc8de39071ae9786d92413b8c3ed 1 parent ae49d5c
Thibault Duplessis ornicar authored
3  DependencyInjection/FOSUserExtension.php
View
@@ -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));
}
4 FOSUserBundle.php
View
@@ -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'));
+ }
}
17 Resources/config/listener.xml
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<container xmlns="http://www.symfony-project.org/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
+
+ <parameters>
+ <parameter key="fos_user.security.interactive_login_listener.class">FOS\UserBundle\Security\InteractiveLoginListener</parameter>
+ </parameters>
+
+ <services>
+ <service id="fos_user.security.interactive_login_listener" class="%fos_user.security.interactive_login_listener.class%">
+ <argument type="service" id="fos_user.user_manager" />
+ </service>
+ </services>
+
+</container>
39 Security/InteractiveLoginListener.php
View
@@ -0,0 +1,39 @@
+<?php
+
+namespace FOS\UserBundle\Security;
+
+use FOS\UserBundle\Model\UserManagerInterface;
+use FOS\UserBundle\Model\User;
+use Symfony\Component\Security\Http\Firewall\ListenerInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\Event;
+use DateTime;
+
+class InteractiveLoginListener implements ListenerInterface
+{
+ protected $userManager;
+
+ public function __construct(UserManagerInterface $userManager)
+ {
+ $this->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);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.