From 0c6e57214cd37877e1bafcc1f80a49b3b1c827c6 Mon Sep 17 00:00:00 2001 From: HerbertYiga Date: Mon, 1 Mar 2021 22:33:56 +0300 Subject: [PATCH 1/4] RA-552:Adding the View Logged in Users functionality to core --- .../java/org/openmrs/util/CurrentUsers.java | 44 +++++++++++++++++++ .../org/openmrs/util/CurrentUsersTest.java | 40 +++++++++++++++++ .../org/openmrs/util/CurrentUserTest.xml | 16 +++++++ 3 files changed, 100 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..242ed6db70c6 --- /dev/null +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -0,0 +1,44 @@ +/** + * 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.openmrs.api.context.Context; +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 != null && user != null) { + 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..c433fdfdb621 --- /dev/null +++ b/api/src/test/java/org/openmrs/util/CurrentUsersTest.java @@ -0,0 +1,40 @@ +/** + * 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_shouldReturnUserNamesForLoggedInUsers() { + 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 @@ + + + + + + From fa74a8a4b32a18c044142278adaa26cf3706b02b Mon Sep 17 00:00:00 2001 From: Ian <52504170+ibacher@users.noreply.github.com> Date: Thu, 24 Jun 2021 13:56:36 -0400 Subject: [PATCH 2/4] Update api/src/main/java/org/openmrs/util/CurrentUsers.java --- api/src/main/java/org/openmrs/util/CurrentUsers.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/org/openmrs/util/CurrentUsers.java b/api/src/main/java/org/openmrs/util/CurrentUsers.java index 242ed6db70c6..31397db2837c 100644 --- a/api/src/main/java/org/openmrs/util/CurrentUsers.java +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -27,13 +27,12 @@ public void loggedInOrOut(User user, Event event, Status status) { if(!(status == Status.SUCCESS)) { return; } + if (event != null && user != null) { - if(event == Event.LOGIN) { - - currentlyLoggedInUsers.add(user.getUsername()); - } else if(event == Event.LOGOUT) { - - currentlyLoggedInUsers.remove(user.getUsername()); + if (event == Event.LOGIN) { + currentlyLoggedInUsers.add(user.getUsername()); + } else if (event == Event.LOGOUT) { + currentlyLoggedInUsers.remove(user.getUsername()); } } } From e637bf6ea1b7948f664ce22632f5aae7fdb03cab Mon Sep 17 00:00:00 2001 From: Ian <52504170+ibacher@users.noreply.github.com> Date: Thu, 24 Jun 2021 13:57:59 -0400 Subject: [PATCH 3/4] Update api/src/main/java/org/openmrs/util/CurrentUsers.java --- api/src/main/java/org/openmrs/util/CurrentUsers.java | 1 + 1 file changed, 1 insertion(+) diff --git a/api/src/main/java/org/openmrs/util/CurrentUsers.java b/api/src/main/java/org/openmrs/util/CurrentUsers.java index 31397db2837c..2b84bf2e159d 100644 --- a/api/src/main/java/org/openmrs/util/CurrentUsers.java +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -36,6 +36,7 @@ public void loggedInOrOut(User user, Event event, Status status) { } } } + public static Set getCurrentUsernames(){ return Collections.unmodifiableSet(new LinkedHashSet(currentlyLoggedInUsers)); } From 8114fe6ff28061da51fdac3dff73fe3446cf20b8 Mon Sep 17 00:00:00 2001 From: Ian <52504170+ibacher@users.noreply.github.com> Date: Thu, 24 Jun 2021 14:00:10 -0400 Subject: [PATCH 4/4] Update api/src/main/java/org/openmrs/util/CurrentUsers.java --- api/src/main/java/org/openmrs/util/CurrentUsers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/org/openmrs/util/CurrentUsers.java b/api/src/main/java/org/openmrs/util/CurrentUsers.java index 2b84bf2e159d..40f7a2241ccd 100644 --- a/api/src/main/java/org/openmrs/util/CurrentUsers.java +++ b/api/src/main/java/org/openmrs/util/CurrentUsers.java @@ -24,7 +24,7 @@ public class CurrentUsers implements UserSessionListener { @Override public void loggedInOrOut(User user, Event event, Status status) { - if(!(status == Status.SUCCESS)) { + if (!(status == Status.SUCCESS)) { return; }