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 Jun 2, 2021
1 parent fb44e6b commit fcdf1df
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 136 deletions.
41 changes: 41 additions & 0 deletions api/src/main/java/org/openmrs/util/CurrentUsers.java
Original file line number Diff line number Diff line change
@@ -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.util;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.openmrs.User;
import org.openmrs.UserSessionListener;
import org.springframework.stereotype.Component;

@Component
public class CurrentUsers implements UserSessionListener {

private static Set<String> currentlyLoggedInUsers = Collections.synchronizedSet(new LinkedHashSet(500));

@Override
public void loggedInOrOut(User user, Event event, Status status) {
if(!(status == Status.SUCCESS)) {
return;
}
if(event == Event.LOGIN) {
currentlyLoggedInUsers.add(user.getUsername());
}
else if(event == Event.LOGOUT) {
currentlyLoggedInUsers.remove(user.getUsername());
}
}

public static Set<String> getCurrentUsernames(){
return Collections.unmodifiableSet(new LinkedHashSet(currentlyLoggedInUsers));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,35 @@
* 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;
package org.openmrs.util;

import java.util.Set;

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.api.context.Context;
import org.openmrs.web.filter.util.CurrentUsers;
import org.openmrs.web.test.BaseWebContextSensitiveTest;
import org.openmrs.api.context.Credentials;
import org.openmrs.api.context.UsernamePasswordCredentials;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;

public class CurrentUsersTest extends BaseWebContextSensitiveTest {

private static final String USER_SET = "org/openmrs/CurrentUserTest.xml";
public class CurrentUsersTest extends BaseContextSensitiveTest {
private static final String USER_SET = "org/openmrs/util/CurrentUserTest.xml";
@Autowired
UserService userService;

@Test

public void getCurrentUsernames_shoulReturnUserNamesForLoggedInUsers() {
executeDataSet(USER_SET);
MockHttpSession session = new MockHttpSession();
User user = userService.getUser(5508);
Context.authenticate(user.getUsername(),"User12345");
Credentials credentials = new UsernamePasswordCredentials("Mukembo","Mukembo123");
Context.authenticate(credentials);
Assert.assertEquals(Context.getAuthenticatedUser().getUsername(),user.getUsername());
CurrentUsers.addUser(session);
List<String> currentUserNames = CurrentUsers.getCurrentUsernames(session);
Assert.assertTrue(currentUserNames.contains("firstaccount"));
Set<String> currentUserNames = CurrentUsers.getCurrentUsernames();
Assert.assertTrue(currentUserNames.contains("Mukembo"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
-->
<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"/>
<users user_id="5508" person_id="5508" system_id="508-x" username="Mukembo" password="07082424c46568fa5442a3ddd9521ff8bf0715a6c4d411b2f79a37a2d45b5c14c97e2f3754f7367390bd68ec7aa22c5f1891dc413b3861063d5973fb4cd4bcda" salt="d485cc2ce9e8f15e4b15411363ee03471c72b2579dbf7d3684ed485ec94d8f480e281db0abc77d09bcd7fe96e6cc9a80423f69a8a594d5825cf02aedc4da787e" 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="2eadf946-e53c-11de-8404-001e378eb67e"/>
</dataset>
121 changes: 0 additions & 121 deletions web/src/main/java/org/openmrs/web/filter/util/CurrentUsers.java

This file was deleted.

0 comments on commit fcdf1df

Please sign in to comment.