Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RA-552:Adding the View Logged in Users functionality to core #3706

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
ibacher marked this conversation as resolved.
Show resolved Hide resolved
return;
}
if (event != null && user != null) {
if(event == Event.LOGIN) {

currentlyLoggedInUsers.add(user.getUsername());
} else if(event == Event.LOGOUT) {

currentlyLoggedInUsers.remove(user.getUsername());
}
}
ibacher marked this conversation as resolved.
Show resolved Hide resolved
}
public static Set<String> getCurrentUsernames(){
ibacher marked this conversation as resolved.
Show resolved Hide resolved
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>