Symfony 如何返回所有登录的活跃用户
我想返回我的应用程序的所有登录用户,并将其呈现在我的仪表板中.user_id
和 user_name
应该从会话中检索(我使用外部 LDAP 库进行身份验证)
I want to return all Logged in users of my application and render it in my Dashboard. The user_id
and user_name
should be retrieved from the session (I am using an external LDAP Library for authentication)
我在数据库中创建了一个名为 lastActivity
的字段,该字段将包含上次登录时间,然后我可以在数据库中查询 lastActivity
显示上次登录的用户2分钟.
I have created a field in the database called lastActivity
which will contain the last login time and then I can query the database for lastActivity
display users logged in in the last 2 minutes.
ActivityListener.php
<?php
namespace BnppSecurityBundleEventListener;
use DoctrineORMEntityManager;
//use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
use SymfonyComponentHttpKernelHttpKernel;
use AcmeSecurityBundleEntityUser;
use DoctrineBundleDoctrineBundleRegistry;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $securityContext;
protected $entityManager;
public function __construct(SecurityContext $securityContext, EntityManager $entityManager)
{
$this->securityContext = $securityContext;
$this->entityManager = $entityManager;
}
/**
* Update the user "lastActivity" on each request
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
// Check that the current request is a "MASTER_REQUEST"
// Ignore any sub-request
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return;
}
// Check token authentication availability
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
if ( ($user instanceof User) && !($user->isActiveNow()) ) {
$user->setLastActivity(new DateTime('now'));
$this->entityManager->flush($user);
}
}
}
}
Services.yml
Services.yml
services:
activity_listener:
class: BnppSecurityBundleEventListenerActivityListener
arguments: [@security.context, @doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
用户实体
<?php
namespace AcmeSecurityBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
/**
* User
*
* @ORMTable(name="users")
* @ORMEntity(repositoryClass="AcmeSecurityBundleEntityUserRepository")
*/
class User implements UserInterface
{
/**
* @var DateTime
* @ORMColumn(name="LASTACTIVITY", type="datetime")
*/
private $lastActivity;
/**
* @return bool whether the user is active or not
*/
public function isActiveNow()
{
$delay = newDateTime('2 minutes ago');
return($this->getlastActivity()>$delay);
}
/**
* Set lastActivity
*
* @paramDatetime $lastActivity
* @return User
*/
public function setlastActivity($lastActivity)
{
$this->lastActivity = $lastActivity;
return $this;
}
/**
* Get lastActivity
*
* @return DateTime
*/
public function getlastActivity()
{
return $this->lastActivity;
}
}
推荐答案
这里有一个很棒的帖子:列出在线用户.
There is a great post here: List online users.
您可以创建一个侦听器来侦听 kernel.controller
事件并在每次用户处于活动状态时更新用户字段 lastActivity.您可以检查 lastActivity <now()- 2 分钟
并更新 lastActivity 时间戳.
You can create a Listener that listens on the kernel.controller
event and updates a user field lastActivity every time a user is active. You can check lastActivity < now()- 2 minutes
and update lastActivity timestamp.
另外:在 symfony 2 中实现用户活动
注意:如果您没有使用 FOSUserBundle,请参阅下面的编辑.
1 将此添加到您的用户实体
/**
* Date/Time of the last activity
*
* @var Datetime
* @ORMColumn(name="last_activity_at", type="datetime")
*/
protected $lastActivityAt;
/**
* @param Datetime $lastActivityAt
*/
public function setLastActivityAt($lastActivityAt)
{
$this->lastActivityAt = $lastActivityAt;
}
/**
* @return Datetime
*/
public function getLastActivityAt()
{
return $this->lastActivityAt;
}
/**
* @return Bool Whether the user is active or not
*/
public function isActiveNow()
{
// Delay during wich the user will be considered as still active
$delay = new DateTime('2 minutes ago');
return ( $this->getLastActivityAt() > $delay );
}
2 创建事件监听器
<?php
namespace AcmeUserBundleEventListener;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
use SymfonyComponentHttpKernelHttpKernel;
use FOSUserBundleModelUserManagerInterface;
use FOSUserBundleModelUserInterface;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $securityContext;
protected $userManager;
public function __construct(SecurityContext $securityContext, UserManagerInterface $userManager)
{
$this->securityContext = $securityContext;
$this->userManager = $userManager;
}
/**
* Update the user "lastActivity" on each request
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
// Check that the current request is a "MASTER_REQUEST"
// Ignore any sub-request
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return;
}
// Check token authentication availability
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
if ( ($user instanceof UserInterface) && !($user->isActiveNow()) ) {
$user->setLastActivityAt(new DateTime());
$this->userManager->updateUser($user);
}
}
}
}
3 将事件监听器声明为服务
parameters:
acme_user.activity_listener.class: AcmeUserBundleEventListenerActivityListener
services:
acme_user.activity_listener:
class: %acme_user.activity_listener.class%
arguments: [@security.context, @fos_user.user_manager]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
一切顺利!
1 将此添加到您的用户实体
Same as Step 1 Above
2 创建事件监听器
<?php
namespace AcmeUserBundleEventListener;
use SymfonyComponentSecurityCoreSecurityContext;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
use SymfonyComponentHttpKernelHttpKernel;
use DoctrineORMEntityManager;
use AcmeUserBundleEntityUser;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $securityContext;
protected $entityManager;
public function __construct(SecurityContext $securityContext, EntityManager $entityManager)
{
$this->securityContext = $securityContext;
$this->entityManager = $entityManager;
}
/**
* Update the user "lastActivity" on each request
* @param FilterControllerEvent $event
*/
public function onCoreController(FilterControllerEvent $event)
{
// Check that the current request is a "MASTER_REQUEST"
// Ignore any sub-request
if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) {
return;
}
// Check token authentication availability
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
if ( ($user instanceof User) && !($user->isActiveNow()) ) {
$user->setLastActivityAt(new DateTime());
$this->entityManager->flush($user);
}
}
}
}
3 将事件监听器声明为服务
parameters:
acme_user.activity_listener.class: AcmeUserBundleEventListenerActivityListener
services:
acme_user.activity_listener:
class: %acme_user.activity_listener.class%
arguments: [@security.context, @doctrine.orm.entity_manager]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
一切顺利!
相关文章