Search in sources :

Example 11 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class PrivilegeValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see PrivilegeValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    Privilege priv = new Privilege();
    priv.setPrivilege("Wallhacking");
    priv.setDescription("description");
    Errors errors = new BindException(priv, "priv");
    new PrivilegeValidator().validate(priv, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Privilege(org.openmrs.Privilege) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 12 with Privilege

use of org.openmrs.Privilege in project openmrs-core by openmrs.

the class UserServiceImpl method checkPrivileges.

/**
 * Convenience method to check if the authenticated user has all privileges they are giving out
 *
 * @param new user that has privileges
 */
private void checkPrivileges(User user) {
    Collection<Role> roles = user.getAllRoles();
    List<String> requiredPrivs = new ArrayList<>();
    for (Role r : roles) {
        if (r.getRole().equals(RoleConstants.SUPERUSER) && !Context.hasPrivilege(PrivilegeConstants.ASSIGN_SYSTEM_DEVELOPER_ROLE)) {
            throw new APIException("User.you.must.have.role", new Object[] { RoleConstants.SUPERUSER });
        }
        if (r.getPrivileges() != null) {
            for (Privilege p : r.getPrivileges()) {
                if (!Context.hasPrivilege(p.getPrivilege())) {
                    requiredPrivs.add(p.getPrivilege());
                }
            }
        }
    }
    if (requiredPrivs.size() == 1) {
        throw new APIException("User.you.must.have.privilege", new Object[] { requiredPrivs.get(0) });
    } else if (requiredPrivs.size() > 1) {
        StringBuilder txt = new StringBuilder("You must have the following privileges in order to assign them: ");
        for (String s : requiredPrivs) {
            txt.append(s).append(", ");
        }
        throw new APIException(txt.substring(0, txt.length() - 2));
    }
}
Also used : Role(org.openmrs.Role) APIException(org.openmrs.api.APIException) ArrayList(java.util.ArrayList) Privilege(org.openmrs.Privilege)

Example 13 with Privilege

use of org.openmrs.Privilege 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)

Example 14 with Privilege

use of org.openmrs.Privilege in project openmrs-module-mirebalais by PIH.

the class MirebalaisHospitalActivator method removeOldPrivileges.

/*  private void migratePaperRecordLocation(PaperRecordProperties paperRecordProperties) {

        Context.getAdministrationService().executeSQL("update patient_identifier set location_id = (select location_id from location where uuid='"+
                Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
                "where identifier_type = (select patient_identifier_type_id from patient_identifier_type where uuid = '" +
                paperRecordProperties.getPaperRecordIdentifierType().getUuid() + "')" +
                "and location_id = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);

        Context.getAdministrationService().executeSQL("update paperrecord_paper_record set record_location = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_HOSPITAL.uuid() + "')" +
                "where record_location = (select location_id from location where uuid='" +
                Locations.MIREBALAIS_CDI_PARENT.uuid() + "')", false);

    }*/
private void removeOldPrivileges() {
    UserService userService = Context.getUserService();
    Privilege privilege = userService.getPrivilege("App: appointmentschedulingui.scheduleAdmin");
    if (privilege != null) {
        userService.purgePrivilege(privilege);
    }
}
Also used : UserService(org.openmrs.api.UserService) Privilege(org.openmrs.Privilege)

Example 15 with Privilege

use of org.openmrs.Privilege in project openmrs-module-coreapps by openmrs.

the class VisitDetailsFragmentControllerTest method createPrivilegeToEditEncounters.

private Privilege createPrivilegeToEditEncounters() {
    Privilege privilege = new Privilege();
    privilege.setPrivilege(EmrApiConstants.PRIVILEGE_EDIT_ENCOUNTER);
    return privilege;
}
Also used : Privilege(org.openmrs.Privilege)

Aggregations

Privilege (org.openmrs.Privilege)25 Test (org.junit.Test)15 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)15 Role (org.openmrs.Role)6 BindException (org.springframework.validation.BindException)5 Errors (org.springframework.validation.Errors)5 EncounterType (org.openmrs.EncounterType)4 User (org.openmrs.User)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Encounter (org.openmrs.Encounter)2 EncounterRole (org.openmrs.EncounterRole)2 APIException (org.openmrs.api.APIException)2 UserService (org.openmrs.api.UserService)2 Document (org.w3c.dom.Document)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 GlobalProperty (org.openmrs.GlobalProperty)1 AdministrationService (org.openmrs.api.AdministrationService)1