use of org.hisp.dhis.common.BaseIdentifiableObject in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.
private <T extends IdentifiableObject> void handleEmbeddedObjects(T object, ObjectBundle bundle, Collection<Property> properties) {
for (Property property : properties) {
if (property.isCollection()) {
Collection<?> objects = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
objects.forEach(o -> {
if (property.isIdentifiableObject()) {
((BaseIdentifiableObject) o).setAutoFields();
}
preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
});
} else {
Object o = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (property.isIdentifiableObject()) {
((BaseIdentifiableObject) o).setAutoFields();
}
preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
}
}
}
use of org.hisp.dhis.common.BaseIdentifiableObject in project dhis2-core by dhis2.
the class IdentifiableObjectBundleHook method preCreate.
@Override
public void preCreate(IdentifiableObject identifiableObject, ObjectBundle bundle) {
((BaseIdentifiableObject) identifiableObject).setAutoFields();
Schema schema = schemaService.getDynamicSchema(identifiableObject.getClass());
handleAttributeValues(identifiableObject, bundle, schema);
}
use of org.hisp.dhis.common.BaseIdentifiableObject in project dhis2-core by dhis2.
the class HibernateGenericStore method update.
@Override
public void update(T object, User user) {
String username = user != null ? user.getUsername() : "system-process";
if (IdentifiableObject.class.isInstance(object)) {
BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
identifiableObject.setAutoFields();
identifiableObject.setLastUpdatedBy(user);
if (identifiableObject.getUser() == null) {
identifiableObject.setUser(user);
}
}
if (!isUpdateAllowed(object, user)) {
AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_UPDATE_DENIED);
throw new UpdateAccessDeniedException(object.toString());
}
AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_UPDATE);
if (object != null) {
getSession().update(object);
}
if (MetadataObject.class.isInstance(object)) {
deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery((IdentifiableObject) object));
}
}
use of org.hisp.dhis.common.BaseIdentifiableObject in project dhis2-core by dhis2.
the class SharingController method setSharing.
@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE)
public void setSharing(@RequestParam String type, @RequestParam String id, HttpServletResponse response, HttpServletRequest request) throws IOException, WebMessageException {
Class<? extends IdentifiableObject> sharingClass = aclService.classForType(type);
if (sharingClass == null || !aclService.isShareable(sharingClass)) {
throw new WebMessageException(WebMessageUtils.conflict("Type " + type + " is not supported."));
}
BaseIdentifiableObject object = (BaseIdentifiableObject) manager.get(sharingClass, id);
if (object == null) {
throw new WebMessageException(WebMessageUtils.notFound("Object of type " + type + " with ID " + id + " was not found."));
}
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())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid public access string: " + sharing.getObject().getPublicAccess()));
}
if (aclService.canMakeExternal(user, object.getClass())) {
object.setExternalAccess(sharing.getObject().hasExternalAccess());
}
if (aclService.canMakePublic(user, object.getClass())) {
object.setPublicAccess(sharing.getObject().getPublicAccess());
}
if (object.getUser() == null) {
object.setUser(user);
}
Iterator<UserGroupAccess> userGroupAccessIterator = object.getUserGroupAccesses().iterator();
while (userGroupAccessIterator.hasNext()) {
UserGroupAccess userGroupAccess = userGroupAccessIterator.next();
userGroupAccessIterator.remove();
userGroupAccessService.deleteUserGroupAccess(userGroupAccess);
}
for (SharingUserGroupAccess sharingUserGroupAccess : sharing.getObject().getUserGroupAccesses()) {
UserGroupAccess userGroupAccess = new UserGroupAccess();
if (!AccessStringHelper.isValid(sharingUserGroupAccess.getAccess())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid user group access string: " + sharingUserGroupAccess.getAccess()));
}
userGroupAccess.setAccess(sharingUserGroupAccess.getAccess());
UserGroup userGroup = manager.get(UserGroup.class, sharingUserGroupAccess.getId());
if (userGroup != null) {
userGroupAccess.setUserGroup(userGroup);
userGroupAccessService.addUserGroupAccess(userGroupAccess);
object.getUserGroupAccesses().add(userGroupAccess);
}
}
Iterator<UserAccess> userAccessIterator = object.getUserAccesses().iterator();
while (userAccessIterator.hasNext()) {
UserAccess userAccess = userAccessIterator.next();
userAccessIterator.remove();
userAccessService.deleteUserAccess(userAccess);
}
for (SharingUserAccess sharingUserAccess : sharing.getObject().getUserAccesses()) {
UserAccess userAccess = new UserAccess();
if (!AccessStringHelper.isValid(sharingUserAccess.getAccess())) {
throw new WebMessageException(WebMessageUtils.conflict("Invalid user access string: " + sharingUserAccess.getAccess()));
}
userAccess.setAccess(sharingUserAccess.getAccess());
User sharingUser = manager.get(User.class, sharingUserAccess.getId());
if (sharingUser != null) {
userAccess.setUser(sharingUser);
userAccessService.addUserAccess(userAccess);
object.getUserAccesses().add(userAccess);
}
}
manager.updateNoAcl(object);
log.info(sharingToString(object));
webMessageService.send(WebMessageUtils.ok("Access control set"), response, request);
}
use of org.hisp.dhis.common.BaseIdentifiableObject in project dhis2-core by dhis2.
the class DefaultAclService method resetSharing.
@Override
public <T extends IdentifiableObject> void resetSharing(T object, User user) {
if (object == null || !isShareable(object.getClass()) || user == null) {
return;
}
BaseIdentifiableObject baseIdentifiableObject = (BaseIdentifiableObject) object;
baseIdentifiableObject.setPublicAccess(AccessStringHelper.DEFAULT);
baseIdentifiableObject.setExternalAccess(false);
if (object.getUser() == null) {
baseIdentifiableObject.setUser(user);
}
if (canMakePublic(user, object.getClass())) {
if (defaultPublic(object.getClass())) {
baseIdentifiableObject.setPublicAccess(AccessStringHelper.READ_WRITE);
}
}
object.getUserAccesses().clear();
object.getUserGroupAccesses().clear();
}
Aggregations