use of org.openmrs.Location in project openmrs-module-mirebalais by PIH.
the class LoginPageController method post.
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 ContextDAO contextDao, @SpringBean("locationService") LocationService locationService, UiUtils ui, EmrContext context, PageRequest request) {
HttpSession httpSession = request.getRequest().getSession();
Location sessionLocation = null;
try {
// TODO as above, grant this privilege to Anonymous instead of using a proxy privilege
Context.addProxyPrivilege(PrivilegeConstants.GET_LOCATIONS);
if (sessionLocationId != null) {
sessionLocation = locationService.getLocation(sessionLocationId);
if (!sessionLocation.hasTag(EmrApiConstants.LOCATION_TAG_SUPPORTS_LOGIN)) {
// the UI shouldn't allow this, but protect against it just in case
httpSession.setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("mirebalais.login.error.invalidLocation"));
return "redirect:" + ui.pageLink("mirebalais", "login");
}
}
if (sessionLocation == null) {
httpSession.setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("mirebalais.login.error.locationRequired"));
return "redirect:" + ui.pageLink("mirebalais", "login");
}
// Set a cookie, so next time someone logs in on this machine, we can default to that same location
request.setCookieValue(EmrConstants.COOKIE_NAME_LAST_SESSION_LOCATION, sessionLocationId.toString());
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_LOCATIONS);
}
try {
context.getUserContext().authenticate(username, password, contextDao);
context.setSessionLocation(sessionLocation);
// set the locale based on the user's default locale
Locale userLocale = GeneralUtils.getDefaultLocale(context.getUserContext().getAuthenticatedUser());
if (userLocale != null) {
context.getUserContext().setLocale(userLocale);
// these have been taken from the core login servlet, not sure if they are necessary
request.getResponse().setLocale(userLocale);
new CookieLocaleResolver().setDefaultLocale(userLocale);
}
return "redirect:" + ui.pageLink("mirebalais", "home");
} catch (ContextAuthenticationException ex) {
httpSession.setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("mirebalais.login.error.authentication"));
return "redirect:" + ui.pageLink("mirebalais", "login");
}
}
use of org.openmrs.Location in project openmrs-module-mirebalais by PIH.
the class RequireUtilTest method shouldReturnTrueIfLocationHasTag.
@Test
public void shouldReturnTrueIfLocationHasTag() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", LocationTags.ADMISSION_LOCATION.name());
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(sessionLocationHasTag(LocationTags.ADMISSION_LOCATION)), appContextModel), is(true));
}
use of org.openmrs.Location in project openmrs-module-mirebalais by PIH.
the class RequireUtilTest method shouldReturnFalseIfLocationDoesNotHaveTag.
@Test
public void shouldReturnFalseIfLocationDoesNotHaveTag() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", LocationTags.ADMISSION_LOCATION.name());
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(sessionLocationHasTag(LocationTags.ARCHIVES_LOCATION)), appContextModel), is(false));
}
use of org.openmrs.Location in project openmrs-module-mirebalais by PIH.
the class RequireUtilTest method shouldReturnFalseIfNotProperLocationTagAndUserHasRetroPrivilege.
@Test
public void shouldReturnFalseIfNotProperLocationTagAndUserHasRetroPrivilege() {
user.addRole(admin);
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", LocationTags.ADMISSION_LOCATION.name());
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitContextModel visit = mock(VisitContextModel.class);
when(visit.getStopDatetimeInMilliseconds()).thenReturn(new DateTime(2014, 1, 1, 1, 1).getMillis());
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
Config config = mock(Config.class);
when(config.isComponentEnabled("visitNote")).thenReturn(false);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag(LocationTags.CONSULT_NOTE_LOCATION), or(and(userHasPrivilege(Privileges.TASK_EMR_ENTER_CONSULT_NOTE), patientHasActiveVisit()), userHasPrivilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE), and(userHasPrivilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE_THIS_PROVIDER_ONLY), patientVisitWithinPastThirtyDays(config))))), appContextModel), is(false));
}
use of org.openmrs.Location in project openmrs-module-mirebalais by PIH.
the class RequireUtilTest method shouldReturnFalseIfImproperLocationTagEventThoughUserHasConsultPrivilegeAndActiveVisit.
@Test
public void shouldReturnFalseIfImproperLocationTagEventThoughUserHasConsultPrivilegeAndActiveVisit() {
user.addRole(doctor);
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", LocationTags.CHECKIN_LOCATION.name());
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitContextModel visit = mock(VisitContextModel.class);
when(visit.getStopDatetimeInMilliseconds()).thenReturn(null);
when(visit.isActive()).thenReturn(true);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
Config config = mock(Config.class);
when(config.isComponentEnabled("visitNote")).thenReturn(false);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag(LocationTags.CONSULT_NOTE_LOCATION), or(and(userHasPrivilege(Privileges.TASK_EMR_ENTER_CONSULT_NOTE), patientHasActiveVisit()), userHasPrivilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE), and(userHasPrivilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE_THIS_PROVIDER_ONLY), patientVisitWithinPastThirtyDays(config))))), appContextModel), is(false));
}
Aggregations