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..40f7a2241ccd --- /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 @@ + + + + + +