From 3a46d26f8859b6baf2a46e468b9472eaed7c2949 Mon Sep 17 00:00:00 2001 From: HerbertYiga Date: Mon, 1 Mar 2021 22:33:56 +0300 Subject: [PATCH] RA-552:Adding the View Logged in Users functionality to core --- .../java/org/openmrs/util/CurrentUsers.java | 41 ++++++++++++++++++ .../org/openmrs/util/CurrentUsersTest.java | 42 +++++++++++++++++++ .../org/openmrs/util/CurrentUserTest.xml | 16 +++++++ 3 files changed, 99 insertions(+) create mode 100644 api/src/main/java/org/openmrs/util/CurrentUsers.java create mode 100644 api/src/test/java/org/openmrs/util/CurrentUsersTest.java create mode 100644 api/src/test/resources/org/openmrs/util/CurrentUserTest.xml diff --git a/api/src/main/java/org/openmrs/util/CurrentUsers.java b/api/src/main/java/org/openmrs/util/CurrentUsers.java new file mode 100644 index 000000000000..b9bc53773873 --- /dev/null +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -0,0 +1,41 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.util; + +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; +import org.openmrs.User; +import org.openmrs.UserSessionListener; +import org.springframework.stereotype.Component; + +@Component +public class CurrentUsers implements UserSessionListener { + + private static Set currentlyLoggedInUsers = Collections.synchronizedSet(new LinkedHashSet(500)); + + @Override + public void loggedInOrOut(User user, Event event, Status status) { + if(!(status == Status.SUCCESS)) { + return; + } + if(event == Event.LOGIN) { + currentlyLoggedInUsers.add(user.getUsername()); + } + else if(event == Event.LOGOUT) { + currentlyLoggedInUsers.remove(user.getUsername()); + } + } + + public static Set getCurrentUsernames(){ + return Collections.unmodifiableSet(new LinkedHashSet(currentlyLoggedInUsers)); + } + +} diff --git a/api/src/test/java/org/openmrs/util/CurrentUsersTest.java b/api/src/test/java/org/openmrs/util/CurrentUsersTest.java new file mode 100644 index 000000000000..cc9f6fe2d459 --- /dev/null +++ b/api/src/test/java/org/openmrs/util/CurrentUsersTest.java @@ -0,0 +1,42 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.util; + +import java.util.Set; + +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.openmrs.User; +import org.openmrs.api.UserService; +import org.openmrs.api.context.Context; +import org.openmrs.api.context.Credentials; +import org.openmrs.api.context.UsernamePasswordCredentials; +import org.openmrs.test.jupiter.BaseContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; + +public class CurrentUsersTest extends BaseContextSensitiveTest { + private static final String USER_SET = "org/openmrs/util/CurrentUserTest.xml"; + @Autowired + UserService userService; + + @Test + + public void getCurrentUsernames_shoulReturnUserNamesForLoggedInUsers() { + executeDataSet(USER_SET); + User user = userService.getUser(5508); + Credentials credentials = new UsernamePasswordCredentials("Mukembo","Mukembo123"); + Context.authenticate(credentials); + Assert.assertEquals(Context.getAuthenticatedUser().getUsername(),user.getUsername()); + Set currentUserNames = CurrentUsers.getCurrentUsernames(); + Assert.assertTrue(currentUserNames.contains("Mukembo")); + + } + +} diff --git a/api/src/test/resources/org/openmrs/util/CurrentUserTest.xml b/api/src/test/resources/org/openmrs/util/CurrentUserTest.xml new file mode 100644 index 000000000000..6d623c48ef45 --- /dev/null +++ b/api/src/test/resources/org/openmrs/util/CurrentUserTest.xml @@ -0,0 +1,16 @@ + + + + + +