Search in sources :

Example 1 with SharingUserAccess

use of org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess 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 2 with SharingUserAccess

use of org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess in project dhis2-core by dhis2.

the class SharingController method getSharing.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void getSharing(@RequestParam String type, @RequestParam String id, HttpServletResponse response) throws IOException, WebMessageException {
    if (!aclService.isShareable(type)) {
        throw new WebMessageException(WebMessageUtils.conflict("Type " + type + " is not supported."));
    }
    Class<? extends IdentifiableObject> klass = aclService.classForType(type);
    IdentifiableObject object = manager.get(klass, 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.canRead(user, object)) {
        throw new AccessDeniedException("You do not have manage access to this object.");
    }
    Sharing sharing = new Sharing();
    sharing.getMeta().setAllowPublicAccess(aclService.canMakePublic(user, object.getClass()));
    sharing.getMeta().setAllowExternalAccess(aclService.canMakeExternal(user, object.getClass()));
    sharing.getObject().setId(object.getUid());
    sharing.getObject().setName(object.getDisplayName());
    sharing.getObject().setDisplayName(object.getDisplayName());
    sharing.getObject().setExternalAccess(object.getExternalAccess());
    if (object.getPublicAccess() == null) {
        String access;
        if (aclService.canMakePublic(user, klass)) {
            access = AccessStringHelper.newInstance().enable(AccessStringHelper.Permission.READ).enable(AccessStringHelper.Permission.WRITE).build();
        } else {
            access = AccessStringHelper.newInstance().build();
        }
        sharing.getObject().setPublicAccess(access);
    } else {
        sharing.getObject().setPublicAccess(object.getPublicAccess());
    }
    if (object.getUser() != null) {
        sharing.getObject().getUser().setId(object.getUser().getUid());
        sharing.getObject().getUser().setName(object.getUser().getDisplayName());
    }
    for (UserGroupAccess userGroupAccess : object.getUserGroupAccesses()) {
        SharingUserGroupAccess sharingUserGroupAccess = new SharingUserGroupAccess();
        sharingUserGroupAccess.setId(userGroupAccess.getUserGroup().getUid());
        sharingUserGroupAccess.setName(userGroupAccess.getUserGroup().getDisplayName());
        sharingUserGroupAccess.setDisplayName(userGroupAccess.getUserGroup().getDisplayName());
        sharingUserGroupAccess.setAccess(userGroupAccess.getAccess());
        sharing.getObject().getUserGroupAccesses().add(sharingUserGroupAccess);
    }
    for (UserAccess userAccess : object.getUserAccesses()) {
        SharingUserAccess sharingUserAccess = new SharingUserAccess();
        sharingUserAccess.setId(userAccess.getUser().getUid());
        sharingUserAccess.setName(userAccess.getUser().getDisplayName());
        sharingUserAccess.setDisplayName(userAccess.getUser().getDisplayName());
        sharingUserAccess.setAccess(userAccess.getAccess());
        sharing.getObject().getUserAccesses().add(sharingUserAccess);
    }
    sharing.getObject().getUserGroupAccesses().sort(SharingUserGroupAccessNameComparator.INSTANCE);
    response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    renderService.toJson(response.getOutputStream(), sharing);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Sharing(org.hisp.dhis.webapi.webdomain.sharing.Sharing) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) UserAccess(org.hisp.dhis.user.UserAccess) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with SharingUserAccess

use of org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess in project dhis2-core by dhis2.

the class SharingController method getSharingUser.

private List<SharingUserAccess> getSharingUser(String key, int max) {
    List<SharingUserAccess> sharingUsers = new ArrayList<>();
    List<User> users = userService.getAllUsersBetweenByName(key, 0, max);
    for (User user : users) {
        SharingUserAccess sharingUserAccess = new SharingUserAccess();
        sharingUserAccess.setId(user.getUid());
        sharingUserAccess.setName(user.getDisplayName());
        sharingUserAccess.setDisplayName(user.getDisplayName());
        sharingUserAccess.setUsername(user.getUsername());
        sharingUsers.add(sharingUserAccess);
    }
    return sharingUsers;
}
Also used : User(org.hisp.dhis.user.User) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) ArrayList(java.util.ArrayList)

Example 4 with SharingUserAccess

use of org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess in project dhis2-core by dhis2.

the class SharingController method getSharing.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@GetMapping(produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Sharing> getSharing(@RequestParam String type, @RequestParam String id) throws WebMessageException {
    if (!aclService.isShareable(type)) {
        throw new WebMessageException(conflict("Type " + type + " is not supported."));
    }
    Class<? extends IdentifiableObject> klass = aclService.classForType(type);
    IdentifiableObject object = manager.getNoAcl(klass, id);
    if (object == null) {
        throw new WebMessageException(notFound("Object of type " + type + " with ID " + id + " was not found."));
    }
    User user = currentUserService.getCurrentUser();
    if (!aclService.canRead(user, object)) {
        throw new AccessDeniedException("You do not have manage access to this object.");
    }
    Sharing sharing = new Sharing();
    sharing.getMeta().setAllowPublicAccess(aclService.canMakePublic(user, object));
    sharing.getMeta().setAllowExternalAccess(aclService.canMakeExternal(user, object));
    sharing.getObject().setId(object.getUid());
    sharing.getObject().setName(object.getDisplayName());
    sharing.getObject().setDisplayName(object.getDisplayName());
    sharing.getObject().setExternalAccess(object.getExternalAccess());
    if (object.getPublicAccess() == null) {
        String access;
        if (aclService.canMakeClassPublic(user, klass)) {
            access = AccessStringHelper.newInstance().enable(AccessStringHelper.Permission.READ).enable(AccessStringHelper.Permission.WRITE).build();
        } else {
            access = AccessStringHelper.newInstance().build();
        }
        sharing.getObject().setPublicAccess(access);
    } else {
        sharing.getObject().setPublicAccess(object.getPublicAccess());
    }
    if (object.getCreatedBy() != null) {
        sharing.getObject().getUser().setId(object.getCreatedBy().getUid());
        sharing.getObject().getUser().setName(object.getCreatedBy().getDisplayName());
    }
    for (org.hisp.dhis.user.UserGroupAccess userGroupAccess : SharingUtils.getDtoUserGroupAccesses(object.getUserGroupAccesses(), object.getSharing())) {
        String userGroupDisplayName = userGroupService.getDisplayName(userGroupAccess.getId());
        if (userGroupDisplayName == null) {
            continue;
        }
        SharingUserGroupAccess sharingUserGroupAccess = new SharingUserGroupAccess();
        sharingUserGroupAccess.setId(userGroupAccess.getId());
        sharingUserGroupAccess.setName(userGroupDisplayName);
        sharingUserGroupAccess.setDisplayName(userGroupDisplayName);
        sharingUserGroupAccess.setAccess(userGroupAccess.getAccess());
        sharing.getObject().getUserGroupAccesses().add(sharingUserGroupAccess);
    }
    for (org.hisp.dhis.user.UserAccess userAccess : SharingUtils.getDtoUserAccesses(object.getUserAccesses(), object.getSharing())) {
        String userDisplayName = userService.getDisplayName(userAccess.getUid());
        if (userDisplayName == null)
            continue;
        SharingUserAccess sharingUserAccess = new SharingUserAccess();
        sharingUserAccess.setId(userAccess.getId());
        sharingUserAccess.setName(userDisplayName);
        sharingUserAccess.setDisplayName(userDisplayName);
        sharingUserAccess.setAccess(userAccess.getAccess());
        sharing.getObject().getUserAccesses().add(sharingUserAccess);
    }
    sharing.getObject().getUserGroupAccesses().sort(SharingUserGroupAccessNameComparator.INSTANCE);
    return ResponseEntity.ok().cacheControl(noCache()).body(sharing);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Sharing(org.hisp.dhis.webapi.webdomain.sharing.Sharing) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with SharingUserAccess

use of org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess 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");
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) User(org.hisp.dhis.user.User) Sharing(org.hisp.dhis.webapi.webdomain.sharing.Sharing) UserAccess(org.hisp.dhis.user.sharing.UserAccess) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) Schema(org.hisp.dhis.schema.Schema) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) SystemDefaultMetadataObject(org.hisp.dhis.common.SystemDefaultMetadataObject) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) UserGroup(org.hisp.dhis.user.UserGroup) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

User (org.hisp.dhis.user.User)5 SharingUserAccess (org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess)5 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)4 Sharing (org.hisp.dhis.webapi.webdomain.sharing.Sharing)4 SharingUserGroupAccess (org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess)4 AccessDeniedException (org.springframework.security.access.AccessDeniedException)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 UserAccess (org.hisp.dhis.user.UserAccess)2 UserGroup (org.hisp.dhis.user.UserGroup)2 UserGroupAccess (org.hisp.dhis.user.UserGroupAccess)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ArrayList (java.util.ArrayList)1 SystemDefaultMetadataObject (org.hisp.dhis.common.SystemDefaultMetadataObject)1 Schema (org.hisp.dhis.schema.Schema)1 UserAccess (org.hisp.dhis.user.sharing.UserAccess)1 UserGroupAccess (org.hisp.dhis.user.sharing.UserGroupAccess)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1