如何在数据库中存储用户的最后访问权限 [英] How to store user last access in DB

查看:108
本文介绍了如何在数据库中存储用户的最后访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2编写Web应用程序,并且需要注册每个登录到该应用程序的用户的最后访问权限。我的实体具有 last_access 属性,键入datetime。

I'm writing a Web App using Symfony2 and I need to register the last access of every user who log in to the app. My entity has a last_access property, type datetime.

代码很简单,我只需要获取当前用户的实体并设置值,但是我不知道该把代码放在哪里。

The code is easy, I just need to get the entity of the current user and set the value, but I don't know where I have to put that code.

我在想这样的事情:

$manager = $this->getDoctrine()->getManager();
$user= $this->get('security.context')->getToken()->getUser();
$user->setLastAccess(new \DateTime('now'));
$manager->persist($user);
$manager->flush();



UPDATE



我使用事件监听器。

UPDATE

I solved this using an event listener.

class LoginListener {
/**
 * @var Doctrine\Bundle\DoctrineBundle\Registry
 */
private $doctrine;

public function __construct(Doctrine $doctrine){
    $this->doctrine = $doctrine;
}

public function onInteractiveLogin(InteractiveLoginEvent $event){
    $user = $event->getAuthenticationToken()->getUser();

    if ($user) {
        $user->setLastAccess(new \DateTime('now'));
        $this->doctrine->getEntityManager()->persist($user);
        $this->doctrine->getEntityManager()->flush();
    }
}

}

我还创建了一个服务:

services:
my.service:
    class: SGRH\AdminBundle\Listener\LoginListener
    arguments: [@doctrine]
    tags:
      - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }

谢谢大家!

推荐答案

您可以对服务进行操作,在登录时调用 security.interactive_login事件。

You can do it with services, at logon the event "security.interactive_login" is invoked.

因此,如果您将YML用于服务:

So if you use YML for services:

your_user_bundle.security.interactive_login_listener:
    class: ACME\YourBundle\Listener\InteractiveLoginListener
    arguments: [ "@doctrine.orm.entity_manager", "@request" ]
    scope: request
    tags:
      - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin } 

和类:

<?php

namespace ACME\YourBundle\Listener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

use Doctrine\ORM\EntityManager;
use ACME\YourBundle\Entity\User;

class InteractiveLoginListener {

    protected $em;
    protected $request;

    public function __construct(EntityManager $em, Request $request) {

        $this->em = $em;
        $this->request = $request;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {

        $user = $event->getAuthenticationToken()->getUser();

        if ($user instanceof User) {
            if($this->request->hasSession()) {
                $user->setLastAccess(new \DateTime('now'));
                $this->em->persist($user);
                $this->em->flush();
            }
        }
    }
}

这篇关于如何在数据库中存储用户的最后访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆