use of org.hisp.dhis.user.sharing.UserGroupAccess in project dhis2-core by dhis2.
the class DefaultAclStoreTest method getAccessibleProgramsReturnsUserGroupOnes.
@Test
void getAccessibleProgramsReturnsUserGroupOnes() {
// a private program
Program programA = createProgram('A');
programA.setPublicAccess("--------");
programA.getSharing().setOwner(owner);
manager.save(programA, false);
// a private program readable by a user group of which the user IS part
// of
Program programB = createProgram('B');
programB.setPublicAccess("--------");
programB.getSharing().setOwner(owner);
UserGroup g = createUserGroup('B', Set.of(owner, user));
UserGroupAccess a = new UserGroupAccess();
a.setUserGroup(g);
a.setAccess("--r-----");
programB.getSharing().addUserGroupAccess(a);
manager.save(programB, false);
List<Long> programIds = aclStore.getAccessiblePrograms(user.getUid(), Collections.singletonList(g.getUid()));
assertContainsOnly(programIds, programB.getId());
}
use of org.hisp.dhis.user.sharing.UserGroupAccess in project dhis2-core by dhis2.
the class SharingTest method withAccessAppliesToUserGroups.
@Test
void withAccessAppliesToUserGroups() {
Sharing original = new Sharing();
original.setUserGroups(singletonMap("key", new UserGroupAccess("abcd1234", "uid")));
Sharing actual = original.withAccess(Sharing::copyMetadataToData);
Map<String, UserGroupAccess> groups = actual.getUserGroups();
assertEquals(1, groups.size());
assertEquals("key", groups.keySet().iterator().next());
assertEquals("abab1234", groups.values().iterator().next().getAccess());
assertEquals("uid", groups.values().iterator().next().getId());
}
use of org.hisp.dhis.user.sharing.UserGroupAccess in project dhis2-core by dhis2.
the class SharingTest method addUserGroupAccessCreatesMapWhenNeeded.
@Test
void addUserGroupAccessCreatesMapWhenNeeded() {
Sharing actual = new Sharing();
actual.addUserGroupAccess(new UserGroupAccess("rw------", "uid"));
assertEquals(1, actual.getUserGroups().size());
}
use of org.hisp.dhis.user.sharing.UserGroupAccess in project dhis2-core by dhis2.
the class SharingController method postSharing.
@PostMapping(consumes = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage postSharing(@RequestParam String type, @RequestParam String id, HttpServletRequest request) throws Exception {
Class<? extends IdentifiableObject> sharingClass = aclService.classForType(type);
if (sharingClass == null || !aclService.isClassShareable(sharingClass)) {
return conflict("Type " + type + " is not supported.");
}
BaseIdentifiableObject object = (BaseIdentifiableObject) manager.getNoAcl(sharingClass, id);
if (object == null) {
return notFound("Object of type " + type + " with ID " + id + " was not found.");
}
if ((object instanceof SystemDefaultMetadataObject) && ((SystemDefaultMetadataObject) object).isDefault()) {
return conflict("Sharing settings of system default metadata object of type " + type + " cannot be modified.");
}
User user = currentUserService.getCurrentUser();
if (!aclService.canManage(user, object)) {
throw new AccessDeniedException("You do not have manage access to this object.");
}
Sharing sharing = renderService.fromJson(request.getInputStream(), Sharing.class);
if (!AccessStringHelper.isValid(sharing.getObject().getPublicAccess())) {
return conflict("Invalid public access string: " + sharing.getObject().getPublicAccess());
}
if (aclService.canMakeExternal(user, object)) {
object.setExternalAccess(sharing.getObject().hasExternalAccess());
}
// ---------------------------------------------------------------------
// Ignore publicAccess if user is not allowed to make objects public
// ---------------------------------------------------------------------
Schema schema = schemaService.getDynamicSchema(sharingClass);
if (aclService.canMakePublic(user, object)) {
object.setPublicAccess(sharing.getObject().getPublicAccess());
}
if (!schema.isDataShareable()) {
if (AccessStringHelper.hasDataSharing(object.getSharing().getPublicAccess())) {
object.getSharing().setPublicAccess(AccessStringHelper.disableDataSharing(object.getSharing().getPublicAccess()));
}
}
if (object.getCreatedBy() == null) {
object.setCreatedBy(user);
}
object.getSharing().getUserGroups().clear();
for (SharingUserGroupAccess sharingUserGroupAccess : sharing.getObject().getUserGroupAccesses()) {
UserGroupAccess userGroupAccess = new UserGroupAccess();
if (!AccessStringHelper.isValid(sharingUserGroupAccess.getAccess())) {
return conflict("Invalid user group access string: " + sharingUserGroupAccess.getAccess());
}
if (!schema.isDataShareable()) {
if (AccessStringHelper.hasDataSharing(sharingUserGroupAccess.getAccess())) {
sharingUserGroupAccess.setAccess(AccessStringHelper.disableDataSharing(sharingUserGroupAccess.getAccess()));
}
}
userGroupAccess.setAccess(sharingUserGroupAccess.getAccess());
UserGroup userGroup = manager.get(UserGroup.class, sharingUserGroupAccess.getId());
if (userGroup != null) {
userGroupAccess.setUserGroup(userGroup);
object.getSharing().addUserGroupAccess(userGroupAccess);
}
}
object.getSharing().getUsers().clear();
for (SharingUserAccess sharingUserAccess : sharing.getObject().getUserAccesses()) {
UserAccess userAccess = new UserAccess();
if (!AccessStringHelper.isValid(sharingUserAccess.getAccess())) {
return conflict("Invalid user access string: " + sharingUserAccess.getAccess());
}
if (!schema.isDataShareable()) {
if (AccessStringHelper.hasDataSharing(sharingUserAccess.getAccess())) {
sharingUserAccess.setAccess(AccessStringHelper.disableDataSharing(sharingUserAccess.getAccess()));
}
}
userAccess.setAccess(sharingUserAccess.getAccess());
User sharingUser = manager.get(User.class, sharingUserAccess.getId());
if (sharingUser != null) {
userAccess.setUser(sharingUser);
object.getSharing().addUserAccess(userAccess);
}
}
manager.updateNoAcl(object);
if (Program.class.isInstance(object)) {
syncSharingForEventProgram((Program) object);
}
log.info(sharingToString(object));
return ok("Access control set");
}
Aggregations