Symfony how to return all logged in Active Users

前端 未结 5 2033
-上瘾入骨i
-上瘾入骨i 2020-12-05 01:31

I want to return all Logged in users of my application and render it in my Dashboard. The user_id and user_name should be retrieve

5条回答
  •  -上瘾入骨i
    2020-12-05 02:01

    As I can't comment on posts, I'd still like to give a remark on the answer by Mick via this answer.

    Since Symfony 2.6 the SecurityContext class is deprecated and, in this case, the TokenStorage class should be used instead.

    Thus, the services.yml would be as follows:

    services:
        acme_user.activity_listener:
            class: %acme_user.activity_listener.class%
            arguments: ['@security.token_storage', '@doctrine.orm.entity_manager']
            tags:
                - { name: kernel.event_listener, event: kernel.controller, method: onCoreController }
    

    And, instead of

    use Symfony\Component\Security\Core\SecurityContext;
    

    One should

    use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
    

    (also replace the SecurityContext inside the class with the TokenStorage class)

    Then, on line 38, the token availability would be checked using

    $this->tokenStorage->getToken()
    

    And, on line 39, the user instance would be obtained using

    $this->tokenStorage->getToken()->getUser()
    

提交回复
热议问题