如何获取服务中的当前登录用户
How to get the current logged User in a service
在 Symfony 2.8/3.0 中,有了我们奇特的新安全组件,我如何在服务 没有注入整个容器?
是否有可能以 非 hacky 方式?
PS:让我们不要考虑“将其作为参数传递给服务函数”,因为它非常明显。还有,脏。
将security.token_storage
服务注入到你的服务中,然后使用:
$this->token_storage->getToken()->getUser();
如此处所述:http://symfony.com/doc/current/book/security.html#retrieving-the-user-object and here: http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services
如果您class扩展控制器
$this->get('security.context')->getToken()->getUser();
或者,如果您有权访问容器元素..
$container = $this->configurationPool->getContainer();
$user = $container->get('security.context')->getToken()->getUser();
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
使用构造函数依赖注入,你可以这样做:
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class A
{
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function foo()
{
dump($this->user);
}
}
Symfony 在 Symfony\Bundle\FrameworkBundle\ControllerControllerTrait
中执行此操作
protected function getUser()
{
if (!$this->container->has('security.token_storage')) {
throw new \LogicException('The SecurityBundle is not registered in your application.');
}
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
return;
}
if (!is_object($user = $token->getUser())) {
// e.g. anonymous authentication
return;
}
return $user;
}
因此,如果您只是注入并替换 security.token_storage
,就可以了。
来自 Symfony 3.3
,仅来自控制器,根据此博客 post:https://symfony.com/blog/new-in-symfony-3-2-user-value-resolver-for-controllers
很简单:
use Symfony\Component\Security\Core\User\UserInterface
public function indexAction(UserInterface $user)
{...}
适用于 Symfony 3.4、4.x、5.x 及更高版本。安全实用程序 class 是在 Symfony 3.4 中引入的。
use Symfony\Component\Security\Core\Security;
public function indexAction(Security $security)
{
$user = $security->getUser();
}
https://symfony.com/doc/3.4/security.html#always-check-if-the-user-is-logged-in
在 symfo 4 中:
use Symfony\Component\Security\Core\Security;
class ExampleService
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function someMethod()
{
$user = $this->security->getUser();
}
}
参见文档:https://symfony.com/doc/current/security.html#retrieving-the-user-object
这在 Symfony 5.2 中运行良好
$this->get('security.token_storage')->getToken()->getUser()
使用 Symfony 5.2+ 和 PHP 8.0+,您还可以使用 #[CurrentUser]
属性
获取登录用户
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
class FooController extends AbstractController
{
public function index(#[CurrentUser] ?User $user)
{
// ...
}
}
在 Symfony 2.8/3.0 中,有了我们奇特的新安全组件,我如何在服务 没有注入整个容器?
是否有可能以 非 hacky 方式?
PS:让我们不要考虑“将其作为参数传递给服务函数”,因为它非常明显。还有,脏。
将security.token_storage
服务注入到你的服务中,然后使用:
$this->token_storage->getToken()->getUser();
如此处所述:http://symfony.com/doc/current/book/security.html#retrieving-the-user-object and here: http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services
如果您class扩展控制器
$this->get('security.context')->getToken()->getUser();
或者,如果您有权访问容器元素..
$container = $this->configurationPool->getContainer();
$user = $container->get('security.context')->getToken()->getUser();
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
使用构造函数依赖注入,你可以这样做:
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class A
{
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function foo()
{
dump($this->user);
}
}
Symfony 在 Symfony\Bundle\FrameworkBundle\ControllerControllerTrait
中执行此操作protected function getUser()
{
if (!$this->container->has('security.token_storage')) {
throw new \LogicException('The SecurityBundle is not registered in your application.');
}
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
return;
}
if (!is_object($user = $token->getUser())) {
// e.g. anonymous authentication
return;
}
return $user;
}
因此,如果您只是注入并替换 security.token_storage
,就可以了。
来自 Symfony 3.3
,仅来自控制器,根据此博客 post:https://symfony.com/blog/new-in-symfony-3-2-user-value-resolver-for-controllers
很简单:
use Symfony\Component\Security\Core\User\UserInterface
public function indexAction(UserInterface $user)
{...}
适用于 Symfony 3.4、4.x、5.x 及更高版本。安全实用程序 class 是在 Symfony 3.4 中引入的。
use Symfony\Component\Security\Core\Security;
public function indexAction(Security $security)
{
$user = $security->getUser();
}
https://symfony.com/doc/3.4/security.html#always-check-if-the-user-is-logged-in
在 symfo 4 中:
use Symfony\Component\Security\Core\Security;
class ExampleService
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function someMethod()
{
$user = $this->security->getUser();
}
}
参见文档:https://symfony.com/doc/current/security.html#retrieving-the-user-object
这在 Symfony 5.2 中运行良好
$this->get('security.token_storage')->getToken()->getUser()
使用 Symfony 5.2+ 和 PHP 8.0+,您还可以使用 #[CurrentUser]
属性
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
class FooController extends AbstractController
{
public function index(#[CurrentUser] ?User $user)
{
// ...
}
}