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");
}
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);
}
Aggregations