Search in sources :

Example 66 with Location

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");
    }
}
Also used : Locale(java.util.Locale) ContextAuthenticationException(org.openmrs.api.context.ContextAuthenticationException) CookieLocaleResolver(org.springframework.web.servlet.i18n.CookieLocaleResolver) HttpSession(javax.servlet.http.HttpSession) Location(org.openmrs.Location)

Example 67 with Location

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 68 with Location

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 69 with Location

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) Config(org.openmrs.module.pihcore.config.Config) DateTime(org.joda.time.DateTime) Location(org.openmrs.Location) VisitContextModel(org.openmrs.module.coreapps.contextmodel.VisitContextModel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 70 with Location

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) Config(org.openmrs.module.pihcore.config.Config) Location(org.openmrs.Location) VisitContextModel(org.openmrs.module.coreapps.contextmodel.VisitContextModel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Location (org.openmrs.Location)235 Test (org.junit.Test)161 Date (java.util.Date)80 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)74 Patient (org.openmrs.Patient)66 Encounter (org.openmrs.Encounter)38 SimpleObject (org.openmrs.ui.framework.SimpleObject)32 PatientIdentifierType (org.openmrs.PatientIdentifierType)31 VisitDomainWrapper (org.openmrs.module.emrapi.visit.VisitDomainWrapper)31 Visit (org.openmrs.Visit)28 AppContextModel (org.openmrs.module.appframework.context.AppContextModel)28 Obs (org.openmrs.Obs)27 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)27 PatientIdentifier (org.openmrs.PatientIdentifier)26 Concept (org.openmrs.Concept)19 EncounterType (org.openmrs.EncounterType)18 DateTime (org.joda.time.DateTime)17 ArrayList (java.util.ArrayList)15 VisitContextModel (org.openmrs.module.coreapps.contextmodel.VisitContextModel)15 BindException (org.springframework.validation.BindException)15