Symfony2 Login and Security

Is there a way that I can save when there was the last time a user was logged in?

I am using symfony2 and everything works fine with the security configuration.

I saw this security and login in a project based on Symfony 2 , which is a similar question, but it just does not fit my needs.

Is there any other solution?

+5
source share
2 answers

You can create AuthenticationHandlerone that Symfony will call when the user logs in successfully, you can save the logon time of the object property User(suppose you have this script).

First, create a successful authentication handler:

namespace Acme\TestBundle\Handler;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;

class AuthenticationHandler extends ContainerAware implements AuthenticationSuccessHandlerInterface
{
    function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        $token->getUser()->setLoginTime(new \DateTime());
        $this->container->get('doctrine')->getEntityManager()->flush();

        return new RedirectResponse($this->container->get('router')->generate('login_success'));
    }
}

, , src/Acme/TestBundle/resources/Config/services.yml

services:
    authentication_handler:
        class: Acme\TestBundle\Handler\AuthenticationHandler
        calls:
            - [ setContainer, [ @service_container ] ] 

, security.yml

form_login:
    success_handler: authentication_handler

, User loginTime . User DaoAuthenticationProvider, : http://symfony.com/doc/current/book/security.html#loading-users-from-the-database.

+15

FOSUserBundle , ( ) "last_login".

+5

All Articles