使用 FOSUserBundle 显示连接用户
Display connect users with FOSUserBundle
我有一个关于使用 FOSUserBundle 列出所有连接用户的问题。我遵循了此 link
的所有步骤
但是当我尝试登录我的用户配置文件时,出现了这个错误:
传递给 App\Repository\UserRepository::__construct() 的参数 1 必须实现接口 Symfony\Bridge\Doctrine\RegistryInterface,给定的 Doctrine\ORM\EntityManager 实例,在 C 中调用: \wamp64\www\staff-test\vendor\doctrine\doctrine-bundle\Repository\ContainerRepositoryFactory.php 第 84 行
我只对我的侦听器进行了修改:
<?php
namespace App\EventListener;
**use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;**
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $tokenStorage;
protected $userManager;
public function __construct(TokenStorage $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->tokenStorage->getToken()**) {
**$user = $this->tokenStorage->getToken()->getUser();**
if ( ($user instanceof UserInterface) && !($user->isActiveNow()) ) {
$user->setLastActivityAt(new \DateTime());
$this->userManager->updateUser($user);
}
}
}
}
在我的 UserRepository 上:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
class UserRepository extends EntityRepository #ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
public function getActive()
{
// Comme vous le voyez, le délais est redondant ici, l'idéale serait de le rendre configurable via votre bundle
$delay = new \DateTime();
$delay->setTimestamp(strtotime('2 minutes ago'));
$qb = $this->createQueryBuilder('u')
->where('u.lastActivity > :delay')
->setParameter('delay', $delay)
;
return $qb->getQuery()->getResult();
}
}
是的,您需要从 ServiceEntityRepository
扩展 UserRepository
。 EntityRepository
的构造函数是 public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $class)
。
我有一个关于使用 FOSUserBundle 列出所有连接用户的问题。我遵循了此 link
的所有步骤但是当我尝试登录我的用户配置文件时,出现了这个错误:
传递给 App\Repository\UserRepository::__construct() 的参数 1 必须实现接口 Symfony\Bridge\Doctrine\RegistryInterface,给定的 Doctrine\ORM\EntityManager 实例,在 C 中调用: \wamp64\www\staff-test\vendor\doctrine\doctrine-bundle\Repository\ContainerRepositoryFactory.php 第 84 行
我只对我的侦听器进行了修改:
<?php
namespace App\EventListener;
**use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;**
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
/**
* Listener that updates the last activity of the authenticated user
*/
class ActivityListener
{
protected $tokenStorage;
protected $userManager;
public function __construct(TokenStorage $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->tokenStorage->getToken()**) {
**$user = $this->tokenStorage->getToken()->getUser();**
if ( ($user instanceof UserInterface) && !($user->isActiveNow()) ) {
$user->setLastActivityAt(new \DateTime());
$this->userManager->updateUser($user);
}
}
}
}
在我的 UserRepository 上:
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
class UserRepository extends EntityRepository #ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
public function getActive()
{
// Comme vous le voyez, le délais est redondant ici, l'idéale serait de le rendre configurable via votre bundle
$delay = new \DateTime();
$delay->setTimestamp(strtotime('2 minutes ago'));
$qb = $this->createQueryBuilder('u')
->where('u.lastActivity > :delay')
->setParameter('delay', $delay)
;
return $qb->getQuery()->getResult();
}
}
是的,您需要从 ServiceEntityRepository
扩展 UserRepository
。 EntityRepository
的构造函数是 public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $class)
。