Skip to content

Commit

Permalink
RA-1516 Added support to select the location from the userProperty in…
Browse files Browse the repository at this point in the history
… 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

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
  • Loading branch information
Suthagar23 committed Jul 27, 2018
1 parent 5c6df6e commit 4475672
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 60 deletions.
Expand Up @@ -23,4 +23,6 @@ public final class ReferenceApplicationConstants {

public static final long PROCESS_HL7_TASK_INTERVAL = 5L;

public static final String LOCATION_USER_PROPERTY_NAME = "referenceapplication.locationUserPropertyName";

}
Expand Up @@ -42,6 +42,9 @@
import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openmrs.api.AdministrationService;
import java.util.HashMap;
import java.util.Map;
import java.util.Locale;

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

String redirectUrl = getRedirectUrl(pageRequest);

Expand Down Expand Up @@ -110,11 +114,24 @@ public String get(PageModel model,
Context.removeProxyPrivilege(GET_LOCATIONS);
}

model.addAttribute("showSessionLocations", !isLocationUserPropertyAvailable(administrationService));
Object showLocation = pageRequest.getAttribute("showSessionLocations");
if(showLocation != null && showLocation.toString().equals("true")) {
model.addAttribute("showSessionLocations", true);
}
model.addAttribute("lastSessionLocation", lastSessionLocation);

return null;
}

private boolean isLocationUserPropertyAvailable(AdministrationService administrationService) {
String locationUserPropertyName = administrationService.getGlobalProperty(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME);
if(StringUtils.isNotBlank(locationUserPropertyName) && !locationUserPropertyName.equals("false")) {
return true;
}
return false;
}

private boolean isUrlWithinOpenmrs(PageRequest pageRequest, String redirectUrl){
if (StringUtils.isNotBlank(redirectUrl)) {
if (redirectUrl.startsWith("http://") || redirectUrl.startsWith("https://")) {
Expand Down Expand Up @@ -187,8 +204,10 @@ private String getRedirectUrl(PageRequest pageRequest) {
public String post(@RequestParam(value = "username", required = false) String username,
@RequestParam(value = "password", required = false) String password,
@RequestParam(value = "sessionLocation", required = false) Integer sessionLocationId,
@SpringBean("locationService") LocationService locationService, UiUtils ui, PageRequest pageRequest,
UiSessionContext sessionContext) {
@SpringBean("locationService") LocationService locationService,
@SpringBean("adminService") AdministrationService administrationService,
UiUtils ui, PageRequest pageRequest,
UiSessionContext sessionContext) {

String redirectUrl = pageRequest.getRequest().getParameter(REQUEST_PARAMETER_NAME_REDIRECT_URL);
redirectUrl = getRelativeUrl(redirectUrl, pageRequest);
Expand All @@ -206,24 +225,43 @@ public String post(@RequestParam(value = "username", required = false) String us
}
}

//TODO uncomment this to replace the if clause after it
if (sessionLocation != null && sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
// Set a cookie, so next time someone logs in on this machine, we can default to that same location
Cookie cookie = new Cookie(COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());
cookie.setHttpOnly(true);
pageRequest.getResponse().addCookie(cookie);
try {
Context.authenticate(username, password);
String locationUserPropertyName = administrationService.getGlobalProperty(ReferenceApplicationConstants.LOCATION_USER_PROPERTY_NAME);
if (StringUtils.isNotBlank(locationUserPropertyName)) {
if (Context.isAuthenticated() && Context.getUserContext().getAuthenticatedUser() != null) {
String locationUuid = Context.getUserContext().getAuthenticatedUser().getUserProperty(locationUserPropertyName);
if (StringUtils.isNotBlank(locationUuid)) {
sessionLocation = locationService.getLocationByUuid(locationUuid);
sessionLocationId = sessionLocation.getLocationId();
}
else {
if(sessionLocationId == null ) {
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
ui.message("referenceapplication.login.error.locationRequired"));
// Since the user is already authenticated without location, need to logout before redirecting
Context.logout();
Map<String, Object> returnParameters = new HashMap<String, Object>();
returnParameters.put("showSessionLocations", true);
return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "login", returnParameters);
}
}
}
}

try {
Context.authenticate(username, password);


if (sessionLocation != null && sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
// Set a cookie, so next time someone logs in on this machine, we can default to that same location
Cookie cookie = new Cookie(COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());
cookie.setHttpOnly(true);
pageRequest.getResponse().addCookie(cookie);
if (Context.isAuthenticated()) {
if (log.isDebugEnabled())
log.debug("User has successfully authenticated");

CurrentUsers.addUser(pageRequest.getRequest().getSession(), Context.getAuthenticatedUser());

sessionContext.setSessionLocation(sessionLocation);

//we set the username value to check it new or old user is trying to log in
cookie = new Cookie(ReferenceApplicationWebConstants.COOKIE_NAME_LAST_USER, String.valueOf(username.hashCode()));
cookie.setHttpOnly(true);
Expand Down Expand Up @@ -253,22 +291,21 @@ public String post(@RequestParam(value = "username", required = false) String us

return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "home");
}
}
catch (ContextAuthenticationException ex) {
if (log.isDebugEnabled())
log.debug("Failed to authenticate user");

} else if (sessionLocation == null) {
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
ui.message(ReferenceApplicationConstants.MODULE_ID + ".error.login.fail"));
ui.message("referenceapplication.login.error.locationRequired"));
} else {
// the UI shouldn't allow this, but protect against it just in case
pageRequest.getSession().setAttribute(ReferenceApplicationWebConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE,
ui.message("referenceapplication.login.error.invalidLocation", sessionLocation.getName()));
}
}
catch (ContextAuthenticationException ex) {
if (log.isDebugEnabled())
log.debug("Failed to authenticate user");

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

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

pageRequest.getSession().setAttribute(SESSION_ATTRIBUTE_REDIRECT_URL, redirectUrl);

// Since the user is already authenticated without location, need to logout before redirecting
Context.logout();
return "redirect:" + ui.pageLink(ReferenceApplicationConstants.MODULE_ID, "login");
}

Expand Down
12 changes: 10 additions & 2 deletions omod/src/main/webapp/pages/login.gsp
Expand Up @@ -19,6 +19,7 @@

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

<% if(showSessionLocations) { %>
<script type="text/javascript">
jQuery(function() {
updateSelectedOption = function() {
Expand Down Expand Up @@ -78,8 +79,13 @@ ${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
jQuery('#sessionLocationError').show();
e.preventDefault();
}
});
});
});
</script>
<% } %>

<script type="text/javascript">
jQuery(function() {
var cannotLoginController = emr.setupConfirmationDialog({
selector: '#cannotLoginPopup',
actions: {
Expand Down Expand Up @@ -130,6 +136,7 @@ ${ ui.includeFragment("referenceapplication", "infoAndErrorMessages") }
<input id="password" type="password" name="password" placeholder="${ ui.message("referenceapplication.login.password.placeholder") }"/>
</p>

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

<p></p>
<% } %>
<p>
<input id="loginButton" class="confirm" type="submit" value="${ ui.message("referenceapplication.login.button") }"/>
</p>
Expand Down

0 comments on commit 4475672

Please sign in to comment.