-
Notifications
You must be signed in to change notification settings - Fork 4.2k
RA-552:Adding the View Logged in Users functionality to core #3706
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0c6e572
RA-552:Adding the View Logged in Users functionality to core
HerbertYiga fa74a8a
Update api/src/main/java/org/openmrs/util/CurrentUsers.java
ibacher e637bf6
Update api/src/main/java/org/openmrs/util/CurrentUsers.java
ibacher 8114fe6
Update api/src/main/java/org/openmrs/util/CurrentUsers.java
ibacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(){ | ||
ibacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Collections.unmodifiableSet(new LinkedHashSet(currentlyLoggedInUsers)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
16
api/src/test/resources/org/openmrs/util/CurrentUserTest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.