Skip to content

Commit

Permalink
RA-552:Adding the View Logged in Users functionality to core
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertYiga committed Mar 9, 2021
1 parent 352f3d5 commit fbe4dfc
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
104 changes: 104 additions & 0 deletions web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java
Original file line number Diff line number Diff line change
@@ -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<String, String> init(ServletContext servletContext) {
Map<String, String> currentUserMap = Collections.synchronizedMap(new TreeMap<String, String>());
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<String, String> getCurrentUsers(HttpSession httpSession) {
Map<String, String> currentUsers = (Map<String, String>) 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<String,String> 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<String> getCurrentUsernames(HttpSession httpSession) {
Map<String, String> currentUsers = getCurrentUsers(httpSession);
List<String> userNames = new ArrayList<String>();
synchronized (currentUsers) {
for (String value : currentUsers.values()) {
userNames.add(value);
}
}
Collections.sort(userNames);
return userNames;
}

}
Original file line number Diff line number Diff line change
@@ -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.web.filter.update.util;

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.web.filter.util.CurrentUsers;
import org.openmrs.web.test.BaseWebContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;

public class CurrentUsersTest extends BaseWebContextSensitiveTest {

protected static final String USER_SET = "org/openmrs/CurrentUserTest.xml";
@Autowired
UserService userService;

@Test

public void getCurrentUsernames_shoulReturnAllCurrentUserNames() {
executeDataSet(USER_SET);
MockHttpSession session = new MockHttpSession();
User user = userService.getUser(5508);
CurrentUsers.addUser(session,user);
List<String> currentUserNames = CurrentUsers.getCurrentUsernames(session);
Assert.assertTrue(currentUserNames.contains("firstaccount"));

}

}
16 changes: 16 additions & 0 deletions web/src/test/resources/org/openmrs/CurrentUserTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
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.
-->
<dataset>
<person person_id="5508" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="edc3d446-e53b-11de-8404-001e378eb67e"/>
<users user_id="5508" person_id="5508" system_id="508-x" username="firstaccount" password="74e3ed4f9e5b955de443feabc9f12ecd291ac6a7eaa2f09a72a5c811ec618b044d7131f687c0a0465fc0c660d09fd245e410e12ffe77d78b2a0c504b40afa202" salt="1c30292f0b438b49b181950f3e831c6a9f38ef1c7d559fca566e8cd6b4efb417150585d4c3345f0fb0b520fc0884ad82ea7c1a28f48c2c562106a3d110716cfc" secret_question="a secret" secret_answer="an answer" creator="1" date_created="2008-08-15 15:57:09.0" changed_by="1" date_changed="2008-08-18 11:51:56.0" retired="false" retire_reason="" uuid="2eadf946-e53c-11de-8404-001e378eb67e"/>
</dataset>

0 comments on commit fbe4dfc

Please sign in to comment.