How to get the current logged User in a service

前端 未结 7 1113
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 20:25

In Symfony 2.8/3.0, with our fancy new security components, how do I get the currently logged User (i.e. FOSUser) object in a service without

相关标签:
7条回答
  • 2020-12-03 20:59

    Works with Symfony 3.4, 4.x, 5.x & above. The Security utility class was introduced in 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

    0 讨论(0)
  • 2020-12-03 21:01

    if you class extend of Controller

    $this->get('security.context')->getToken()->getUser();
    

    Or, if you has access to container element..

    $container = $this->configurationPool->getContainer();
    $user = $container->get('security.context')->getToken()->getUser();
    

    http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

    0 讨论(0)
  • 2020-12-03 21:02

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

    See doc : https://symfony.com/doc/current/security.html#retrieving-the-user-object

    0 讨论(0)
  • 2020-12-03 21:10

    From Symfony 3.3, from a Controller only, according this blog post: https://symfony.com/blog/new-in-symfony-3-2-user-value-resolver-for-controllers

    It's easy as:

    use Symfony\Component\Security\Core\User\UserInterface
    
    public function indexAction(UserInterface $user)
    {...}
    
    0 讨论(0)
  • 2020-12-03 21:16

    Using constructor dependency injection, you can do it this way:

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 21:17

    Symfony does this in 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;
    }
    

    So if you simply inject and replace security.token_storage, you're good to go.

    0 讨论(0)
提交回复
热议问题