use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.
the class DefaultAclService method verifySharing.
@Override
public <T extends IdentifiableObject> List<ErrorReport> verifySharing(T object, User user) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || haveOverrideAuthority(user) || !isShareable(object)) {
return errorReports;
}
if (!AccessStringHelper.isValid(object.getSharing().getPublicAccess())) {
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3010, object.getPublicAccess()));
return errorReports;
}
Schema schema = schemaService.getSchema(HibernateProxyUtils.getRealClass(object));
if (!schema.isDataShareable()) {
ErrorReport errorReport = null;
if (object.getSharing().getPublicAccess() != null && AccessStringHelper.hasDataSharing(object.getSharing().getPublicAccess())) {
errorReport = new ErrorReport(object.getClass(), ErrorCode.E3011, object.getClass());
} else {
for (UserAccess userAccess : object.getSharing().getUsers().values()) {
if (AccessStringHelper.hasDataSharing(userAccess.getAccess())) {
errorReport = new ErrorReport(object.getClass(), ErrorCode.E3011, object.getClass());
break;
}
}
for (UserGroupAccess userGroupAccess : object.getSharing().getUserGroups().values()) {
if (AccessStringHelper.hasDataSharing(userGroupAccess.getAccess())) {
errorReport = new ErrorReport(object.getClass(), ErrorCode.E3011, object.getClass());
break;
}
}
}
if (errorReport != null) {
errorReports.add(errorReport);
}
}
boolean canMakePublic = canMakePublic(user, object);
boolean canMakePrivate = canMakePrivate(user, object);
boolean canMakeExternal = canMakeExternal(user, object);
if (object.getExternalAccess()) {
if (!canMakeExternal) {
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3006, user.getUsername(), object.getClass()));
}
}
errorReports.addAll(verifyImplicitSharing(user, object));
if (AccessStringHelper.DEFAULT.equals(object.getPublicAccess())) {
if (canMakePublic || canMakePrivate) {
return errorReports;
}
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3009, user.getUsername(), object.getClass()));
} else {
if (canMakePublic) {
return errorReports;
}
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3008, user.getUsername(), object.getClass()));
}
return errorReports;
}
use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.
the class DataApprovalStoreIntegrationTest method testApprovalStatusWithUserSharing.
@Test
void testApprovalStatusWithUserSharing() {
transactionTemplate.execute(status -> {
categoryOptionA.getSharing().addUserAccess(new UserAccess(userA, "r-r-----"));
categoryOptionB.getSharing().addUserAccess(new UserAccess(userA, "r-r-----"));
sharingTest(1);
return null;
});
}
use of org.hisp.dhis.user.sharing.UserAccess 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;
});
}
use of org.hisp.dhis.user.sharing.UserAccess 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));
}
use of org.hisp.dhis.user.sharing.UserAccess in project dhis2-core by dhis2.
the class DataSetServiceTest method testDataSharingDataSet.
@Test
void testDataSharingDataSet() {
User user = createUser('A');
injectSecurityContext(user);
DataSet dataSet = createDataSet('A', new MonthlyPeriodType());
UserAccess userAccess = new UserAccess();
userAccess.setUser(user);
userAccess.setAccess(AccessStringHelper.DATA_READ_WRITE);
dataSet.getSharing().addUserAccess(userAccess);
Access access = aclService.getAccess(dataSet, user);
assertTrue(access.getData().isRead());
assertTrue(access.getData().isWrite());
}
Aggregations