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