Search in sources :

Example 21 with Privilege

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

the class PrivilegeValidator method validate.

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if privilege is null or empty or whitespace
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object obj, Errors errors) {
    Privilege privilege = (Privilege) obj;
    if (privilege == null) {
        errors.rejectValue("privilege", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "privilege", "error.privilege");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "privilege", "description");
    }
}
Also used : Privilege(org.openmrs.Privilege)

Example 22 with Privilege

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

the class Context method checkCoreDataset.

/**
 * Runs through the core data (e.g. privileges, roles, and global properties) and adds them if
 * necessary.
 */
public static void checkCoreDataset() {
    // setting core roles
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
        Set<String> currentRoleNames = new HashSet<>();
        for (Role role : Context.getUserService().getAllRoles()) {
            currentRoleNames.add(role.getRole().toUpperCase());
        }
        Map<String, String> map = OpenmrsUtil.getCoreRoles();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String roleName = entry.getKey();
            if (!currentRoleNames.contains(roleName.toUpperCase())) {
                Role role = new Role();
                role.setRole(roleName);
                role.setDescription(entry.getValue());
                Context.getUserService().saveRole(role);
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core roles for openmrs system", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ROLES);
    }
    // setting core privileges
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
        Set<String> currentPrivilegeNames = new HashSet<>();
        for (Privilege privilege : Context.getUserService().getAllPrivileges()) {
            currentPrivilegeNames.add(privilege.getPrivilege().toUpperCase());
        }
        Map<String, String> map = OpenmrsUtil.getCorePrivileges();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String privilegeName = entry.getKey();
            if (!currentPrivilegeNames.contains(privilegeName.toUpperCase())) {
                Privilege p = new Privilege();
                p.setPrivilege(privilegeName);
                p.setDescription(entry.getValue());
                Context.getUserService().savePrivilege(p);
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core privileges", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_PRIVILEGES);
    }
    // setting core global properties
    try {
        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
        Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
        Set<String> currentPropNames = new HashSet<>();
        Map<String, GlobalProperty> propsMissingDescription = new HashMap<>();
        Map<String, GlobalProperty> propsMissingDatatype = new HashMap<>();
        for (GlobalProperty prop : Context.getAdministrationService().getAllGlobalProperties()) {
            currentPropNames.add(prop.getProperty().toUpperCase());
            if (prop.getDescription() == null) {
                propsMissingDescription.put(prop.getProperty().toUpperCase(), prop);
            }
            if (prop.getDatatypeClassname() == null) {
                propsMissingDatatype.put(prop.getProperty().toUpperCase(), prop);
            }
        }
        for (GlobalProperty coreProp : OpenmrsConstants.CORE_GLOBAL_PROPERTIES()) {
            String corePropName = coreProp.getProperty().toUpperCase();
            // if the prop doesn't exist, save it
            if (!currentPropNames.contains(corePropName)) {
                Context.getAdministrationService().saveGlobalProperty(coreProp);
                // add to list in case
                currentPropNames.add(corePropName);
            // of duplicates
            } else {
                // if the prop is missing its description, update it
                GlobalProperty propToUpdate = propsMissingDescription.get(corePropName);
                if (propToUpdate != null) {
                    propToUpdate.setDescription(coreProp.getDescription());
                    Context.getAdministrationService().saveGlobalProperty(propToUpdate);
                }
                // set missing datatypes
                propToUpdate = propsMissingDatatype.get(corePropName);
                if (propToUpdate != null && coreProp.getDatatypeClassname() != null) {
                    propToUpdate.setDatatypeClassname(coreProp.getDatatypeClassname());
                    propToUpdate.setDatatypeConfig(coreProp.getDatatypeConfig());
                    propToUpdate.setPreferredHandlerClassname(coreProp.getPreferredHandlerClassname());
                    propToUpdate.setHandlerConfig(coreProp.getHandlerConfig());
                    Context.getAdministrationService().saveGlobalProperty(propToUpdate);
                }
            }
        }
    } catch (Exception e) {
        log.error("Error while setting core global properties", e);
    } finally {
        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
        Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
    }
    // setting default validation rule
    AdministrationService as = Context.getAdministrationService();
    Boolean disableValidation = Boolean.valueOf(as.getGlobalProperty(OpenmrsConstants.GP_DISABLE_VALIDATION, "false"));
    ValidateUtil.setDisableValidation(disableValidation);
    PersonName.setFormat(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_FORMAT));
    Allergen.setOtherNonCodedConceptUuid(Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_ALLERGEN_OTHER_NON_CODED_UUID));
}
Also used : HashMap(java.util.HashMap) DatabaseUpdateException(org.openmrs.util.DatabaseUpdateException) InputRequiredException(org.openmrs.util.InputRequiredException) APIException(org.openmrs.api.APIException) ModuleMustStartException(org.openmrs.module.ModuleMustStartException) MessageException(org.openmrs.notification.MessageException) GlobalProperty(org.openmrs.GlobalProperty) Role(org.openmrs.Role) AdministrationService(org.openmrs.api.AdministrationService) Privilege(org.openmrs.Privilege) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 23 with Privilege

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

the class ModuleFileParser method extractPrivileges.

private List<Privilege> extractPrivileges(Element configRoot) {
    List<Privilege> result = new ArrayList<>();
    NodeList privileges = configRoot.getElementsByTagName("privilege");
    if (privileges.getLength() == 0) {
        return result;
    }
    log.debug("# privileges: {}", privileges.getLength());
    int i = 0;
    while (i < privileges.getLength()) {
        Element element = (Element) privileges.item(i);
        String name = getElementTrimmed(element, "name");
        String description = getElementTrimmed(element, "description");
        log.debug("extension name: {}, description: {}", name, description);
        if (name.isEmpty() || description.isEmpty()) {
            log.warn("'name' and 'description' are required for privilege. Given '{}' and '{}'", name, description);
        } else {
            result.add(new Privilege(name, description));
        }
        i++;
    }
    return result;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Privilege(org.openmrs.Privilege)

Example 24 with Privilege

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

the class UserServiceTest method saveUser_shouldGrantNewRolesInRolesListToUser.

@Test
public void saveUser_shouldGrantNewRolesInRolesListToUser() {
    // add in some basic properties
    executeDataSet(XML_FILENAME);
    User u = userService.getUserByUsername(ADMIN_USERNAME);
    Role role1 = new Role();
    role1.setDescription("testing1");
    role1.setRole("test1");
    Privilege p1 = userService.getAllPrivileges().get(0);
    Set<Privilege> privileges1 = new HashSet<>();
    privileges1.add(p1);
    role1.setPrivileges(privileges1);
    Role role2 = new Role();
    role2.setDescription("testing2");
    role2.setRole("test2");
    Privilege p2 = userService.getAllPrivileges().get(0);
    Set<Privilege> privileges2 = new HashSet<>();
    privileges2.add(p2);
    role2.setPrivileges(privileges2);
    userService.saveUser(u.addRole(role1));
    userService.saveUser(u.addRole(role2));
    // so the contents are fetched from the db
    Context.evictFromSession(u);
    userService.getUser(u.getUserId()).hasRole("test1");
    userService.getUser(u.getUserId()).hasRole("test2");
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) Privilege(org.openmrs.Privilege) HashSet(java.util.HashSet) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 25 with Privilege

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

the class UserServiceTest method getPrivilegeByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see UserService#getPrivilegeByUuid(String)
 */
@Test
public void getPrivilegeByUuid_shouldFindObjectGivenValidUuid() {
    executeDataSet(XML_FILENAME);
    String uuid = "d979d066-15e6-467c-9d4b-cb575ef97f0f";
    Privilege privilege = userService.getPrivilegeByUuid(uuid);
    Assert.assertEquals("Some Privilege", privilege.getPrivilege());
}
Also used : Privilege(org.openmrs.Privilege) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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