Search in sources :

Example 21 with UserGroup

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

the class IdentifiableObjectManagerTest method testRemoveUserGroupFromSharing.

@Test
void testRemoveUserGroupFromSharing() {
    User userA = createUser('A');
    userService.addUser(userA);
    UserGroup userGroupA = createUserGroup('A', Sets.newHashSet(userA));
    identifiableObjectManager.save(userGroupA);
    String userGroupUid = userGroupA.getUid();
    DataElement de = createDataElement('A');
    Sharing sharing = new Sharing();
    sharing.setUserGroupAccess(singleton(new UserGroupAccess("rw------", userGroupA.getUid())));
    de.setSharing(sharing);
    identifiableObjectManager.save(de, false);
    de = identifiableObjectManager.get(de.getUid());
    assertEquals(1, de.getSharing().getUserGroups().size());
    identifiableObjectManager.delete(userGroupA);
    identifiableObjectManager.removeUserGroupFromSharing(userGroupUid);
    dbmsManager.clearSession();
    de = identifiableObjectManager.get(de.getUid());
    assertEquals(0, de.getSharing().getUserGroups().size());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) Sharing(org.hisp.dhis.user.sharing.Sharing) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 22 with UserGroup

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

the class HibernateIdentifiableObjectStoreTest method testMetadataRead.

/**
 * Test Metadata Read access User and UserGroups mapping User1 | User2 |
 * User3 | User 4 Group1 x | | | Group2 X | | | X
 *
 * DataElementA access defined for Users and UserGroups User1 | User2 |
 * User3 | UserGroup1 | UserGroup2 Can access DEA | X | | X |
 */
@Test
void testMetadataRead() {
    User admin = createAndInjectAdminUser();
    User user1 = new User();
    user1.setAutoFields();
    User user2 = new User();
    user2.setAutoFields();
    User user3 = new User();
    user3.setAutoFields();
    User user4 = new User();
    user4.setAutoFields();
    UserGroup userGroup1 = new UserGroup();
    userGroup1.setAutoFields();
    UserGroup userGroup2 = new UserGroup();
    userGroup2.setAutoFields();
    user1.getGroups().add(userGroup1);
    user1.getGroups().add(userGroup2);
    user4.getGroups().add(userGroup2);
    Map<String, UserAccess> userSharing = new HashMap<>();
    userSharing.put(user1.getUid(), new UserAccess(user1, AccessStringHelper.DEFAULT));
    userSharing.put(user2.getUid(), new UserAccess(user2, AccessStringHelper.READ));
    userSharing.put(user3.getUid(), new UserAccess(user3, AccessStringHelper.DEFAULT));
    userSharing.put(user4.getUid(), new UserAccess(user4, AccessStringHelper.DEFAULT));
    Map<String, UserGroupAccess> userGroupSharing = new HashMap<>();
    userGroupSharing.put(userGroup1.getUid(), new UserGroupAccess(userGroup1, AccessStringHelper.READ_WRITE));
    userGroupSharing.put(userGroup2.getUid(), new UserGroupAccess(userGroup2, AccessStringHelper.DEFAULT));
    DataElement dataElement = createDataElement('A');
    String dataElementUid = "deabcdefghA";
    dataElement.setUid(dataElementUid);
    dataElement.setCreatedBy(admin);
    Sharing sharing = Sharing.builder().external(false).publicAccess(AccessStringHelper.DEFAULT).owner("testOwner").userGroups(userGroupSharing).users(userSharing).build();
    dataElement.setSharing(sharing);
    dataElementStore.save(dataElement, false);
    dataElement = dataElementStore.getByUidNoAcl(dataElementUid);
    assertNotNull(dataElement.getSharing());
    assertEquals(2, dataElement.getSharing().getUserGroups().size());
    assertEquals(4, dataElement.getSharing().getUsers().size());
    // User1 can't access but it belong to UserGroup1 which has access
    assertNotNull(dataElementStore.getDataElement(dataElement.getUid(), user1));
    // User2 has access to DEA
    assertNotNull(dataElementStore.getDataElement(dataElement.getUid(), user2));
    // User3 doesn't have access and also does't belong to any groups
    assertNull(dataElementStore.getDataElement(dataElement.getUid(), user3));
    // User4 doesn't have access and it belong to UserGroup2 which also
    // doesn't have access
    assertNull(dataElementStore.getDataElement(dataElement.getUid(), user4));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) UserAccess(org.hisp.dhis.user.sharing.UserAccess) HashMap(java.util.HashMap) Sharing(org.hisp.dhis.user.sharing.Sharing) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 23 with UserGroup

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

the class DataApprovalAuditServiceTest method setPrivateAccess.

private void setPrivateAccess(BaseIdentifiableObject object, UserGroup... userGroups) {
    object.getSharing().setPublicAccess(ACCESS_NONE);
    // Needed for sharing to work
    object.setOwner(userZ.getUid());
    object.getSharing().setOwner(userZ);
    for (UserGroup group : userGroups) {
        object.getSharing().addUserGroupAccess(new UserGroupAccess(group, ACCESS_READ));
    }
    identifiableObjectManager.updateNoAcl(object);
}
Also used : UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess)

Example 24 with UserGroup

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

the class DataApprovalAuditServiceTest method getUserGroup.

private UserGroup getUserGroup(String userGroupName, Set<User> users) {
    UserGroup userGroup = new UserGroup();
    userGroup.setAutoFields();
    userGroup.setName(userGroupName);
    userGroup.setMembers(users);
    userGroupService.addUserGroup(userGroup);
    return userGroup;
}
Also used : UserGroup(org.hisp.dhis.user.UserGroup)

Example 25 with UserGroup

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

the class AclServiceTest method testUserCanUpdateDashboardSharedWithUserGroup.

@Test
void testUserCanUpdateDashboardSharedWithUserGroup() {
    User user1 = createUser('A');
    User user2 = createUser('B');
    manager.save(user1);
    manager.save(user2);
    UserGroup userGroup = createUserGroup('A', Sets.newHashSet(user1, user2));
    manager.save(userGroup);
    user1.getGroups().add(userGroup);
    user2.getGroups().add(userGroup);
    Dashboard dashboard = new Dashboard("Dashboard");
    dashboard.setCreatedBy(user1);
    dashboard.getSharing().setOwner(user1);
    manager.save(dashboard);
    UserGroupAccess userGroupAccess = new UserGroupAccess(userGroup, AccessStringHelper.READ);
    dashboard.getSharing().addUserGroupAccess(userGroupAccess);
    manager.save(dashboard, false);
    assertTrue(aclService.canRead(user1, dashboard));
    assertTrue(aclService.canUpdate(user1, dashboard));
    assertTrue(aclService.canDelete(user1, dashboard));
    assertTrue(aclService.canManage(user1, dashboard));
    Access access = aclService.getAccess(dashboard, user2);
    assertTrue(access.isRead());
    assertFalse(access.isUpdate());
    assertFalse(access.isDelete());
    assertFalse(access.isManage());
    assertTrue(aclService.canRead(user2, dashboard));
    assertFalse(aclService.canUpdate(user2, dashboard));
    assertFalse(aclService.canDelete(user2, dashboard));
    assertFalse(aclService.canManage(user2, dashboard));
}
Also used : User(org.hisp.dhis.user.User) UserAccess(org.hisp.dhis.user.sharing.UserAccess) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) Dashboard(org.hisp.dhis.dashboard.Dashboard) UserGroup(org.hisp.dhis.user.UserGroup) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

UserGroup (org.hisp.dhis.user.UserGroup)76 User (org.hisp.dhis.user.User)50 Test (org.junit.jupiter.api.Test)32 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)23 UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)22 DataElement (org.hisp.dhis.dataelement.DataElement)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 HashSet (java.util.HashSet)11 List (java.util.List)11 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)10 UserAccess (org.hisp.dhis.user.sharing.UserAccess)10 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Program (org.hisp.dhis.program.Program)6 ArrayList (java.util.ArrayList)5 CategoryOption (org.hisp.dhis.category.CategoryOption)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)4 UserGroupAccess (org.hisp.dhis.user.UserGroupAccess)4