如何在DB中存储用户上次访问权限

时间:2013-09-21 11:26:59

标签: symfony doctrine-orm

我正在使用Symfony2编写Web应用程序,我需要注册登录该应用程序的每个用户的上次访问权限。我的实体有一个last_access属性,类型为datetime。

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

我在考虑这样的事情:

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

更新

我使用事件监听器解决了这个问题。

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 }

感谢大家!

4 个答案:

答案 0 :(得分:1)

您可以使用服务,在登录时调用“security.interactive_login”事件。

因此,如果您使用YML进行服务:

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();
            }
        }
    }
}

答案 1 :(得分:1)

以防万一有人想要更新版本的symfony,以下是代码。有些事情发生了变化,即没有更多请求,现在你使用RequestStack。

services.yml文件

 app.security.interactive_login_listener:
      class: AppBundle\Security\InteractiveLoginListener
      arguments: ["@doctrine.orm.entity_manager", "@request_stack" ]
      scope: request
      tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin }

和班级(注意我使用AppBundle不是我自己的自定义捆绑包。     

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

use Doctrine\ORM\EntityManager;
use AppBundle\Entity\User;

class InteractiveLoginListener {

    protected $em;
    protected $request;

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

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

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {

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

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

答案 2 :(得分:0)

通常,您可以在Cookie或会话中保存当前用户信息,例如Id,Username..etc。 只需将其调出并对数据库进行查询,并将当前日期时间添加到数据库中的列中。

E.g

Update Last_session = 'Current datetime'
Where UserID = 'UserID gotten form cookie/session'
From db.user;

答案 3 :(得分:0)

在symfony2中,您可以使用doctrine扩展名。它提供了Translatable,Sluggable,Tree-NestedSet,Timestampable,Loggable,Sortable等功能。

https://github.com/l3pp4rd/DoctrineExtensions

我希望这会对你有所帮助