use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict 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.dxf2.webmessage.WebMessageUtils.conflict 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);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict 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);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class UserKeyJsonValueController method addUserKeyJsonValue.
/**
* Creates a new KeyJsonValue Object on the current user with the key, namespace and value supplied.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public void addUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, @RequestParam(defaultValue = "false") boolean encrypt, HttpServletResponse response) throws IOException, WebMessageException {
if (userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key) != null) {
throw new WebMessageException(WebMessageUtils.conflict("The key '" + key + "' already exists in the namespace '" + namespace + "'."));
}
if (!renderService.isValidJson(body)) {
throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
}
UserKeyJsonValue userKeyJsonValue = new UserKeyJsonValue();
userKeyJsonValue.setKey(key);
userKeyJsonValue.setUser(currentUserService.getCurrentUser());
userKeyJsonValue.setNamespace(namespace);
userKeyJsonValue.setValue(body);
userKeyJsonValue.setEncrypted(encrypt);
userKeyJsonValueService.addUserKeyJsonValue(userKeyJsonValue);
response.setStatus(HttpServletResponse.SC_CREATED);
messageService.sendJson(WebMessageUtils.created("Key '" + key + "' in namespace '" + namespace + "' created."), response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict in project dhis2-core by dhis2.
the class EventController method getCsvEvents.
@RequestMapping(value = "", method = RequestMethod.GET, produces = { "application/csv", "application/csv+gzip", "text/csv" })
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
public void getCsvEvents(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false) String order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false, defaultValue = "false") boolean skipHeader, IdSchemes idSchemes, HttpServletResponse response, HttpServletRequest request) throws IOException, WebMessageException {
boolean allowNoAttrOptionCombo = trackedEntityInstance != null && entityInstanceService.getTrackedEntityInstance(trackedEntityInstance) != null;
DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, allowNoAttrOptionCombo);
if (attributeOptionCombo == null && !allowNoAttrOptionCombo) {
throw new WebMessageException(WebMessageUtils.conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
}
lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
EventSearchParams params = eventService.getFromUrl(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, getOrderParams(order), null, false, null, null, null, includeDeleted);
Events events = eventService.getEvents(params);
OutputStream outputStream = response.getOutputStream();
response.setContentType("application/csv");
if (ContextUtils.isAcceptCsvGzip(request)) {
response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
outputStream = new GZIPOutputStream(outputStream);
response.setContentType("application/csv+gzip");
}
if (!StringUtils.isEmpty(attachment)) {
response.addHeader("Content-Disposition", "attachment; filename=" + attachment);
}
csvEventService.writeEvents(outputStream, events, !skipHeader);
}
Aggregations