Search in sources :

Example 1 with WebMessageUtils.ok

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok in project dhis2-core by dhis2.

the class KeyJsonValueController method updateKeyJsonValue.

/**
     * Update a key in the given namespace.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws WebMessageException, IOException {
    if (!hasAccess(namespace)) {
        throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
    }
    KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
    if (keyJsonValue == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
    }
    if (!renderService.isValidJson(body)) {
        throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
    }
    keyJsonValue.setValue(body);
    keyJsonValueService.updateKeyJsonValue(keyJsonValue);
    response.setStatus(HttpServletResponse.SC_OK);
    messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' updated."), response);
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with WebMessageUtils.ok

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok 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 3 with WebMessageUtils.ok

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok in project dhis2-core by dhis2.

the class SystemSettingController method setSystemSetting.

@RequestMapping(value = "/{key}", method = RequestMethod.POST, consumes = { ContextUtils.CONTENT_TYPE_TEXT, ContextUtils.CONTENT_TYPE_HTML })
@PreAuthorize("hasRole('ALL') or hasRole('F_SYSTEM_SETTING')")
public void setSystemSetting(@PathVariable(value = "key") String key, @RequestParam(value = "value", required = false) String value, @RequestBody(required = false) String valuePayload, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    if (key == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Key must be specified"));
    }
    if (value == null && valuePayload == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Value must be specified as query param or as payload"));
    }
    value = ObjectUtils.firstNonNull(value, valuePayload);
    Serializable valueObject = SettingKey.getAsRealClass(key, value);
    systemSettingManager.saveSystemSetting(key, valueObject);
    webMessageService.send(WebMessageUtils.ok("System setting " + key + " set to value '" + valueObject + "'."), response, request);
}
Also used : Serializable(java.io.Serializable) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with WebMessageUtils.ok

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok in project dhis2-core by dhis2.

the class UserKeyJsonValueController method deleteUserKeyJsonValue.

/**
     * Delete a key.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json")
public void deleteUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws WebMessageException {
    UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key);
    if (userKeyJsonValue == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
    }
    userKeyJsonValueService.deleteUserKeyJsonValue(userKeyJsonValue);
    messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' deleted from the namespace '" + namespace + "'."), response);
}
Also used : UserKeyJsonValue(org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with WebMessageUtils.ok

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok in project dhis2-core by dhis2.

the class EventController method putJsonEventForEventDate.

@RequestMapping(value = "/{uid}/eventDate", method = RequestMethod.PUT, consumes = "application/json")
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')")
public void putJsonEventForEventDate(HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, ImportOptions importOptions) throws IOException, WebMessageException {
    if (!programStageInstanceService.programStageInstanceExists(uid)) {
        throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + uid));
    }
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    Event updatedEvent = renderService.fromJson(inputStream, Event.class);
    updatedEvent.setEvent(uid);
    eventService.updateEventForEventDate(updatedEvent);
    webMessageService.send(WebMessageUtils.ok("Event updated " + uid), response, request);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InputStream(java.io.InputStream) Event(org.hisp.dhis.dxf2.events.event.Event) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RequestMapping (org.springframework.web.bind.annotation.RequestMapping)19 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)17 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 Dashboard (org.hisp.dhis.dashboard.Dashboard)3 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)3 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)3 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)2 Event (org.hisp.dhis.dxf2.events.event.Event)2 KeyJsonValue (org.hisp.dhis.keyjsonvalue.KeyJsonValue)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 BulkSmsGatewayConfig (org.hisp.dhis.sms.config.BulkSmsGatewayConfig)2 SmsGatewayConfig (org.hisp.dhis.sms.config.SmsGatewayConfig)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 Semver (com.vdurmont.semver4j.Semver)1 Map (java.util.Map)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 MinMaxDataElement (org.hisp.dhis.minmax.MinMaxDataElement)1