Skip to content

Commit 579f24e

Browse files
RA-1908: LoginPageController to set the "clientTimezone" user property. (#88)
* HTML-755 Add userProperty "clientTimezone" when selecting a location. Add userProperty "clientTimezone" when selecting a location. If we only have 1 location, if timezones are enabled and that user don't have the user proprety "clientTimezone" set on the DB, it will force the user to click on that single location, if timezones are not enable then it will skip the location selection screen(if only 1 location are possible for that user). Update the UiFramework version. * HTML-755 Fix typo and formatting code * HTML-755 Formatting code * HTML-755 Correct typo * HTML-755 Change method name. * HTML-755 Adding new test * HTML-755 Adding new test * HTML-755 UPPERCASE constants names * HTML-755 Constant Convention changes.
1 parent 0410c09 commit 579f24e

4 files changed

Lines changed: 98 additions & 39 deletions

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ private String getRedirectUrl(PageRequest pageRequest) {
230230
public String post(@RequestParam(value = "username", required = false) String username,
231231
@RequestParam(value = "password", required = false) String password,
232232
@RequestParam(value = "sessionLocation", required = false) Integer sessionLocationId,
233+
@RequestParam(value = "clientTimezone", required = false) String clientTimezone,
233234
@SpringBean("locationService") LocationService locationService,
234235
@SpringBean("adminService") AdministrationService administrationService, UiUtils ui,
235236
@SpringBean("appFrameworkService") AppFrameworkService appFrameworkService, PageRequest pageRequest,
@@ -240,6 +241,9 @@ public String post(@RequestParam(value = "username", required = false) String us
240241
Location sessionLocation = null;
241242
if (sessionLocationId != null) {
242243
try {
244+
if (StringUtils.isNotEmpty(clientTimezone)) {
245+
ui.setClientTimezone(clientTimezone);
246+
}
243247
// TODO as above, grant this privilege to Anonymous instead of using a proxy privilege
244248
Context.addProxyPrivilege(VIEW_LOCATIONS);
245249
Context.addProxyPrivilege(GET_LOCATIONS);
@@ -264,10 +268,17 @@ public String post(@RequestParam(value = "username", required = false) String us
264268
}
265269

266270
//If there is a single login location, default to that
271+
boolean clientTimezoneProperty = false;
272+
if (Context.isAuthenticated()) {
273+
clientTimezoneProperty = StringUtils.isBlank(Context.getAuthenticatedUser().getUserProperty("clientTimezone"));
274+
}
267275
if (sessionLocation == null) {
268276
List<Location> loginLocations = appFrameworkService.getLoginLocations();
269277
if (loginLocations.size() == 1) {
270-
sessionLocation = loginLocations.get(0);
278+
if (!ui.convertTimezones() || (ui.convertTimezones() && !clientTimezoneProperty)) {
279+
sessionLocation = loginLocations.get(0);
280+
}
281+
271282
}
272283
}
273284

omod/src/main/webapp/pages/login.gsp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@
107107
});
108108
</script>
109109

110+
<script type="text/javascript">
111+
jq(document).ready(function () {
112+
if(jq("#clientTimezone").length){
113+
jq("#clientTimezone").val(Intl.DateTimeFormat().resolvedOptions().timeZone)
114+
}
115+
});
116+
</script>
117+
110118
<div id="content" class="container-fluid">
111119
<div class= "row">
112120
<div class="col-12 col-sm-12 col-md-12 col-lg-12">
@@ -162,6 +170,9 @@
162170
<li id="${ui.encodeHtml(it.name)}" tabindex="0" value="${it.id}">${ui.encodeHtmlContent(ui.format(it))}</li>
163171
<% } %>
164172
</ul>
173+
<% if (ui.convertTimezones()) { %>
174+
<input type="hidden" id="clientTimezone" name="clientTimezone">
175+
<%} %>
165176
</p>
166177

167178
<input type="hidden" id="sessionLocationInput" name="sessionLocation"

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

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ public class LoginPageControllerTest {
9292

9393
private static final String VIEW_LOCATIONS = "View Locations";
9494

95-
private final UiUtils uiUtils = new UiUtils() {
96-
95+
private final UiUtils uiUtilsWithoutTimezones = new UiUtils() {
96+
@Override
97+
public boolean convertTimezones() {return false;}
98+
9799
@Override
98100
public String pageLink(String providerName, String pageName) {
99101
return new BasicUiUtils().pageLink(providerName, pageName);
@@ -104,6 +106,21 @@ public String message(String code, Object... args) {
104106
return null;
105107
}
106108
};
109+
110+
private final UiUtils uiUtilsWithTimezone = new UiUtils() {
111+
@Override
112+
public boolean convertTimezones() {return true;}
113+
114+
@Override
115+
public String pageLink(String providerName, String pageName) {
116+
return new BasicUiUtils().pageLink(providerName, pageName);
117+
}
118+
119+
@Override
120+
public String message(String code, Object... args) {
121+
return null;
122+
}
123+
};
107124

108125
private UiSessionContext sessionContext;
109126

@@ -114,6 +131,7 @@ public String message(String code, Object... args) {
114131
@Before
115132
public void setup() {
116133
mockStatic(Context.class);
134+
117135
locationService = mock(LocationService.class);
118136
sessionContext = mock(UiSessionContext.class);
119137
appFrameworkService = mock(AppFrameworkService.class);
@@ -169,8 +187,8 @@ public void get_shouldRedirectTheUserToTheHomePageIfTheyAreAlreadyAuthenticatedA
169187
throws Exception {
170188
when(Context.isAuthenticated()).thenReturn(true);
171189
when(Context.getUserContext()).thenReturn(mock(UserContext.class));
172-
String homeRedirect = "redirect:" + uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
173-
assertEquals(homeRedirect, new LoginPageController().get(null, uiUtils, createPageRequest(null, null), null, null,
190+
String homeRedirect = "redirect:" + uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
191+
assertEquals(homeRedirect, new LoginPageController().get(null, uiUtilsWithoutTimezones, createPageRequest(null, null), null, null,
174192
appFrameworkService, administrationService));
175193
}
176194

@@ -185,7 +203,7 @@ public void get_shouldRedirectTheUserToTheHomePageIfTheyAreAlreadyAuthenticatedA
185203
@Verifies(value = "should show the user the login page if they are not authenticated", method = "get(PageModel,UiUtils,PageRequest)")
186204
public void get_shouldShowTheUserTheLoginPageIfTheyAreNotAuthenticated() throws Exception {
187205
when(Context.isAuthenticated()).thenReturn(false);
188-
assertNull(new LoginPageController().get(new PageModel(), uiUtils, createPageRequest(null, null), null, null,
206+
assertNull(new LoginPageController().get(new PageModel(), uiUtilsWithoutTimezones, createPageRequest(null, null), null, null,
189207
appFrameworkService, administrationService));
190208
}
191209

@@ -207,7 +225,7 @@ public void get_shouldSetRedirectUrlInThePageModelIfOpenmrsRelatedUrlWasSpecifie
207225
request.addParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL, redirectUrl);
208226
PageModel pageModel = new PageModel();
209227

210-
new LoginPageController().get(pageModel, uiUtils, createPageRequest(request, null), null, null, appFrameworkService,
228+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, createPageRequest(request, null), null, null, appFrameworkService,
211229
administrationService);
212230

213231
assertEquals(redirectUrl, pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -231,7 +249,7 @@ public void get_shouldSetTheRefererAsTheRedirectUrlInThePageModelIfNoRedirectPar
231249
request.addHeader("Referer", refererUrl);
232250
PageModel pageModel = new PageModel();
233251

234-
new LoginPageController().get(pageModel, uiUtils, createPageRequest(request, null), null, null, appFrameworkService,
252+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, createPageRequest(request, null), null, null, appFrameworkService,
235253
administrationService);
236254

237255
assertEquals(refererUrl, pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -258,7 +276,7 @@ public void get_shouldSetRedirectUrlInThePageModelIfAnyWasSpecifiedInTheSession(
258276
request.setSession(httpSession);
259277

260278
PageModel pageModel = new PageModel();
261-
new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null, appFrameworkService,
279+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null, appFrameworkService,
262280
administrationService);
263281

264282
assertEquals(redirectUrl, pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -286,7 +304,7 @@ public void get_shouldRedirectUserToRequestedUrlIfAuthenticated() throws Excepti
286304

287305
PageModel pageModel = new PageModel();
288306

289-
assertEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null,
307+
assertEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null,
290308
appFrameworkService, administrationService));
291309
}
292310

@@ -313,7 +331,7 @@ public void get_shouldRedirectUserToHomeIfAuthenticated() throws Exception {
313331

314332
PageModel pageModel = new PageModel();
315333

316-
assertNotEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null,
334+
assertNotEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null,
317335
appFrameworkService, administrationService));
318336
}
319337

@@ -326,7 +344,7 @@ public void get_shouldRedirectUserToHomeIfAuthenticated() throws Exception {
326344
public void get_shouldNotSetRedirectUrlParamAfterManualLogout() throws Exception {
327345
when(Context.isAuthenticated()).thenReturn(false);
328346

329-
final String homeRedirect = "redirect:" + uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
347+
final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
330348

331349
MockHttpServletRequest request = new MockHttpServletRequest();
332350
request.addHeader("Referer", "somePage/weDont/wantTo/beRedirected");
@@ -337,7 +355,7 @@ public void get_shouldNotSetRedirectUrlParamAfterManualLogout() throws Exception
337355

338356
PageModel pageModel = new PageModel();
339357

340-
new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null, appFrameworkService,
358+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null, appFrameworkService,
341359
administrationService);
342360

343361
assertEquals("", pageModel.getAttribute(ReferenceApplicationWebConstants.REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -353,7 +371,7 @@ public void get_shouldNotSetRedirectUrlParamAfterManualLogout() throws Exception
353371
public void post_shouldRedirectNewUserToHome() throws Exception {
354372
setupMocksForSuccessfulAuthentication(true);
355373

356-
final String homeRedirect = "redirect:" + uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
374+
final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
357375
String redirectUrl = TEST_CONTEXT_PATH + "/referenceapplication/patient.page";
358376

359377
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -365,8 +383,8 @@ public void post_shouldRedirectNewUserToHome() throws Exception {
365383

366384
mockAuthenticatedUser();
367385

368-
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, locationService,
369-
administrationService, uiUtils, null, pageRequest, sessionContext));
386+
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, null, locationService,
387+
administrationService, uiUtilsWithoutTimezones, null, pageRequest, sessionContext));
370388
}
371389

372390
/**
@@ -389,7 +407,7 @@ public void post_shouldRedirectOldUserToRedirectUrl() throws Exception {
389407
mockAuthenticatedUser();
390408

391409
assertEquals("redirect:" + redirectUrl, new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID,
392-
locationService, administrationService, uiUtils, null, pageRequest, sessionContext));
410+
null, locationService, administrationService, uiUtilsWithoutTimezones, null, pageRequest, sessionContext));
393411
}
394412

395413
/**
@@ -401,17 +419,17 @@ public void post_shouldRedirectOldUserToRedirectUrl() throws Exception {
401419
public void post_shouldRedirectTheUserToTheHomePageIfTheRedirectUrlIsTheLoginPage() throws Exception {
402420
setupMocksForSuccessfulAuthentication(true);
403421

404-
final String redirectUrl = uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "login");
405-
final String homeRedirect = "redirect:" + uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
422+
final String redirectUrl = uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "login");
423+
final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
406424
MockHttpServletRequest request = new MockHttpServletRequest();
407425
request.setContextPath("/openmrs");
408426
request.setParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL, redirectUrl);
409427
PageRequest pageRequest = createPageRequest(request, null);
410428

411429
mockAuthenticatedUser();
412430

413-
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, locationService,
414-
administrationService, uiUtils, null, pageRequest, sessionContext));
431+
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, null, locationService,
432+
administrationService, uiUtilsWithoutTimezones, null, pageRequest, sessionContext));
415433

416434
}
417435

@@ -434,9 +452,9 @@ private void mockAuthenticatedUser() {
434452
public void post_shouldSendTheUserBackToTheLoginPageWhenAuthenticationFails() throws Exception {
435453
when(Context.isAuthenticated()).thenReturn(false);
436454
MockHttpServletRequest request = new MockHttpServletRequest();
437-
String page = new LoginPageController().post(null, null, SESSION_LOCATION_ID, locationService, administrationService,
438-
uiUtils, appFrameworkService, createPageRequest(request, null), sessionContext);
439-
assertEquals("redirect:" + uiUtils.pageLink("referenceapplication", "login"), page);
455+
String page = new LoginPageController().post(null, null, SESSION_LOCATION_ID, null, locationService, administrationService,
456+
uiUtilsWithoutTimezones, appFrameworkService, createPageRequest(request, null), sessionContext);
457+
assertEquals("redirect:" + uiUtilsWithoutTimezones.pageLink("referenceapplication", "login"), page);
440458
}
441459

442460
/**
@@ -448,9 +466,9 @@ public void post_shouldSendTheUserBackToTheLoginPageWhenAuthenticationFails() th
448466
public void post_shouldSendTheUserBackToTheLoginPageIfAnInvalidLocationIsSelected() throws Exception {
449467
setupMocksForSuccessfulAuthentication(false);
450468
MockHttpServletRequest request = new MockHttpServletRequest();
451-
String page = new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, locationService,
452-
administrationService, uiUtils, null, createPageRequest(request, null), sessionContext);
453-
assertEquals("redirect:" + uiUtils.pageLink("referenceapplication", "login"), page);
469+
String page = new LoginPageController().post(USERNAME, PASSWORD, SESSION_LOCATION_ID, null, locationService,
470+
administrationService, uiUtilsWithoutTimezones, null, createPageRequest(request, null), sessionContext);
471+
assertEquals("redirect:" + uiUtilsWithoutTimezones.pageLink("referenceapplication", "login"), page);
454472
}
455473

456474
/**
@@ -468,7 +486,7 @@ public void get_shouldNotSetTheRefererAsTheRedirectUrlInThePageModelIfRefererUrl
468486
request.setContextPath(TEST_CONTEXT_PATH);
469487
request.addHeader("Referer", refererUrl);
470488
PageModel pageModel = new PageModel();
471-
new LoginPageController().get(pageModel, uiUtils, createPageRequest(request, null), null, null, appFrameworkService,
489+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, createPageRequest(request, null), null, null, appFrameworkService,
472490
administrationService);
473491

474492
assertEquals("", pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -490,7 +508,7 @@ public void get_shouldSetTheRefererAsTheRedirectUrlInThePageModelIfRefererUrlIsW
490508
request.setContextPath(TEST_CONTEXT_PATH);
491509
request.addHeader("Referer", refererUrl);
492510
PageModel pageModel = new PageModel();
493-
new LoginPageController().get(pageModel, uiUtils, createPageRequest(request, null), null, null, appFrameworkService,
511+
new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, createPageRequest(request, null), null, null, appFrameworkService,
494512
administrationService);
495513

496514
assertEquals(redirectUrl, pageModel.get(REQUEST_PARAMETER_NAME_REDIRECT_URL));
@@ -521,7 +539,7 @@ public void get_shouldChooseRedirectUrlOverReferer() throws Exception {
521539

522540
PageModel pageModel = new PageModel();
523541

524-
assertEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtils, pageRequest, null, null,
542+
assertEquals("redirect:" + redirectUrl, new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, null,
525543
appFrameworkService, administrationService));
526544
}
527545

@@ -542,9 +560,9 @@ public void post_shouldSendUserToLocationSelectionPageIfTheyAreAssociatedWithMul
542560
final String expectedPage = "redirect:/openmrs/" + ReferenceApplicationConstants.MODULE_ID + "/login.page";
543561
ConversionService conversionService = mock(ConversionService.class);
544562
when(conversionService.convert(eq(true), eq(String.class))).thenReturn("true");
545-
Whitebox.setInternalState(uiUtils, "conversionService", conversionService);
546-
String page = new LoginPageController().post(USERNAME, PASSWORD, null, locationService, administrationService,
547-
uiUtils, appFrameworkService, createPageRequest(request, null), sessionContext);
563+
Whitebox.setInternalState(uiUtilsWithoutTimezones, "conversionService", conversionService);
564+
String page = new LoginPageController().post(USERNAME, PASSWORD, null, null, locationService, administrationService,
565+
uiUtilsWithoutTimezones, appFrameworkService, createPageRequest(request, null), sessionContext);
548566
assertEquals(expectedPage, page);
549567
}
550568

@@ -574,7 +592,7 @@ public void get_shouldScaleDownLoginLocationsToUserSpecificOnesInCaseMultipleLoc
574592
PageRequest pageRequest = createPageRequest(request, null);
575593
PageModel pageModel = new PageModel();
576594

577-
assertNull(new LoginPageController().get(pageModel, uiUtils, pageRequest, null, locationService, appFrameworkService,
595+
assertNull(new LoginPageController().get(pageModel, uiUtilsWithoutTimezones, pageRequest, null, locationService, appFrameworkService,
578596
administrationService));
579597
List<Location> locations = (List) pageModel.getAttribute("locations");
580598
assertEquals(2, locations.size());
@@ -587,7 +605,7 @@ public void post_shouldAutoSelectALocationAfterAuthenticationIfLoginLocationsSiz
587605
when(Context.isAuthenticated()).thenReturn(false).thenReturn(true);
588606
when(Context.getAuthenticatedUser()).thenReturn(mock(User.class));
589607
final int locationId = 1;
590-
final String homeRedirect = "redirect:" + uiUtils.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
608+
final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
591609
PageRequest pageRequest = createPageRequest(new MockHttpServletRequest(), null);
592610

593611
Location location = new Location(locationId);
@@ -596,10 +614,29 @@ public void post_shouldAutoSelectALocationAfterAuthenticationIfLoginLocationsSiz
596614
when(administrationService.getGlobalProperty(eq(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME)))
597615
.thenReturn("someValue");
598616

599-
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, null, locationService,
600-
administrationService, uiUtils, appFrameworkService, pageRequest, sessionContext));
617+
assertEquals(homeRedirect, new LoginPageController().post(USERNAME, PASSWORD, null, null, locationService,
618+
administrationService, uiUtilsWithoutTimezones, appFrameworkService, pageRequest, sessionContext));
601619

602620
verify(sessionContext, times(1)).setSessionLocation(eq(location));
603621
}
604-
622+
623+
@Test
624+
public void post_shouldNotAutoSelectALocationAfterAuthenticationIfLoginLocationsSizeIsOneAndUsingTimezonesWithoutClientTimezone() throws Exception {
625+
when(Context.isAuthenticated()).thenReturn(false).thenReturn(true);
626+
when(Context.getAuthenticatedUser()).thenReturn(mock(User.class));
627+
final int LOCATION_ID = 1;
628+
final String LOCATION_SELECTION_PAGE = "redirect:/openmrs/" + ReferenceApplicationConstants.MODULE_ID + "/login.page";
629+
PageRequest pageRequest = createPageRequest(new MockHttpServletRequest(), null);
630+
631+
Location location = new Location(LOCATION_ID);
632+
location.addTag(new LocationTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN, null));
633+
when(appFrameworkService.getLoginLocations()).thenReturn(Collections.singletonList(location));
634+
when(administrationService.getGlobalProperty(eq(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME)))
635+
.thenReturn("someValue");
636+
637+
assertEquals(LOCATION_SELECTION_PAGE, new LoginPageController().post(USERNAME, PASSWORD, null, null, locationService,
638+
administrationService, uiUtilsWithTimezone, appFrameworkService, pageRequest, sessionContext));
639+
640+
}
641+
605642
}

0 commit comments

Comments
 (0)