Search in sources :

Example 6 with UserAccess

use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.

the class DashboardCascadeSharingTest method setUpTest.

@Override
public void setUpTest() {
    userService = _userService;
    userGroupA = createUserGroup('A', Collections.EMPTY_SET);
    objectManager.save(userGroupA);
    userA = createUser('A');
    userA.getGroups().add(userGroupA);
    userService.addUser(userA);
    userB = createUser('B');
    userService.addUser(userB);
    sharingReadForUserA = new Sharing(DEFAULT, new UserAccess(userA, READ));
    sharingReadWriteForUserB = new Sharing(DEFAULT, new UserAccess(userB, READ_WRITE));
    sharingReadForUserAB = new Sharing(DEFAULT, new UserAccess(userA, READ), new UserAccess(userB, READ));
    sharingUserGroupA = new Sharing(DEFAULT, new UserGroupAccess(userGroupA, READ));
    programA = createProgram('A');
    programA.setSharing(defaultSharing());
    objectManager.save(programA, false);
    createAndInjectAdminUser();
}
Also used : Sharing(org.hisp.dhis.user.sharing.Sharing) UserAccess(org.hisp.dhis.user.sharing.UserAccess) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess)

Example 7 with UserAccess

use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.

the class DhisConvenienceTest method enableDataSharing.

protected void enableDataSharing(User user, IdentifiableObject object, String access) {
    object.getSharing().resetUserAccesses();
    UserAccess userAccess = new UserAccess();
    userAccess.setUser(user);
    userAccess.setAccess(access);
    object.getSharing().addUserAccess(userAccess);
}
Also used : UserAccess(org.hisp.dhis.user.sharing.UserAccess)

Example 8 with UserAccess

use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.

the class SharingTest method withAccessAppliesToUsers.

@Test
void withAccessAppliesToUsers() {
    Sharing original = new Sharing();
    original.setUsers(singletonMap("key", new UserAccess("abcd1234", "uid")));
    Sharing actual = original.withAccess(Sharing::copyMetadataToData);
    Map<String, UserAccess> users = actual.getUsers();
    assertEquals(1, users.size());
    assertEquals("key", users.keySet().iterator().next());
    assertEquals("abab1234", users.values().iterator().next().getAccess());
    assertEquals("uid", users.values().iterator().next().getId());
}
Also used : Sharing(org.hisp.dhis.user.sharing.Sharing) UserAccess(org.hisp.dhis.user.sharing.UserAccess) Test(org.junit.jupiter.api.Test)

Example 9 with UserAccess

use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.

the class DefaultSharingService method saveSharing.

@Override
public <T extends IdentifiableObject> ObjectReport saveSharing(@NotNull Class<T> entityClass, @NotNull T entity, @NotNull Sharing sharing) {
    ObjectReport objectReport = new ObjectReport(Sharing.class, 0);
    BaseIdentifiableObject object = (BaseIdentifiableObject) entity;
    if ((object instanceof SystemDefaultMetadataObject) && ((SystemDefaultMetadataObject) object).isDefault()) {
        objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3013, entityClass.getSimpleName()).setErrorKlass(entityClass));
    }
    User user = currentUserService.getCurrentUser();
    if (!aclService.canManage(user, object)) {
        objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3014).setErrorKlass(entityClass));
    }
    if (!AccessStringHelper.isValid(sharing.getPublicAccess())) {
        objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3015, sharing.getPublicAccess()).setErrorKlass(entityClass));
    }
    if (aclService.canMakeClassExternal(user, entityClass)) {
        object.getSharing().setExternal(sharing.isExternal());
    }
    // ---------------------------------------------------------------------
    // Ignore publicAccess if user is not allowed to make objects public
    // ---------------------------------------------------------------------
    Schema schema = schemaService.getDynamicSchema(entityClass);
    if (aclService.canMakePublic(user, object)) {
        object.setPublicAccess(sharing.getPublicAccess());
    }
    if (!schema.isDataShareable()) {
        if (AccessStringHelper.hasDataSharing(object.getSharing().getPublicAccess())) {
            objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3016).setErrorKlass(entityClass));
        }
    }
    object.getSharing().setOwner(sharing.getOwner());
    // --------------------------------------
    // Handle UserGroupAccesses
    // --------------------------------------
    object.getSharing().getUserGroups().clear();
    if (sharing.hasUserGroupAccesses()) {
        for (UserGroupAccess sharingUserGroupAccess : sharing.getUserGroups().values()) {
            if (!AccessStringHelper.isValid(sharingUserGroupAccess.getAccess())) {
                objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3017, sharingUserGroupAccess.getAccess()).setErrorKlass(entityClass));
            }
            if (!schema.isDataShareable()) {
                if (AccessStringHelper.hasDataSharing(sharingUserGroupAccess.getAccess())) {
                    objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3016).setErrorKlass(entityClass));
                }
            }
            UserGroup userGroup = userGroupService.getUserGroup(sharingUserGroupAccess.getId());
            if (userGroup != null) {
                object.getSharing().addUserGroupAccess(sharingUserGroupAccess);
            }
        }
    }
    // --------------------------------------
    // Handle UserAccesses
    // --------------------------------------
    object.getSharing().getUsers().clear();
    if (sharing.hasUserAccesses()) {
        for (UserAccess sharingUserAccess : sharing.getUsers().values()) {
            if (!AccessStringHelper.isValid(sharingUserAccess.getAccess())) {
                objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3018, sharingUserAccess.getAccess()).setErrorKlass(entityClass));
            }
            if (!schema.isDataShareable()) {
                if (AccessStringHelper.hasDataSharing(sharingUserAccess.getAccess())) {
                    objectReport.addErrorReport(new ErrorReport(Sharing.class, ErrorCode.E3016).setErrorKlass(entityClass));
                }
            }
            User sharingUser = userService.getUser(sharingUserAccess.getId());
            if (sharingUser != null) {
                object.getSharing().addUserAccess(sharingUserAccess);
            }
        }
    }
    manager.updateNoAcl(object);
    if (Program.class.isInstance(object)) {
        syncSharingForEventProgram((Program) object);
    }
    log.info(sharingToString(object));
    return objectReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) User(org.hisp.dhis.user.User) UserAccess(org.hisp.dhis.user.sharing.UserAccess) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) SystemDefaultMetadataObject(org.hisp.dhis.common.SystemDefaultMetadataObject) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) UserGroup(org.hisp.dhis.user.UserGroup)

Example 10 with UserAccess

use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.

the class ObjectBundleServiceUserTest method testUpdateUserWithNoAccessUserRole.

@Test
void testUpdateUserWithNoAccessUserRole() throws IOException {
    createUserAndInjectSecurityContext(true);
    ObjectBundleParams params = createBundleParams(ObjectBundleMode.COMMIT, ImportStrategy.CREATE_AND_UPDATE, AtomicMode.ALL, "dxf2/user_userrole.json");
    ObjectBundle bundle1 = objectBundleService.create(params);
    objectBundleService.commit(bundle1);
    User userB = manager.get(User.class, "MwhEJUnTHkn");
    User userA = manager.get(User.class, "sPWjoHSY03y");
    assertEquals(2, userA.getUserAuthorityGroups().size());
    assertEquals(2, userB.getUserAuthorityGroups().size());
    UserAuthorityGroup userManagerRole = manager.get(UserAuthorityGroup.class, "xJZBzAHI88H");
    assertNotNull(userManagerRole);
    userManagerRole.getSharing().resetUserAccesses();
    userManagerRole.getSharing().addUserAccess(new UserAccess(userB, "rw------"));
    userManagerRole.setPublicAccess("--------");
    userManagerRole.setCreatedBy(userB);
    manager.update(userManagerRole);
    SecurityContextHolder.clearContext();
    userA.setPassword("passwordUserA");
    manager.update(userA);
    injectSecurityContext(userA);
    params = createBundleParams(ObjectBundleMode.COMMIT, ImportStrategy.CREATE_AND_UPDATE, AtomicMode.ALL, "dxf2/user_userrole_update.json");
    ObjectBundle bundle2 = objectBundleService.create(params);
    objectBundleService.commit(bundle2);
    assertEquals(2, userA.getUserAuthorityGroups().size());
    assertEquals(2, userB.getUserAuthorityGroups().size());
}
Also used : User(org.hisp.dhis.user.User) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) UserAccess(org.hisp.dhis.user.sharing.UserAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

UserAccess (org.hisp.dhis.user.sharing.UserAccess)31 Test (org.junit.jupiter.api.Test)25 User (org.hisp.dhis.user.User)19 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)14 Sharing (org.hisp.dhis.user.sharing.Sharing)12 UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)9 DataElement (org.hisp.dhis.dataelement.DataElement)8 Dashboard (org.hisp.dhis.dashboard.Dashboard)7 EventVisualization (org.hisp.dhis.eventvisualization.EventVisualization)6 UserGroup (org.hisp.dhis.user.UserGroup)6 HashMap (java.util.HashMap)4 List (java.util.List)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 Schema (org.hisp.dhis.schema.Schema)3 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)3 Visualization (org.hisp.dhis.visualization.Visualization)3 Date (java.util.Date)2 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)2 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2