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 bae8500 commit 0c6e572
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
44 changes: 44 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,44 @@
/**
* 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.openmrs.api.context.Context;
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 != null && user != null) {
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));
}

}
40 changes: 40 additions & 0 deletions api/src/test/java/org/openmrs/util/CurrentUsersTest.java
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.util;

import java.util.Set;
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.api.context.Credentials;
import org.openmrs.api.context.UsernamePasswordCredentials;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;

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

@Test
public void getCurrentUsernames_shouldReturnUserNamesForLoggedInUsers() {
executeDataSet(USER_SET);
User user = userService.getUser(5508);
Credentials credentials = new UsernamePasswordCredentials("Mukembo","Mukembo123");
Context.authenticate(credentials);
Assert.assertEquals(Context.getAuthenticatedUser().getUsername(),user.getUsername());
Set<String> currentUserNames = CurrentUsers.getCurrentUsernames();
Assert.assertTrue(currentUserNames.contains("Mukembo"));
}

}
16 changes: 16 additions & 0 deletions api/src/test/resources/org/openmrs/util/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="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>

0 comments on commit 0c6e572

Please sign in to comment.