Skip to content

Commit 8d605a5

Browse files
suthagar23dkayiwa
authored andcommitted
RA-1516 Added support to select the location from the userProperty in the Login Screen (#47)
RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen RA-1516 Added support to select the location from the userProperty in the Login Screen Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes Indention changes LBAC-13 Added implementation to create RefApp location glopal property Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes Added changes
1 parent 5c6df6e commit 8d605a5

4 files changed

Lines changed: 122 additions & 60 deletions

File tree

api/src/main/java/org/openmrs/module/referenceapplication/ReferenceApplicationConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public final class ReferenceApplicationConstants {
2323

2424
public static final long PROCESS_HL7_TASK_INTERVAL = 5L;
2525

26+
public static final String LOCATION_USER_PROPERTY_NAME = "referenceapplication.locationUserPropertyName";
27+
2628
}

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

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import javax.servlet.http.HttpServletRequest;
4343
import java.net.MalformedURLException;
4444
import java.net.URL;
45+
import org.openmrs.api.AdministrationService;
46+
import java.util.HashMap;
47+
import java.util.Map;
4548
import java.util.Locale;
4649

4750
import static org.openmrs.module.referenceapplication.ReferenceApplicationWebConstants.COOKIE_NAME_LAST_SESSION_LOCATION;
@@ -82,7 +85,8 @@ public String get(PageModel model,
8285
PageRequest pageRequest,
8386
@CookieValue(value = COOKIE_NAME_LAST_SESSION_LOCATION, required = false) String lastSessionLocationId,
8487
@SpringBean("locationService") LocationService locationService,
85-
@SpringBean("appFrameworkService") AppFrameworkService appFrameworkService) {
88+
@SpringBean("appFrameworkService") AppFrameworkService appFrameworkService,
89+
@SpringBean("adminService") AdministrationService administrationService) {
8690

8791
String redirectUrl = getRedirectUrl(pageRequest);
8892

@@ -110,11 +114,26 @@ public String get(PageModel model,
110114
Context.removeProxyPrivilege(GET_LOCATIONS);
111115
}
112116

117+
Boolean isLocationUserPropertyAvailable = isLocationUserPropertyAvailable(administrationService);
118+
Object showLocation = pageRequest.getAttribute("showSessionLocations");
119+
if(showLocation != null && showLocation.toString().equals("true")) {
120+
// if the request contains a attribute as showSessionLocations, then ignore isLocationUserPropertyAvailable
121+
isLocationUserPropertyAvailable = false;
122+
}
123+
model.addAttribute("showSessionLocations", !isLocationUserPropertyAvailable);
113124
model.addAttribute("lastSessionLocation", lastSessionLocation);
114125

115126
return null;
116127
}
117128

129+
private boolean isLocationUserPropertyAvailable(AdministrationService administrationService) {
130+
String locationUserPropertyName = administrationService.getGlobalProperty(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME);
131+
if(StringUtils.isNotBlank(locationUserPropertyName)) {
132+
return true;
133+
}
134+
return false;
135+
}
136+
118137
private boolean isUrlWithinOpenmrs(PageRequest pageRequest, String redirectUrl){
119138
if (StringUtils.isNotBlank(redirectUrl)) {
120139
if (redirectUrl.startsWith("http://") || redirectUrl.startsWith("https://")) {
@@ -187,8 +206,10 @@ private String getRedirectUrl(PageRequest pageRequest) {
187206
public String post(@RequestParam(value = "username", required = false) String username,
188207
@RequestParam(value = "password", required = false) String password,
189208
@RequestParam(value = "sessionLocation", required = false) Integer sessionLocationId,
190-
@SpringBean("locationService") LocationService locationService, UiUtils ui, PageRequest pageRequest,
191-
UiSessionContext sessionContext) {
209+
@SpringBean("locationService") LocationService locationService,
210+
@SpringBean("adminService") AdministrationService administrationService,
211+
UiUtils ui, PageRequest pageRequest,
212+
UiSessionContext sessionContext) {
192213

193214
String redirectUrl = pageRequest.getRequest().getParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL);
194215
redirectUrl = getRelativeUrl(redirectUrl, pageRequest);
@@ -206,24 +227,43 @@ public String post(@RequestParam(value = "username", required = false) String us
206227
}
207228
}
208229

209-
//TODO uncomment this to replace the if clause after it
210-
if (sessionLocation != null && sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
211-
// Set a cookie, so next time someone logs in on this machine, we can default to that same location
212-
Cookie cookie = new Cookie(COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());
213-
cookie.setHttpOnly(true);
214-
pageRequest.getResponse().addCookie(cookie);
230+
try {
231+
Context.authenticate(username, password);
232+
String locationUserPropertyName = administrationService.getGlobalProperty(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME);
233+
if (StringUtils.isNotBlank(locationUserPropertyName)) {
234+
if (Context.isAuthenticated() && Context.getUserContext().getAuthenticatedUser() != null) {
235+
String locationUuid = Context.getUserContext().getAuthenticatedUser().getUserProperty(locationUserPropertyName);
236+
if (StringUtils.isNotBlank(locationUuid)) {
237+
sessionLocation = locationService.getLocationByUuid(locationUuid);
238+
}
239+
if (sessionLocation != null) {
240+
sessionLocationId = sessionLocation.getLocationId();
241+
}
242+
else {
243+
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
244+
ui.message("referenceapplication.login.error.locationRequired"));
245+
// Since the user is already authenticated without location, need to logout before redirecting
246+
Context.logout();
247+
Map<String, Object> returnParameters = new HashMap<String, Object>();
248+
returnParameters.put("showSessionLocations", true);
249+
return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "login", returnParameters);
250+
}
251+
}
252+
}
253+
215254

216-
try {
217-
Context.authenticate(username, password);
218255

256+
if (sessionLocation != null && sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
257+
// Set a cookie, so next time someone logs in on this machine, we can default to that same location
258+
Cookie cookie = new Cookie(COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());
259+
cookie.setHttpOnly(true);
260+
pageRequest.getResponse().addCookie(cookie);
219261
if (Context.isAuthenticated()) {
220262
if (log.isDebugEnabled())
221263
log.debug("User has successfully authenticated");
222-
223264
CurrentUsers.addUser(pageRequest.getRequest().getSession(), Context.getAuthenticatedUser());
224265

225266
sessionContext.setSessionLocation(sessionLocation);
226-
227267
//we set the username value to check it new or old user is trying to log in
228268
cookie = new Cookie(ReferenceApplicationWebConstants.COOKIE_NAME_LAST_USER, String.valueOf(username.hashCode()));
229269
cookie.setHttpOnly(true);
@@ -253,22 +293,21 @@ public String post(@RequestParam(value = "username", required = false) String us
253293

254294
return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
255295
}
256-
}
257-
catch (ContextAuthenticationException ex) {
258-
if (log.isDebugEnabled())
259-
log.debug("Failed to authenticate user");
260-
296+
} else if (sessionLocation == null) {
261297
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
262-
ui.message(ReferenceApplicationConstants.MODULE_ID + ".error.login.fail"));
298+
ui.message("referenceapplication.login.error.locationRequired"));
299+
} else {
300+
// the UI shouldn't allow this, but protect against it just in case
301+
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
302+
ui.message("referenceapplication.login.error.invalidLocation", sessionLocation.getName()));
263303
}
304+
}
305+
catch (ContextAuthenticationException ex) {
306+
if (log.isDebugEnabled())
307+
log.debug("Failed to authenticate user");
264308

265-
} else if (sessionLocation == null) {
266309
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
267-
ui.message("referenceapplication.login.error.locationRequired"));
268-
} else {
269-
// the UI shouldn't allow this, but protect against it just in case
270-
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
271-
ui.message("referenceapplication.login.error.invalidLocation", sessionLocation.getName()));
310+
ui.message(ReferenceApplicationConstants.MODULE_ID + ".error.login.fail"));
272311
}
273312

274313
if (log.isDebugEnabled())
@@ -277,7 +316,8 @@ public String post(@RequestParam(value = "username", required = false) String us
277316
//TODO limit login attempts by IP Address
278317

279318
pageRequest.getSession().setAttribute(SESSION_ATTRIBUTE_REDIRECT_URL, redirectUrl);
280-
319+
// Since the user is already authenticated without location, need to logout before redirecting
320+
Context.logout();
281321
return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "login");
282322
}
283323

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
2121

22+
<% if(showSessionLocations) { %>
2223
<script type="text/javascript">
2324
jQuery(function() {
2425
updateSelectedOption = function() {
@@ -78,8 +79,13 @@ ${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
7879
jQuery('#sessionLocationError').show();
7980
e.preventDefault();
8081
}
81-
});
82-
82+
});
83+
});
84+
</script>
85+
<% } %>
86+
87+
<script type="text/javascript">
88+
jQuery(function() {
8389
var cannotLoginController = emr.setupConfirmationDialog({
8490
selector: '#cannotLoginPopup',
8591
actions: {
@@ -130,6 +136,7 @@ ${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
130136
<input id="password" type="password" name="password" placeholder="${ ui.message("referenceapplication.login.password.placeholder") }"/>
131137
</p>
132138

139+
<% if(showSessionLocations) { %>
133140
<p class="clear">
134141
<label for="sessionLocation">
135142
${ ui.message("referenceapplication.login.sessionLocation") }: <span class="location-error" id="sessionLocationError" style="display: none">${ui.message("referenceapplication.login.error.locationRequired")}</span>
@@ -145,6 +152,7 @@ ${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
145152
<% if (lastSessionLocation != null) { %> value="${lastSessionLocation.id}" <% } %> />
146153

147154
<p></p>
155+
<% } %>
148156
<p>
149157
<input id="loginButton" class="confirm" type="submit" value="${ ui.message("referenceapplication.login.button") }"/>
150158
</p>

0 commit comments

Comments
 (0)