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 3, 2021
1 parent dfd3392 commit 30fa4a4
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 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,97 @@
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;
}

}

0 comments on commit 30fa4a4

Please sign in to comment.