How to save login date

New topics about Symfony 2 should go here

Moderators: dcobalt, tiagojsag

How to save login date

Postby Pablo » Sat Apr 14, 2012 4:39 pm

Hello. I have a little problem. I have login form which work really nice. I want to save the last login date of the user. How can I do this thing. I was thinking that I can put this function in SecuredController in public function securityCheckAction() {} but it doesn`t work :( And for my suprise If I delete this Action I can login without any problem :(
I wrote this function
Code: Select all
      
        $em = $this->getDoctrine()->getEntityManager();
        $user = $this->get('security.context')->getToken()->getUser();
        $user->setUserLastLogin(new \Datetime('now'));
        $em->persist($user);
        $em->flush();

But I don`t know where to write it :( Any suggestion?
Pablo
Member
 
Posts: 69
Joined: Thu Oct 06, 2011 11:24 am

Re: How to save login date

Postby blogsh » Sun Apr 15, 2012 8:49 pm

The form_login authentication method fires an event if a user could be logged in successfully. You can set the handler for this event with the "success_handler" option in your security configuration: http://symfony.com/doc/current/referenc ... urity.html

The handler should implement AuthenticationSuccessHandlerInterface:
https://github.com/symfony/symfony/blob ... erface.php
blogsh
Faithful Member
 
Posts: 501
Joined: Thu Mar 03, 2011 9:35 pm
Location: Germany

Re: How to save login date

Postby trsteel88 » Mon Apr 16, 2012 11:37 am

Create an interactive login listener.

See FOSUserBundle do it here:

Class: https://github.com/FriendsOfSymfony/FOS ... stener.php

Service: https://github.com/FriendsOfSymfony/FOS ... curity.xml

You will need to inject the Entity Manager service into the class if you do not have a user manager.
trsteel88
Member
 
Posts: 64
Joined: Tue Jun 21, 2011 11:18 am


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: No registered users and 9 guests

cron