logo

Getting the list of online users

In ELMA, you can track the users who are currently signed in to the system or ELMA Agent. You can do it using the script:

var authService = Locator.GetServiceNotNull<IAuthenticationService>();
//get all the active sessions
var activeSessions = authService.GetActiveSessions();
 
var users = UserManager.Instance.FindByIdArray(activeSessions.Select(c => (long)c.UserId).ToArray());
 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here
The script below applies to PublicAPI 3.8 and higher
//get all the active sessions
var activeSessions = PublicAPI.Services.Authentication.GetActiveSessions();
var users = PublicAPI.Portal.Security.User.FindByIdArray(activeSessions.Select(c => (long)c.UserId).ToArray());

Now, the users variable, which is inside the foreach cycle, stores all the matching IDs, i.e. all the signed in users.

This script can be used in a Code type portlet, to display all the signed in users on the main page. More about the Code portlet

For this, create a portlet and add the following markup:

<script type="text/javascript">
function showUserInfo(id)
    {
        createAndLoadWindow(ТUserInfoWindowТ, { title: ’Online Users’, width: 800 }, Т/Security/User/ProfileInfo/Т + id);
    }
</script>
@using EleWise.ELMA.Security.Models
@using EleWise.ELMA.Model.Managers
@using EleWise.ELMA.Security.Services
@using EleWise.ELMA.Security.Managers
@using EleWise.ELMA.Services
<h2>Online Users</h2>
@{
var authService = Locator.GetServiceNotNull<IAuthenticationService>();
var activeSessions = authService.GetActiveSessions();
}
@{
    var users = UserManager.Instance.FindByIdArray(activeSessions.Select(c => (long)c.UserId).ToArray());
    foreach(var user in users)
    {
<p><a href="javascript:showUserInfo(@user.Id);">@user.FullName</a></p>
    }
}
 
Now this portlet will display the list of all the online system users with links to their profiles