if (isAuthenticated() && (getAuthenticatedUser().hasPrivilege(privilege) || getAuthenticatedRole().hasPrivilege(privilege))) { notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true); return true; notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true); return true; if (getAnonymousRole().hasPrivilege(privilege)) { notifyPrivilegeListeners(getAuthenticatedUser(), privilege, true); return true; notifyPrivilegeListeners(getAuthenticatedUser(), privilege, false); return false;
userContext = new UserContext(); httpSession.setAttribute(WebConstants.OPENMRS_USER_CONTEXT_HTTPSESSION_ATTR, userContext); User user = userContext.getAuthenticatedUser(); if (user != null) { httpSession.setAttribute("username", user.getUsername()); httpSession.setAttribute("locale", userContext.getLocale());
/** * Convenience method. Passes through to userContext.hasPrivilege(String) * * @should give daemon user full privileges */ public static boolean hasPrivilege(String privilege) { // the daemon threads have access to all things if (Daemon.isDaemonThread()) { return true; } return getUserContext().hasPrivilege(privilege); }
/** * Gets all the roles for the (un)authenticated user. Anonymous and Authenticated roles are * appended if necessary * * @return all expanded roles for a user * @throws Exception */ public Set<Role> getAllRoles() throws Exception { return getAllRoles(getAuthenticatedUser()); }
/** * Gets all the roles for a user. Anonymous and Authenticated roles are appended if necessary * * @param user * @return all expanded roles for a user * @should not fail with null user * @should add anonymous role to all users * @should add authenticated role to all authenticated users * @should return same roles as user getAllRoles method */ public Set<Role> getAllRoles(User user) throws Exception { Set<Role> roles = new HashSet<>(); // add the Anonymous Role roles.add(getAnonymousRole()); // add the Authenticated role if (user != null && getAuthenticatedUser() != null && getAuthenticatedUser().equals(user)) { roles.addAll(user.getAllRoles()); roles.add(getAuthenticatedRole()); } return roles; }
public void authenticateMockUser() { User user = new User(); user.setUuid("1010d442-e134-11de-babe-001e378eb67e"); user.setUserId(1); user.setUsername("admin"); user.addRole(new Role(RoleConstants.SUPERUSER)); Person person = new Person(); person.setUuid("6adb7c42-cfd2-4301-b53b-ff17c5654ff7"); person.setId(1); person.addName(new PersonName("Bob", "", "Smith")); Calendar calendar = Calendar.getInstance(); calendar.set(1980, 01, 01); person.setBirthdate(calendar.getTime()); person.setGender("M"); user.setPerson(person); when(userContext.getAuthenticatedUser()).thenReturn(user); when(userContext.isAuthenticated()).thenReturn(true); }
/** * @return "active" user who has been authenticated, otherwise <code>null</code> */ public static User getAuthenticatedUser() { if (Daemon.isDaemonThread()) { return Daemon.getDaemonThreadUser(); } return getUserContext().getAuthenticatedUser(); }
/** * logs out the "active" (authenticated) user within context * * @see #authenticate * @should not fail if session hasn't been opened yet */ public static void logout() { if (!isSessionOpen()) { return; // fail early if there isn't even a session open } log.debug("Logging out : {}", getAuthenticatedUser()); getUserContext().logout(); // reset the UserContext object (usually cleared out by closeSession() // soon after this) setUserContext(new UserContext()); }
/** * Convenience method. Passes through to {@link UserContext#setLocale(Locale)} */ public static void setLocale(Locale locale) { getUserContext().setLocale(locale); }
/** * Used to define a unit of work. All "units of work" should be surrounded by openSession and * closeSession calls. */ public static void openSession() { log.trace("opening session"); setUserContext(new UserContext()); // must be cleared out in // closeSession() getContextDAO().openSession(); }
/** * Convenience method. Passes through to {@link UserContext#getLocale()} * * @should not fail if session hasn't been opened */ public static Locale getLocale() { // if a session hasn't been opened, just fetch the default if (!isSessionOpen()) { return LocaleUtility.getDefaultLocale(); } return getUserContext().getLocale(); }
@Test(expected = APIAuthenticationException.class) public void before_shouldThrowAPIAuthenticationException() { Context.getUserContext().logout(); Context.getConceptService().getConcept(3); }
person.setGender("M"); user.setPerson(person); when(userContext.getAuthenticatedUser()).thenReturn(user); when(userContext.isAuthenticated()).thenReturn(true);
testStr = HtmlFormEntryUtil.getTestStr(includeStr.substring(includeOrExcludeTestIndex)); testRoles = testStr.split(","); User currentUser = Context.getUserContext().getAuthenticatedUser(); try { Set<Role> assignedRoles = currentUser.getAllRoles(); testStr = HtmlFormEntryUtil.getTestStr(includeStr.substring(includeOrExcludeTestIndex)) ; testRoles = testStr.split(","); User currentUser = Context.getUserContext().getAuthenticatedUser(); try { Set<Role> assignedRoles = currentUser.getAllRoles();
Context.getUserContext().setLocale(new Locale("en", "US"));
@Test public void processDeath_shouldMapValuesAndSavePatient() throws Exception { // given final Date dateDied = new Date(); final Concept causeOfDeath = new Concept(2); when(conceptService.getConcept(anyString())).thenReturn(new Concept()); when(locationService.getDefaultLocation()).thenReturn(new Location()); UserContext userContext = mock(UserContext.class); this.contextMockHelper.setUserContext(userContext); when(userContext.hasPrivilege(anyString())).thenReturn(true); ArgumentCaptor<Patient> argumentCaptor = ArgumentCaptor.forClass(Patient.class); when(patientDaoMock.savePatient(argumentCaptor.capture())).thenReturn(new Patient()); // when final Patient patient = new Patient(); patient.addIdentifier(new PatientIdentifier("an identifier", new PatientIdentifierType(1234), new Location())); patientService.processDeath(patient, dateDied, causeOfDeath, "unknown"); // then final Patient savedPatient = argumentCaptor.getValue(); assertEquals(true, savedPatient.getDead()); assertEquals(dateDied, savedPatient.getDeathDate()); assertEquals(causeOfDeath, savedPatient.getCauseOfDeath()); }