Search in sources :

Example 1 with UserGroupAccess

use of org.hisp.dhis.user.sharing.UserGroupAccess 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 2 with UserGroupAccess

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

the class SharingTest method setDtoUserGroupAccessClearsExisting.

@Test
void setDtoUserGroupAccessClearsExisting() {
    Sharing actual = new Sharing();
    UserGroup group1 = new UserGroup();
    group1.setUid("id");
    actual.setDtoUserGroupAccesses(singleton(new org.hisp.dhis.user.UserGroupAccess(group1, "rw------")));
    UserGroup group2 = new UserGroup();
    group2.setUid("uid");
    actual.setDtoUserGroupAccesses(singleton(new org.hisp.dhis.user.UserGroupAccess(group2, "r-------")));
    assertEquals(1, actual.getUserGroups().size());
    UserGroupAccess userAccess = actual.getUserGroups().values().iterator().next();
    assertEquals("r-------", userAccess.getAccess());
    assertEquals("uid", userAccess.getId());
}
Also used : Sharing(org.hisp.dhis.user.sharing.Sharing) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) Test(org.junit.jupiter.api.Test)

Example 3 with UserGroupAccess

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

the class SharingTest method setUserGroupAccessClearsExisting.

@Test
void setUserGroupAccessClearsExisting() {
    Sharing actual = new Sharing();
    actual.setUserGroupAccess(singleton(new UserGroupAccess("rw------", "id")));
    actual.setUserGroupAccess(singleton(new UserGroupAccess("r-------", "uid")));
    assertEquals(1, actual.getUserGroups().size());
    UserGroupAccess userAccess = actual.getUserGroups().values().iterator().next();
    assertEquals("r-------", userAccess.getAccess());
    assertEquals("uid", userAccess.getId());
}
Also used : Sharing(org.hisp.dhis.user.sharing.Sharing) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) Test(org.junit.jupiter.api.Test)

Example 4 with UserGroupAccess

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

the class InterpretationServiceTest method testCreateChartAndInterpretationSyncSharing.

// TODO enable
@Test
@Disabled
void testCreateChartAndInterpretationSyncSharing() throws IOException {
    UserGroup userGroup = createUserGroup('A', Sets.newHashSet(userA, userB));
    userGroupService.addUserGroup(userGroup);
    Visualization visualization = createVisualization('A');
    manager.save(visualization);
    visualization.setPublicAccess(AccessStringHelper.READ_WRITE);
    visualization.getSharing().addUserGroupAccess(new UserGroupAccess(userGroup, AccessStringHelper.READ));
    assertEquals(1, visualization.getUserGroupAccesses().size());
    manager.update(visualization);
    assertEquals(AccessStringHelper.READ_WRITE, visualization.getPublicAccess());
    assertEquals(1, visualization.getUserGroupAccesses().size());
    Interpretation interpretation = new Interpretation(visualization, null, "test");
    interpretationService.saveInterpretation(interpretation);
    interpretationService.updateInterpretation(interpretation);
    assertEquals(AccessStringHelper.READ_WRITE, interpretation.getPublicAccess());
    assertEquals(interpretation.getUserGroupAccesses().size(), visualization.getUserGroupAccesses().size());
}
Also used : Visualization(org.hisp.dhis.visualization.Visualization) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with UserGroupAccess

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

the class DataApprovalStoreIntegrationTest method testApprovalStatusWithOtherUserAccess.

@Test
void testApprovalStatusWithOtherUserAccess() {
    transactionTemplate.execute(status -> {
        categoryOptionA.getSharing().setOwner(userB);
        categoryOptionB.getSharing().setOwner(userB);
        categoryOptionA.getSharing().addUserAccess(new UserAccess(userB, "r-r-----"));
        categoryOptionB.getSharing().addUserAccess(new UserAccess(userB, "r-r-----"));
        categoryOptionA.getSharing().addUserGroupAccess(new UserGroupAccess(userGroupB, "r-r-----"));
        categoryOptionB.getSharing().addUserGroupAccess(new UserGroupAccess(userGroupB, "r-r-----"));
        sharingTest(0);
        return null;
    });
}
Also used : UserAccess(org.hisp.dhis.user.sharing.UserAccess) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)29 UserGroup (org.hisp.dhis.user.UserGroup)22 Test (org.junit.jupiter.api.Test)21 User (org.hisp.dhis.user.User)14 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)13 UserAccess (org.hisp.dhis.user.sharing.UserAccess)13 DataElement (org.hisp.dhis.dataelement.DataElement)10 Sharing (org.hisp.dhis.user.sharing.Sharing)8 HashMap (java.util.HashMap)3 Schema (org.hisp.dhis.schema.Schema)3 ArrayList (java.util.ArrayList)2 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 CategoryOption (org.hisp.dhis.category.CategoryOption)2 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2 SystemDefaultMetadataObject (org.hisp.dhis.common.SystemDefaultMetadataObject)2 ErrorReport (org.hisp.dhis.feedback.ErrorReport)2 Program (org.hisp.dhis.program.Program)2 Date (java.util.Date)1 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)1 Dashboard (org.hisp.dhis.dashboard.Dashboard)1