/** * Send an Alert to all super users that modules did not start due to cyclic dependencies */ private static void notifySuperUsersAboutCyclicDependencies(Exception ex) { try { Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); Context.getAlertService().notifySuperUsers("Module.error.cyclicDependencies", ex, ex.getMessage()); } catch (Exception e) { log.error("Unable to send an alert to the super users", e); } finally { Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); } }
/** * Start the scheduler given the following start up properties. * * @param p properties used to start the service */ public static void startup(Properties p) { // Override the Scheduler constants if specified by the user String val = p.getProperty("scheduler.username", null); if (val != null) { SchedulerConstants.SCHEDULER_DEFAULT_USERNAME = val; log.warn("Deprecated runtime property: scheduler.username. Value set in global_property in database now."); } val = p.getProperty("scheduler.password", null); if (val != null) { SchedulerConstants.SCHEDULER_DEFAULT_PASSWORD = val; log.warn("Deprecated runtime property: scheduler.username. Value set in global_property in database now."); } // TODO: do this for all services try { Context.addProxyPrivilege(PrivilegeConstants.MANAGE_SCHEDULER); Context.getSchedulerService().onStartup(); } finally { Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_SCHEDULER); } }
/** * Send an Alert to all super users that the given module did not start successfully. * * @param mod The Module that failed */ private static void notifySuperUsersAboutModuleFailure(Module mod) { try { // Add the privileges necessary for notifySuperUsers Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); Context.addProxyPrivilege(PrivilegeConstants.GET_USERS); // Send an alert to all administrators Context.getAlertService().notifySuperUsers("Module.startupError.notification.message", null, mod.getName()); } finally { // Remove added privileges Context.removeProxyPrivilege(PrivilegeConstants.GET_USERS); Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); } }
/** * Send an Alert to all super users that the given module did not start successfully. * * @param mod The Module that failed */ private static void notifySuperUsersAboutModuleFailure(Module mod) { try { // Add the privileges necessary for notifySuperUsers Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); // Send an alert to all administrators Context.getAlertService().notifySuperUsers("Module.startupError.notification.message", null, mod.getName()); } catch (Exception e) { log.error("Unable to send an alert to the super users", e); } finally { // Remove added privileges Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS); } }
/** * Shutdown the scheduler service that is statically associated with the Context class. */ public static void shutdown() { SchedulerService service = null; // ignores errors while getting the scheduler service try { service = Context.getSchedulerService(); } catch (Exception e) { // pass } // TODO: Do this for all services try { Context.addProxyPrivilege(PrivilegeConstants.MANAGE_SCHEDULER); // doesn't attempt shutdown if there was an error getting the scheduler service if (service != null) { service.onShutdown(); } } finally { Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_SCHEDULER); } }
Context.addProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS); log.debug("Executing sql: " + sql); String[] sqlStatements = sql.split(";"); Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
try { Context.openSession(); Context.addProxyPrivilege(PrivilegeConstants.ADD_ORDERS); uniqueOrderNumbers.add(((OrderNumberGenerator) orderService).getNewOrderNumber(null));
Context.addProxyPrivilege(PrivilegeConstants.EDIT_COHORTS); try { Context.getCohortService().notifyPatientVoided(patient);
private void voidExistingObs(Obs obs, String changeMessage, Obs newObs) { // void out the original observation to keep it around for // historical purposes try { Context.addProxyPrivilege(PrivilegeConstants.DELETE_OBS); // fetch a clean copy of this obs from the database so that // we don't write the changes to the database when we save // the fact that the obs is now voided evictObsAndChildren(obs); obs = Context.getObsService().getObs(obs.getObsId()); //delete the previous file from the appdata/complex_obs folder if (newObs.hasPreviousVersion() && newObs.getPreviousVersion().isComplex()) { File previousFile = AbstractHandler.getComplexDataFile(obs); previousFile.delete(); } // calling this via the service so that AOP hooks are called Context.getObsService().voidObs(obs, changeMessage); } finally { Context.removeProxyPrivilege(PrivilegeConstants.DELETE_OBS); } }
/** * @see org.openmrs.api.UserService#setUserProperty(User, String, String) */ @Override public User setUserProperty(User user, String key, String value) { if (user != null) { if (!Context.hasPrivilege(PrivilegeConstants.EDIT_USERS) && !user.equals(Context.getAuthenticatedUser())) { throw new APIException("you.are.not.authorized.change.properties", new Object[] { user.getUserId() }); } user.setUserProperty(key, value); try { Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS); Context.getUserService().saveUser(user); } finally { Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS); } } return user; }
/** * @see org.openmrs.api.UserService#removeUserProperty(org.openmrs.User, java.lang.String) */ @Override public User removeUserProperty(User user, String key) { if (user != null) { // if the current user isn't allowed to edit users and // the user being edited is not the current user, throw an // exception if (!Context.hasPrivilege(PrivilegeConstants.EDIT_USERS) && !user.equals(Context.getAuthenticatedUser())) { throw new APIException("you.are.not.authorized.change.properties", new Object[] { user.getUserId() }); } user.removeUserProperty(key); try { Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS); Context.getUserService().saveUser(user); } finally { Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS); } } return user; }
boolean emailAsUsername; try { Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES); emailAsUsername = Boolean.parseBoolean(as.getGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false"));
/** * @see EncounterService#voidEncounter(Encounter, String) */ @Test(expected = APIException.class) public void voidEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call voidEncounter(Encounter,String) method Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS); Context.getEncounterService().voidEncounter(encounter, "test"); }
/** * @see EncounterService#purgeEncounter(Encounter,Boolean) */ @Test(expected = APIException.class) public void purgeEncounterCascade_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call purgeEncounter(Encounter,Boolean) method Context.addProxyPrivilege(PrivilegeConstants.PURGE_ENCOUNTERS); Context.getEncounterService().purgeEncounter(encounter, Boolean.TRUE); }
/** * @see EncounterService#saveEncounter(Encounter) */ @Test(expected = APIException.class) public void saveEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call saveEncounter(Encounter) method Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS); Context.getEncounterService().saveEncounter(encounter); }
/** * @see EncounterService#purgeEncounter(Encounter) */ @Test(expected = APIException.class) public void purgeEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call purgeEncounter(Encounter) method Context.addProxyPrivilege(PrivilegeConstants.PURGE_ENCOUNTERS); Context.getEncounterService().purgeEncounter(encounter); }
/** * @see EncounterService#unvoidEncounter(Encounter) */ @Test(expected = APIException.class) public void unvoidEncounter_shouldFailfIfUserIsNotSupposedToEditEncountersOfTypeOfGivenEncounter() { // get encounter that has type with edit privilege set Encounter encounter = getEncounterWithEditPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call unvoidEncounter(Encounter) method Context.addProxyPrivilege(PrivilegeConstants.EDIT_ENCOUNTERS); Context.getEncounterService().unvoidEncounter(encounter); }
/** * @see EncounterService#getEncounter(Integer) */ @Test(expected = APIException.class) public void getEncounter_shouldFailIfUserIsNotAllowedToViewEncounterByGivenId() { // get encounter that has type with view privilege set Encounter encounter = getEncounterWithViewPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // left this user as is - i.e. without required privilege // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call getEncounter(Integer) method Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS); assertNull(Context.getEncounterService().getEncounter(encounter.getId())); }
Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS);
/** * @see EncounterService#getEncounter(Integer) */ @Test public void getEncounter_shouldReturnEncounterIfUserIsAllowedToViewIt() { // get encounter that has type with view privilege set Encounter encounter = getEncounterWithViewPrivilege(); User user = Context.getUserService().getUserByUsername("test_user"); assertNotNull(user); // add required privilege to role in which this user is Role role = Context.getUserService().getRole("Provider"); role.addPrivilege(encounter.getEncounterType().getViewPrivilege()); user.addRole(role); // and authenticate under it's account Context.becomeUser(user.getSystemId()); // have to add privilege in order to be able to call getEncounter(Integer) method Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS); assertNotNull(Context.getEncounterService().getEncounter(encounter.getId())); }