Skip to content

Commit 5564667

Browse files
RESTWS-946: /session endpoint throws an error if user doesn't have Get Providers privilege (#613)
1 parent 57b2e5e commit 5564667

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void delete(HttpServletRequest request) {
132132
*
133133
* @return Provider if the user is authenticated
134134
*/
135-
private Provider getCurrentProvider() {
135+
protected Provider getCurrentProvider() {
136136
Provider currentProvider = null;
137137
User currentUser = Context.getAuthenticatedUser();
138138
if (currentUser != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;
11+
12+
import java.util.Collection;
13+
import java.util.HashSet;
14+
15+
import org.openmrs.Provider;
16+
import org.openmrs.User;
17+
import org.openmrs.api.context.Context;
18+
import org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9.SessionController1_9;
19+
import org.openmrs.util.PrivilegeConstants;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.stereotype.Controller;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
25+
/**
26+
* @see SessionController1_9
27+
*/
28+
@Controller
29+
@RequestMapping
30+
public class SessionController2_0 extends SessionController1_9 {
31+
32+
private static final Logger log = LoggerFactory.getLogger(SessionController2_0.class);
33+
34+
/**
35+
* @see SessionController1_9#getCurrentProvider()
36+
*/
37+
@Override
38+
protected Provider getCurrentProvider() {
39+
Provider currentProvider = null;
40+
User currentUser = Context.getAuthenticatedUser();
41+
if (currentUser != null) {
42+
Collection<Provider> providers = new HashSet<Provider>();
43+
try {
44+
Context.addProxyPrivilege(PrivilegeConstants.GET_PROVIDERS);
45+
if (currentUser.getPerson() != null) {
46+
providers = Context.getProviderService().getProvidersByPerson(currentUser.getPerson(), false);
47+
}
48+
}
49+
finally {
50+
Context.removeProxyPrivilege(PrivilegeConstants.GET_PROVIDERS);
51+
}
52+
if (providers.size() > 1) {
53+
log.warn("Can't handle users with multiple provider accounts");
54+
} else if (providers.size() == 1) {
55+
currentProvider = providers.iterator().next();
56+
}
57+
}
58+
return currentProvider;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;
11+
12+
import org.apache.commons.beanutils.PropertyUtils;
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
import org.openmrs.api.context.Context;
16+
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
17+
18+
/**
19+
* Tests functionality of {@link SessionController2_0}
20+
*/
21+
public class SessionController2_0Test extends BaseModuleWebContextSensitiveTest {
22+
23+
/**
24+
* @see SessionController2_0#get()
25+
* @verifies return the session with current provider if the user doesn't have Get Providers privilege
26+
*/
27+
@Test
28+
public void get_shouldReturnCurrentProviderIfTheUserDoesNotHaveGetProvidersPrivilege() throws Exception {
29+
executeDataSet("sessionControllerTestDataset.xml");
30+
31+
// authenticate new user without privileges
32+
Context.logout();
33+
Context.authenticate("test_user", "test");
34+
Assert.assertTrue(Context.isAuthenticated());
35+
36+
SessionController2_0 controller = Context.getRegisteredComponents(SessionController2_0.class).iterator().next();
37+
38+
Object ret = controller.get();
39+
Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider");
40+
Assert.assertNotNull(currentProvider);
41+
Assert.assertTrue(currentProvider.toString().contains("Test Provider"));
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public License,
5+
v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
7+
the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
8+
9+
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
10+
graphic logo is a trademark of OpenMRS Inc.
11+
12+
-->
13+
<dataset>
14+
15+
<person person_id="601" gender="M" dead="false" birthdate_estimated="0" creator="1" date_created="2008-08-15 15:57:09.0" voided="false" uuid="hy6b4e41-790c-484f-b6ed-71dc3e4222de"/>
16+
<users user_id="601" person_id="601" system_id="7-5" username="test_user" password="4a1750c8607d0fa237de36c6305715c223415189" salt="c788c6ad82a157b712392ca695dfcf2eed193d7f" creator="1" date_created="2008-08-15 15:57:09.0" retired="false" uuid="06d05314-e132-11de-babe-001e37123456"/>
17+
<provider provider_id="601" person_id="601" name="Mr. Test Provider" identifier="Test Provider" creator="1" date_created="2008-08-15 15:57:09.0" retired="false" uuid="e1009293-c561-47ae-b112-214052c17888" />
18+
19+
</dataset>

0 commit comments

Comments
 (0)