Navigation Menu

Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Add interactive_login listener to update User.lastLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Mar 2, 2011
1 parent ae49d5c commit 99387cf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
3 changes: 1 addition & 2 deletions DependencyInjection/FOSUserExtension.php
Expand Up @@ -24,8 +24,7 @@ public function load(array $configs, ContainerBuilder $container)
} }
$loader->load(sprintf('%s.xml', $config['db_driver'])); $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', 'listener') as $basename) {
foreach (array('controller', 'templating', 'twig', 'form', 'validator', 'security', 'util') as $basename) {
$loader->load(sprintf('%s.xml', $basename)); $loader->load(sprintf('%s.xml', $basename));
} }


Expand Down
4 changes: 4 additions & 0 deletions FOSUserBundle.php
Expand Up @@ -14,4 +14,8 @@


class FOSUserBundle extends Bundle class FOSUserBundle extends Bundle
{ {
public function boot()
{
$this->container->get('fos_user.security.interactive_login_listener')->register($this->container->get('event_dispatcher'));
}
} }
17 changes: 17 additions & 0 deletions Resources/config/listener.xml
@@ -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 changes: 39 additions & 0 deletions Security/InteractiveLoginListener.php
@@ -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);
}
}
}

0 comments on commit 99387cf

Please sign in to comment.