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/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUsersTest.java b/api/src/test/java/org/openmrs/util/CurrentUsersTest.java similarity index 59% rename from web/src/test/java/org/openmrs/web/filter/update/util/CurrentUsersTest.java rename to api/src/test/java/org/openmrs/util/CurrentUsersTest.java index fa2861e8838c..cc9f6fe2d459 100644 --- a/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUsersTest.java +++ b/api/src/test/java/org/openmrs/util/CurrentUsersTest.java @@ -7,36 +7,35 @@ * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS * graphic logo is a trademark of OpenMRS Inc. */ -package org.openmrs.web.filter.update.util; +package org.openmrs.util; + +import java.util.Set; -import java.util.List; 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.web.filter.util.CurrentUsers; -import org.openmrs.web.test.BaseWebContextSensitiveTest; +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; -import org.springframework.mock.web.MockHttpSession; - -public class CurrentUsersTest extends BaseWebContextSensitiveTest { - private static final String USER_SET = "org/openmrs/CurrentUserTest.xml"; +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); - MockHttpSession session = new MockHttpSession(); User user = userService.getUser(5508); - Context.authenticate(user.getUsername(),"User12345"); + Credentials credentials = new UsernamePasswordCredentials("Mukembo","Mukembo123"); + Context.authenticate(credentials); Assert.assertEquals(Context.getAuthenticatedUser().getUsername(),user.getUsername()); - CurrentUsers.addUser(session); - List currentUserNames = CurrentUsers.getCurrentUsernames(session); - Assert.assertTrue(currentUserNames.contains("firstaccount")); + Set currentUserNames = CurrentUsers.getCurrentUsernames(); + Assert.assertTrue(currentUserNames.contains("Mukembo")); } diff --git a/web/src/test/resources/org/openmrs/CurrentUserTest.xml b/api/src/test/resources/org/openmrs/util/CurrentUserTest.xml similarity index 58% rename from web/src/test/resources/org/openmrs/CurrentUserTest.xml rename to api/src/test/resources/org/openmrs/util/CurrentUserTest.xml index 3524c74406ac..6d623c48ef45 100644 --- a/web/src/test/resources/org/openmrs/CurrentUserTest.xml +++ b/api/src/test/resources/org/openmrs/util/CurrentUserTest.xml @@ -12,5 +12,5 @@ --> - + diff --git a/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java b/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java deleted file mode 100644 index eb04cb5b6b68..000000000000 --- a/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * 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.web.filter.util; - -import java.util.ArrayList; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpSession; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.openmrs.User; -import org.openmrs.api.context.Context; -import org.openmrs.web.WebConstants; - -public class CurrentUsers { - - private static final Log log = LogFactory.getLog(CurrentUsers.class); - - /** - * Initialize the current users list. - * - * @param servletContext - */ - - public static Map init(ServletContext servletContext) { - Map currentUserMap = Collections.synchronizedMap(new TreeMap()); - servletContext.setAttribute(WebConstants.CURRENT_USERS, currentUserMap); - return currentUserMap; - } - /** - * Get the current list of map of users stored in the session - * - * @param httpSession the current session - * @return map of users logged in - */ - @SuppressWarnings("unchecked") - private static Map getCurrentUsers(HttpSession httpSession) { - Map currentUsers = (Map) httpSession.getServletContext().getAttribute( - WebConstants.CURRENT_USERS); - if (currentUsers == null) { - currentUsers = init(httpSession.getServletContext()); - } - return currentUsers; - } - - /** - * Add the user to the current users. - * - * @param httpSession - * @param user the user that just logged in - */ - public static void addUser(HttpSession httpSession) { - User user = Context.getAuthenticatedUser(); - Map currentUsers = getCurrentUsers(httpSession); - - String currentUserName = user.getUsername(); - //if the user name is blank then print their system id - if(StringUtils.isBlank(currentUserName)) { - currentUserName = "systemid:" + user.getSystemId(); - - } - - if(log.isDebugEnabled()) { - - log.debug("Adding current user name" + currentUserName); - - } - - currentUsers.put(httpSession.getId(),currentUserName); - - } - - /** - * Remove the current user from the list of current users. - * - * @param httpSession - */ - public static void removeUser(HttpSession httpSession) { - String sessionId = httpSession.getId(); - Map currentUsers = getCurrentUsers(httpSession); - if (log.isDebugEnabled()) { - log.debug("Removing user from the current users. session: " + sessionId + " user: " - + currentUsers.get(sessionId)); - } - currentUsers.remove(sessionId); - } - - /** - * Get sorted user names list. - * - * @param httpSession - * @return sorted user names - */ - public static List getCurrentUsernames(HttpSession httpSession) { - Map currentUsers = getCurrentUsers(httpSession); - List userNames = new ArrayList(); - synchronized (currentUsers) { - for (String value : currentUsers.values()) { - userNames.add(value); - } - } - Collections.sort(userNames); - return userNames; - } - -}