SQL and performance improvements (#2017)
This commit is contained in:
@@ -10,48 +10,37 @@
|
||||
namespace App\Security;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Repository\UserRepository;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
/**
|
||||
* @deprecated will be removed with 2.0
|
||||
*/
|
||||
final class CurrentUser
|
||||
{
|
||||
/**
|
||||
* @var TokenStorageInterface
|
||||
*/
|
||||
private $storage;
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $repository;
|
||||
/**
|
||||
* @var User|null
|
||||
*/
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* @param TokenStorageInterface $storage
|
||||
* @param UserRepository $repository
|
||||
*/
|
||||
public function __construct(TokenStorageInterface $storage, UserRepository $repository)
|
||||
public function __construct(TokenStorageInterface $storage)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User|null
|
||||
*/
|
||||
public function getUser()
|
||||
public function getUser(): ?User
|
||||
{
|
||||
if (null === $this->storage->getToken()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// some inline caching to prevent multiple DB lookups
|
||||
if (null !== $this->user) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
if (null === $this->storage->getToken()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var User $user */
|
||||
$user = $this->storage->getToken()->getUser();
|
||||
|
||||
@@ -59,7 +48,7 @@ final class CurrentUser
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->user = $this->repository->getUserById($user->getId());
|
||||
$this->user = $user;
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user