Search in sources :

Example 6 with BaseIdentifiableObject

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());
        }
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Property(org.hisp.dhis.schema.Property)

Example 7 with BaseIdentifiableObject

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);
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Schema(org.hisp.dhis.schema.Schema)

Example 8 with BaseIdentifiableObject

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));
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 9 with BaseIdentifiableObject

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);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) UserAccess(org.hisp.dhis.user.UserAccess) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) UserGroup(org.hisp.dhis.user.UserGroup) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Sharing(org.hisp.dhis.webapi.webdomain.sharing.Sharing) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with BaseIdentifiableObject

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();
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Aggregations

BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)13 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)5 User (org.hisp.dhis.user.User)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)3 Period (org.hisp.dhis.period.Period)3 Schema (org.hisp.dhis.schema.Schema)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ArrayList (java.util.ArrayList)2 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)2 Property (org.hisp.dhis.schema.Property)2 UserGroup (org.hisp.dhis.user.UserGroup)2 Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1