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 4, 2021
1 parent 8acf74a commit 12e8ff9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 91 deletions.
51 changes: 24 additions & 27 deletions web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.openmrs.web.filter.util;

import java.util.ArrayList;

import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -35,12 +36,10 @@ public class CurrentUsers {
* @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;

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
Expand All @@ -49,15 +48,14 @@ public static Map<String,String> init(ServletContext servletContext){
* @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;
}
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.
Expand Down Expand Up @@ -91,17 +89,16 @@ public static void addUser(HttpSession httpSession,User user) {
* @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;
}
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
Expand Up @@ -9,40 +9,33 @@
*/
package org.openmrs.web.filter.update.util;

import java.util.Date;
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(5510);

session.setAttribute("user",user);

User user = userService.getUser(5508);
CurrentUsers.addUser(session,user);
List<String> currentUserNames = CurrentUsers.getCurrentUsernames(session);
Assert.assertEquals(1,currentUserNames.size());


Assert.assertTrue(currentUserNames.contains("firstaccount"));

}

}
50 changes: 0 additions & 50 deletions web/src/test/resources/org/openmrs/CurrentUserTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,6 @@
-->
<dataset>
<location location_id="1" name="Test Location" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="fccbe8f3-597e-4c10-981a-35c541c20541"/>
<person person_id="2" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="f2b7dd73-2d01-4e2b-8ebd-12d832eac8e4"/>
<person_address person_address_id="2" person_id="2" preferred="true" address1="123 First St" address2="Indianapolis" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="1c3fb4c1-0671-4f42-97ea-118837496daf"/>
<person_name person_name_id="2" preferred="true" person_id="2" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="8d6142e2-f256-4fc7-a085-073646c6666d"/>
<patient patient_id="2" creator="1" date_created="2005-01-01 00:00:00.0" voided="false"/>
<patient_identifier_type patient_identifier_type_id="1" name="Test Identifier Type" description="Test description" creator="1" date_created="2005-01-01 00:00:00.0" required="false" retired="false" uuid="9872ce12-59c1-47bd-b7ec-f5786e12830a"/>
<patient_identifier patient_identifier_id="1" patient_id="2" identifier="1234" identifier_type="1" preferred="1" location_id="1" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="7eabecbb-9698-46e0-9abf-362a86d2dccd"/>
<privilege privilege="Some Privilege" description="This is a test privilege" uuid="d979d066-15e6-467c-9d4b-cb575ef97f0f"/>
<role role="Some Role" description="This is a test role" uuid="94d62f4b-dd14-4597-be10-402def31094c"/>
<role role="Some Role To Delete" description="This is a test role" uuid="24v32d4b-dd14-4597-be10-402def31094c"/>
<role role="Parent" description="This is a test parent role" uuid="8de52b9e-e07d-4978-ba4a-a3828c13e482"/>
<role_role parent_role="Parent" child_role="Some Role" />
<role_role parent_role="Parent" child_role="Some Role To Delete" />

<person person_id="5505" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="c4d125f7-5315-446c-9a45-c2f5e0fe96a8"/>
<person_name person_name_id="4" preferred="true" person_id="5505" given_name="Susy" middle_name=" " family_name="Kingman" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="8d6142e2-f256-4fc7-a085-073646c66333"/>

<person person_id="5506" gender="M" dead="false" creator="1" date_created="2008-08-15 15:57:09.0" voided="false" uuid="4e7f6953-0f86-4702-a06b-17112e8f144b"/>
<person_name person_name_id="5" preferred="true" person_id="5506" given_name="Jack" middle_name=" " family_name="Black" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="6fc41fe3-b01f-42a8-a825-547d779b27d5"/>

<person person_id="5507" gender="M" dead="false" creator="1" date_created="2008-08-15 15:57:09.0" voided="false" uuid="dc99369f-f88e-4d54-be56-100e1ff45cf0"/>
<person_name person_name_id="6" preferred="true" person_id="5507" given_name="Hank" middle_name=" " family_name="Williams" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="642e1d7f-00b8-45db-9705-b68341e61782"/>

<person person_id="5508" gender="M" dead="false" creator="1" date_created="2008-08-15 15:57:09.0" voided="false" uuid="dc99369fbvjhbj-njhgvj97-vc6-0nn7"/>
<person_name person_name_id="7" preferred="true" person_id="5508" given_name="Super" middle_name=" " family_name="User" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="6jhloijo-jhbjh877b-jghvky-m888hhy6"/>

<users user_id="5505" person_id="5505" system_id="6-7" username="incorrectlyhashedSha1" password="4a1750c8607dfa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" secret_question="" 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="013c49c6-e132-11de-babe-001e378eb67e"/>
<users user_id="5506" person_id="5506" system_id="7-5" username="correctlyhashedSha1" password="4a1750c8607d0fa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" secret_question="" 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="06d05314-e132-11de-babe-001e378eb67e"/>
<users user_id="5507" person_id="5507" system_id="8-3" username="userWithSha512Hash" email="[email protected]" password="74e3ed4f9e5b955de443feabc9f12ecd291ac6a7eaa2f09a72a5c811ec618b044d7131f687c0a0465fc0c660d09fd245e410e12ffe77d78b2a0c504b40afa202" salt="1c30292f0b438b49b181950f3e831c6a9f38ef1c7d559fca566e8cd6b4efb417150585d4c3345f0fb0b520fc0884ad82ea7c1a28f48c2c562106a3d110716cfc" secret_question="secret" secret_answer="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="0bc18050-e132-11de-babe-001e378eb67e"/>
<users user_id="5508" person_id="5508" system_id="9-9" username="userThatReplacesDaemonForAmbiguousUseCases" password="eeeda5c0cc3837151b2d61cfeab54a91kjbkjb88" salt="42af4c437a47cd778a54flh6gy" secret_question="" secret_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="0bc18050-e132-11de-11kb-hjhh87hui"/>

<user_role user_id="5505" role="Some Role" />
<user_role user_id="5506" role="Some Role" />
<user_role user_id="5507" role="Parent" />

<person person_id="4" gender="M" dead="false" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="f2b7dd73-2d01-4e2b-8ebd-12d832eac8e5"/>
<users user_id="4" person_id="4" system_id="3-7" username="usernumberthree" 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="11ce8b28-e132-11de-babe-001e378eb67e"/>
<person_name person_name_id="5553" preferred="true" person_id="4" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="8d6142e2-f256-4fc7-a085-073646c66663"/>
<person_name person_name_id="5555" preferred="false" person_id="4" given_name="John" middle_name=" " family_name="Doe" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="8d6142e2-f256-4fc7-a085-073646c66664"/>

<user_property user_id="5505" property="some key" property_value="some value" />

<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"/>
<person_name person_name_id="5508" preferred="true" person_id="5508" given_name="Someone" middle_name="With" family_name="MultipleUserAccounts" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" uuid="f5bbff8e-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"/>
<users user_id="5509" person_id="5508" system_id="509-x" username="secondaccount" 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="458f5934-e53c-11de-8404-001e378eb67e"/>
<users user_id="5510" person_id="5508" system_id="510-x" username="oldaccount" 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="true" retire_reason="testing" uuid="658db69a-e53c-11de-8404-001e378eb67e"/>

<person person_id="5511" gender="F" dead="false" creator="1" date_created="2008-08-15 15:46:47.0" voided="false" uuid="edc3d446-e53b-11de-8404-001e378eb23e"/>
<users user_id="5511" person_id="5511" system_id="511-x" username="test" password="e9fb6ffdc1f903bdd8ae621347d5329762de868347d317dacecebf6e9e73f4d97297cb1237d8edaa02c5a9bd25949b251c2169202236455c23cdf4d828487c4a" salt="b3cc5eea8fdc9f8579b07267f209fe184f2202cc6379922e0b13f2753c641808bbba8b0ecf4d0592db8e5001e57876c506ae4d3579d5274d88ca8f26b7e14d9d" 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="658db69a-e53c-11de-8404-001e378eb99e"/>

<user_property user_id="5511" property="some key" property_value="some value" />
</dataset>

0 comments on commit 12e8ff9

Please sign in to comment.