Search in sources :

Example 1 with UserContext

use of org.openmrs.api.context.UserContext in project openmrs-core by openmrs.

the class OpenmrsFilter method doFilterInternal.

/**
 * This method is called for every request for a page/image/javascript file/etc The main point
 * of this is to make sure the user's current userContext is on the session and on the current
 * thread
 *
 * @see org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
 */
@Override
protected void doFilterInternal(HttpServletRequest httpRequest, HttpServletResponse httpResponse, FilterChain chain) throws ServletException, IOException {
    HttpSession httpSession = httpRequest.getSession();
    // used by htmlInclude tag
    httpRequest.setAttribute(WebConstants.INIT_REQ_UNIQUE_ID, String.valueOf(System.currentTimeMillis()));
    if (log.isDebugEnabled()) {
        log.debug("requestURI " + httpRequest.getRequestURI());
        log.debug("requestURL " + httpRequest.getRequestURL());
        log.debug("request path info " + httpRequest.getPathInfo());
    }
    // User context is created if it doesn't already exist and added to the session
    // note: this usercontext storage logic is copied to webinf/view/uncaughtexception.jsp to
    // prevent stack traces being shown to non-authenticated users
    UserContext userContext = (UserContext) httpSession.getAttribute(WebConstants.OPENMRS_USER_CONTEXT_HTTPSESSION_ATTR);
    // default the session username attribute to anonymous
    httpSession.setAttribute("username", "-anonymous user-");
    // and set it onto the session
    if (userContext == null) {
        userContext = new UserContext();
        httpSession.setAttribute(WebConstants.OPENMRS_USER_CONTEXT_HTTPSESSION_ATTR, userContext);
        if (log.isDebugEnabled()) {
            log.debug("Just set user context " + userContext + " as attribute on session");
        }
    } else {
        // set username as attribute on session so parent servlet container
        // can identify sessions easier
        User user = userContext.getAuthenticatedUser();
        if (user != null) {
            httpSession.setAttribute("username", user.getUsername());
        }
    }
    // set the locale on the session (for the servlet container as well)
    httpSession.setAttribute("locale", userContext.getLocale());
    // Add the user context to the current thread
    Context.setUserContext(userContext);
    Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());
    log.debug("before chain.Filter");
    // continue the filter chain (going on to spring, authorization, etc)
    try {
        chain.doFilter(httpRequest, httpResponse);
    } finally {
        Context.clearUserContext();
    }
    log.debug("after chain.doFilter");
}
Also used : User(org.openmrs.User) HttpSession(javax.servlet.http.HttpSession) UserContext(org.openmrs.api.context.UserContext)

Example 2 with UserContext

use of org.openmrs.api.context.UserContext in project openmrs-module-mirebalais by PIH.

the class RequireUtilTest method setup.

@Before
public void setup() throws Exception {
    appFrameworkService = new AppFrameworkServiceImpl(null, null, null, null, null, null, null, null);
    doctor = new Role("Doctor");
    admin = new Role("Admin");
    Privilege enterConsultNote = new Privilege(Privileges.TASK_EMR_ENTER_CONSULT_NOTE.privilege());
    enterConsultNote.setUuid(Privileges.TASK_EMR_ENTER_CONSULT_NOTE.uuid());
    enterConsultNote.setDescription(Privileges.TASK_EMR_ENTER_CONSULT_NOTE.description());
    Privilege retroClinicalNote = new Privilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE.privilege());
    retroClinicalNote.setUuid(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE.uuid());
    retroClinicalNote.setDescription(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE.description());
    Privilege retroClinicalNoteThisProviderOnly = new Privilege(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE_THIS_PROVIDER_ONLY.privilege());
    retroClinicalNoteThisProviderOnly.setUuid(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE_THIS_PROVIDER_ONLY.uuid());
    retroClinicalNoteThisProviderOnly.setDescription(Privileges.TASK_EMR_RETRO_CLINICAL_NOTE_THIS_PROVIDER_ONLY.description());
    doctor.addPrivilege(enterConsultNote);
    doctor.addPrivilege(retroClinicalNoteThisProviderOnly);
    admin.addPrivilege(enterConsultNote);
    admin.addPrivilege(retroClinicalNote);
    user = new User();
    user.setUsername("bobMeIn");
    user.setUuid("123-456");
    user.setSystemId("abc");
    user.setRetired(true);
    userContext = mock(UserContext.class);
    when(userContext.getAuthenticatedUser()).thenReturn(user);
    uiSessionContext = new UiSessionContext();
    uiSessionContext.setUserContext(userContext);
}
Also used : Role(org.openmrs.Role) UiSessionContext(org.openmrs.module.appui.UiSessionContext) User(org.openmrs.User) UserContext(org.openmrs.api.context.UserContext) AppFrameworkServiceImpl(org.openmrs.module.appframework.service.AppFrameworkServiceImpl) Privilege(org.openmrs.Privilege) RequireUtil.userHasPrivilege(org.openmrs.module.mirebalais.require.RequireUtil.userHasPrivilege) Before(org.junit.Before)

Aggregations

User (org.openmrs.User)2 UserContext (org.openmrs.api.context.UserContext)2 HttpSession (javax.servlet.http.HttpSession)1 Before (org.junit.Before)1 Privilege (org.openmrs.Privilege)1 Role (org.openmrs.Role)1 AppFrameworkServiceImpl (org.openmrs.module.appframework.service.AppFrameworkServiceImpl)1 UiSessionContext (org.openmrs.module.appui.UiSessionContext)1 RequireUtil.userHasPrivilege (org.openmrs.module.mirebalais.require.RequireUtil.userHasPrivilege)1