Skip to content
Advertisement

Symfony how to return all logged in Active Users

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 retrieved from the session (I am using an external LDAP Library for authentication)

I have created a field in the database called lastActivity which will contain the last login time and then I can query the database for lastActivity display users logged in in the last 2 minutes.

ActivityListener.php

JavaScript

Services.yml

JavaScript

User Entity

JavaScript

Advertisement

Answer

There is a great post here: List online users.

You can create a Listener that listens on the kernel.controller event and updates a user field lastActivity every time a user is active. You can check lastActivity < now()- 2 minutes and update lastActivity timestamp.

Also: Implementing user activity in symfony 2

Here is how to do it

Note: If you’re not using FOSUserBundle, see Edit below.

1 Add this to your User Entity

JavaScript

2 Create Event Listener

JavaScript

3 Declare event Listener as a service

JavaScript

And you’re good to go!

Edit (without FOSUserBundle)

1 Add this to your User Entity

JavaScript

2 Create Event Listener

JavaScript

3 Declare event Listener as a service

JavaScript

And you’re good to go!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement