Skip to content

Commit beacb42

Browse files
committed
RA-552:Adding the View Logged in Users functionality to core
1 parent dfd3392 commit beacb42

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
11+
package org.openmrs.web.filter.util;
12+
13+
import java.util.ArrayList;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.TreeMap;
19+
20+
import javax.servlet.ServletContext;
21+
import javax.servlet.http.HttpSession;
22+
23+
import org.apache.commons.lang.StringUtils;
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
26+
import org.openmrs.User;
27+
import org.openmrs.web.WebConstants;
28+
29+
public class CurrentUsers {
30+
31+
private static final Log log = LogFactory.getLog(CurrentUsers.class);
32+
33+
/**
34+
* Initialize the current users list.
35+
*
36+
* @param servletContext
37+
*/
38+
39+
public static Map<String, String> init(ServletContext servletContext) {
40+
Map<String, String> currentUserMap = Collections.synchronizedMap(new TreeMap<String, String>());
41+
servletContext.setAttribute(WebConstants.CURRENT_USERS, currentUserMap);
42+
return currentUserMap;
43+
}
44+
/**
45+
* Get the current list of map of users stored in the session
46+
*
47+
* @param httpSession the current session
48+
* @return map of users logged in
49+
*/
50+
@SuppressWarnings("unchecked")
51+
private static Map<String, String> getCurrentUsers(HttpSession httpSession) {
52+
Map<String, String> currentUsers = (Map<String, String>) httpSession.getServletContext().getAttribute(
53+
WebConstants.CURRENT_USERS);
54+
if (currentUsers == null) {
55+
currentUsers = init(httpSession.getServletContext());
56+
}
57+
return currentUsers;
58+
}
59+
60+
/**
61+
* Add the user to the current users.
62+
*
63+
* @param httpSession
64+
* @param user the user that just logged in
65+
*/
66+
public static void addUser(HttpSession httpSession,User user) {
67+
Map<String,String> currentUsers = getCurrentUsers(httpSession);
68+
69+
String currentUserName = user.getUsername();
70+
//if the user name is blank then print their system id
71+
if(StringUtils.isBlank(currentUserName)) {
72+
currentUserName = "systemid:" + user.getSystemId();
73+
74+
}
75+
76+
if(log.isDebugEnabled()) {
77+
78+
log.debug("Adding current user name" + currentUserName);
79+
80+
}
81+
82+
currentUsers.put(httpSession.getId(),currentUserName);
83+
84+
}
85+
86+
/**
87+
* Get sorted user names list.
88+
*
89+
* @param httpSession
90+
* @return sorted user names
91+
*/
92+
public static List<String> getCurrentUsernames(HttpSession httpSession) {
93+
Map<String, String> currentUsers = getCurrentUsers(httpSession);
94+
List<String> userNames = new ArrayList<String>();
95+
synchronized (currentUsers) {
96+
for (String value : currentUsers.values()) {
97+
userNames.add(value);
98+
}
99+
}
100+
Collections.sort(userNames);
101+
return userNames;
102+
}
103+
104+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.web.filter.update.util;
11+
12+
import java.util.List;
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
import org.openmrs.User;
16+
import org.openmrs.api.UserService;
17+
import org.openmrs.test.BaseContextSensitiveTest;
18+
19+
import org.openmrs.web.filter.util.CurrentUsers;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.mock.web.MockHttpSession;
22+
23+
public class CurrentUserTest extends BaseContextSensitiveTest {
24+
25+
protected static final String USER_SET = "org/openmrs/CurrentUserTest.xml";
26+
@Autowired
27+
UserService userService;
28+
29+
@SuppressWarnings("deprecation")
30+
@Test
31+
public void getCurrentUsernames_shoulReturnCurrentUserNamesForAgivenSession() {
32+
executeDataSet(USER_SET);
33+
MockHttpSession session = new MockHttpSession();
34+
User user = userService.getUser(5508);
35+
CurrentUsers.addUser(session,user);
36+
List<String> currentUserNames = CurrentUsers.getCurrentUsernames(session);
37+
Assert.assertTrue(currentUserNames.contains("firstaccount"));
38+
39+
}
40+
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public License,
5+
v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
7+
the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
8+
9+
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
10+
graphic logo is a trademark of OpenMRS Inc.
11+
12+
-->
13+
<dataset>
14+
<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"/>
15+
<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"/>
16+
</dataset>

0 commit comments

Comments
 (0)