From beacb42a33e1804e1e916418654763019f4cfe39 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 --- .../openmrs/web/filter/util/CurrentUsers.java | 104 ++++++++++++++++++ .../filter/update/util/CurrentUserTest.java | 41 +++++++ .../resources/org/openmrs/CurrentUserTest.xml | 16 +++ 3 files changed, 161 insertions(+) create mode 100644 web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java create mode 100644 web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java create mode 100644 web/src/test/resources/org/openmrs/CurrentUserTest.xml 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 new file mode 100644 index 000000000000..463aec3534db --- /dev/null +++ b/web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java @@ -0,0 +1,104 @@ +/** + * 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.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) { + 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); + + } + + /** + * 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; + } + +} diff --git a/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java b/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.java new file mode 100644 index 000000000000..26033d50136a --- /dev/null +++ b/web/src/test/java/org/openmrs/web/filter/update/util/CurrentUserTest.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.web.filter.update.util; + +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.openmrs.User; +import org.openmrs.api.UserService; +import org.openmrs.test.BaseContextSensitiveTest; + +import org.openmrs.web.filter.util.CurrentUsers; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; + +public class CurrentUserTest extends BaseContextSensitiveTest { + + protected static final String USER_SET = "org/openmrs/CurrentUserTest.xml"; + @Autowired + UserService userService; + + @SuppressWarnings("deprecation") + @Test + public void getCurrentUsernames_shoulReturnCurrentUserNamesForAgivenSession() { + executeDataSet(USER_SET); + MockHttpSession session = new MockHttpSession(); + User user = userService.getUser(5508); + CurrentUsers.addUser(session,user); + List currentUserNames = CurrentUsers.getCurrentUsernames(session); + Assert.assertTrue(currentUserNames.contains("firstaccount")); + + } + +} diff --git a/web/src/test/resources/org/openmrs/CurrentUserTest.xml b/web/src/test/resources/org/openmrs/CurrentUserTest.xml new file mode 100644 index 000000000000..3524c74406ac --- /dev/null +++ b/web/src/test/resources/org/openmrs/CurrentUserTest.xml @@ -0,0 +1,16 @@ + + + + + +