Symfony2: Inject current user in Service

后端 未结 7 1973
夕颜
夕颜 2020-12-25 10:37

I am trying to inject the currently logged in user into a service. My goal is to extend some twig functionality to output it based on user preferences. In this example I wan

相关标签:
7条回答
  • 2020-12-25 11:14

    The user is a bad candidate to be a service.

    • First it is a model not a service
    • Second there is service security.context where you can get user from.

    In a twig template you can use app.user. See symfony doc global-template-variables. If you want to show something based on user permissions you can do {{ is_granted('ROLE_USER') }}.

    0 讨论(0)
  • 2020-12-25 11:20

    I think that this question deserves an updated answer since 2.6.x+ since the new security component improvements.

    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
    
    class UserDateExtension extends \Twig_Extension
    {
        /**
         * @var TokenStorage
         */
        protected $tokenStorage;
    
    
        /**
         * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage    $tokenStorage
         */
        public function __construct(TokenStorage $tokenStorage)
        {
            $this->tokenStorage = $tokenStorage;
        }
    
        public function getUser()
        {
            return $this->tokenStorage->getToken()->getUser();
        }
    
        public function getFilters()
        {
            return array(
                'user_date' => new \Twig_Filter_Method($this, "formatUserDate"),
            );
        }
    
        public function formatUserDate($date, $format)
        {
            $user = $this->getUser();
            // do stuff
        }
    }
    

    Services.yml

    twig.date_extension:
        class: Acme\Twig\SpecialDateExtension
        tags:
            - { name: twig.extension }
        arguments:
            - "@security.token_storage"
    
    0 讨论(0)
  • 2020-12-25 11:25

    From Symfony 2.6.

    You need use @security.token_storage

    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
    
    class UserDateExtension extends \Twig_Extension
    {
    /**
     * @var TokenStorageInterface
     */
    protected $tokenStorage;
    
    
    /**
     * @param $tokenStorage TokenStorage
     */
    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }
    
    public function getUser()
    {
        return $this->tokenStorage->getToken()->getUser();
    }
    
    public function getFilters()
    {
        return array(
            'user_date' => new \Twig_Filter_Method($this, "formatUserDate"),
        );
    }
    
    public function formatUserDate($date, $format)
    {
        $user = $this->getUser();
        // do stuff
    }
    

    }

    And Services.yml

    twig.date_extension:
        class: Acme\Twig\SpecialDateExtension
        tags:
            - { name: twig.extension }
        arguments: ["@security.token_storage"]
    

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

    0 讨论(0)
  • 2020-12-25 11:29

    I would use a twig extension for that:

    class UserDateExtension extends \Twig_Extension
    {
        private $context;
    
        public function __construct(SecurityContext $context)
        {
            $this->context = $context;
        }
    
        public function getUser()
        {
            return $this->context->getToken()->getUser();
        }
    
        public function getFilters()
        {
            return array(
                'user_date' => new \Twig_Filter_Method($this, "formatUserDate"),
            );
        }
    
        public function formatUserDate($date, $format)
        {
            $user = $this->getUser();
            // do stuff
        }
    

    Now in services.xml

        <service id="user_date_twig_extension" class="%user_date_twig_extension.class%">
            <tag name="twig.extension" />
            <argument type="service" id="security.context" />
        </service>
    

    Then in twig you could do:

    {{ date | user_date('d/m/Y') }}
    
    0 讨论(0)
  • 2020-12-25 11:31

    services.yml

    my_service:
        class: ...
        arguments:
            - "@=service('security.token_storage').getToken().getUser()"
    

    Service.php

    protected $currentUser;
    
    public function __construct($user)
    {
        $this->currentUser = $user;
    }
    

    http://symfony.com/doc/current/book/service_container.html#using-the-expression-language

    0 讨论(0)
  • 2020-12-25 11:33

    You can try injecting @service_container and do $this->container->get('security.context')->getToken()->getUser();.

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