Skip to content

Commit dc9b76e

Browse files
RA-1928: LoginPageController to handle URLs with one "/" only. (#91)
* RA-1928 If only contains one "/" character, it till use the entire urlPath for the urlContextPath * RA-1928 Change from http to https * RA-1928 Change from http to https * RA-1928 Undo changes from http to https * RA-1928 New Test for LoginPageController.java * RA-1928 Update test get_shouldRedirectUserToHomeIfAuthenticatedAndPathForOpenmrsWasGivenOnUrl * RA-1928 Update constants name * RA-1928 Renaming method
1 parent 35f8190 commit dc9b76e

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

omod/src/main/java/org/openmrs/module/referenceapplication/page/controller/LoginPageController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private boolean isUrlWithinOpenmrs(PageRequest pageRequest, String redirectUrl)
162162
try {
163163
URL url = new URL(redirectUrl);
164164
String urlPath = url.getFile();
165-
String urlContextPath = urlPath.substring(0, urlPath.indexOf('/', 1));
165+
String urlContextPath = urlPath.substring(1).contains("/") ? urlPath.substring(0, urlPath.indexOf('/', 1)) : urlPath;
166166
if (StringUtils.equals(pageRequest.getRequest().getContextPath(), urlContextPath)) {
167167
return true;
168168
}

omod/src/test/java/org/openmrs/module/referenceapplication/page/controller/LoginPageControllerTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,33 @@ public void get_shouldRedirectUserToHomeIfAuthenticated() throws Exception {
334334
assertNotEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null,
335335
appFrameworkService, administrationService));
336336
}
337+
338+
/**
339+
* @see LoginPageController#get(org.openmrs.ui.framework.page.PageModel,
340+
* org.openmrs.ui.framework.UiUtils, org.openmrs.ui.framework.page.PageRequest, String,
341+
* org.openmrs.api.LocationService,
342+
* org.openmrs.module.appframework.service.AppFrameworkService,
343+
* org.openmrs.api.AdministrationService)
344+
*/
345+
@Test
346+
@Verifies(value = "should redirect user to home when user is authenticated if ? redirectUrl is for openrms, even if only have '/openrms'", method = "get(PageModel,UiUtils,PageRequest)")
347+
public void get_shouldRedirectUserToHomeIfAuthenticatedAndURLPathAreInOpenmrsScope() throws Exception {
348+
when(Context.isAuthenticated()).thenReturn(true);
349+
when(Context.getUserContext()).thenReturn(mock(UserContext.class));
350+
351+
final String REDIRECT_FULL_URL = "http://openmrs.org/openmrs";
352+
final String REDIRECT_URL_TO_OPENMRS = "/openmrs";
353+
MockHttpServletRequest request = new MockHttpServletRequest();
354+
request.setContextPath(TEST_CONTEXT_PATH);
355+
request.setParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL, REDIRECT_FULL_URL);
356+
PageRequest pageRequest = createPageRequest(request, null);
357+
HttpSession httpSession = new MockHttpSession();
358+
request.setSession(httpSession);
359+
360+
PageModel pageModel = new PageModel();
361+
assertEquals("redirect:" + REDIRECT_URL_TO_OPENMRS, new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null,
362+
appFrameworkService, administrationService));
363+
}
337364

338365
/**
339366
* @see LoginPageController#post(String, String, Integer, LocationService, AdministrationService,

0 commit comments

Comments
 (0)