Search in sources :

Example 81 with WebMessageException

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

the class MeController method getSetting.

@RequestMapping(value = "/settings/{key}")
public void getSetting(HttpServletResponse response, @PathVariable String key) throws IOException, WebMessageException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
    if (!keyEnum.isPresent()) {
        throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
    }
    Serializable value = userSettingService.getUserSetting(keyEnum.get(), currentUser);
    if (value == null) {
        throw new WebMessageException(WebMessageUtils.notFound("User setting not found for key: " + key));
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), value);
}
Also used : Serializable(java.io.Serializable) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException)

Example 82 with WebMessageException

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

the class UserController method putJsonObject.

@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
public void putJsonObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
    if (users.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
    }
    User currentUser = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(currentUser, users.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
    }
    User parsed = renderService.fromJson(request.getInputStream(), getEntityClass());
    parsed.setUid(pvUid);
    if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
        throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    params.setImportReportMode(ImportReportMode.FULL);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
        User user = userService.getUser(pvUid);
        userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
    }
    renderService.toJson(response.getOutputStream(), importReport);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 83 with WebMessageException

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

the class UserController method putXmlObject.

// -------------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------------
@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" })
public void putXmlObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<User> users = getEntity(pvUid, NO_WEB_OPTIONS);
    if (users.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict(getEntityName() + " does not exist: " + pvUid));
    }
    User currentUser = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(currentUser, users.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this user.");
    }
    User parsed = renderService.fromXml(request.getInputStream(), getEntityClass());
    parsed.setUid(pvUid);
    if (!userService.canAddOrUpdateUser(IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser)) {
        throw new WebMessageException(WebMessageUtils.conflict("You must have permissions to create user, or ability to manage at least one user group for the user."));
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    params.setImportReportMode(ImportReportMode.FULL);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() == Status.OK && importReport.getStats().getUpdated() == 1) {
        User user = userService.getUser(pvUid);
        userGroupService.updateUserGroups(user, IdentifiableObjectUtils.getUids(parsed.getGroups()), currentUser);
    }
    renderService.toXml(response.getOutputStream(), importReport);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 84 with WebMessageException

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

the class UserController method validateInviteUser.

/**
     * Validates whether a user can be invited / created.
     *
     * @param user the user.
     */
private boolean validateInviteUser(User user, User currentUser) throws WebMessageException {
    if (!validateCreateUser(user, currentUser)) {
        return false;
    }
    UserCredentials credentials = user.getUserCredentials();
    if (credentials == null) {
        throw new WebMessageException(WebMessageUtils.conflict("User credentials is not present"));
    }
    credentials.setUserInfo(user);
    String valid = securityService.validateInvite(user.getUserCredentials());
    if (valid != null) {
        throw new WebMessageException(WebMessageUtils.conflict(valid + ": " + user.getUserCredentials()));
    }
    return true;
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UserCredentials(org.hisp.dhis.user.UserCredentials)

Example 85 with WebMessageException

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

the class LockExceptionController method addLockException.

@RequestMapping(method = RequestMethod.POST)
public void addLockException(@RequestParam("ou") String organisationUnitId, @RequestParam("pe") String periodId, @RequestParam("ds") String dataSetId, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    User user = userService.getCurrentUser();
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
    if (dataSet == null || period == null) {
        throw new WebMessageException(WebMessageUtils.conflict(" DataSet or Period is invalid"));
    }
    if (!aclService.canUpdate(user, dataSet)) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to update this object");
    }
    boolean created = false;
    List<String> listOrgUnitIds = new ArrayList<>();
    if (organisationUnitId.startsWith("[") && organisationUnitId.endsWith("]")) {
        String[] arrOrgUnitIds = organisationUnitId.substring(1, organisationUnitId.length() - 1).split(",");
        Collections.addAll(listOrgUnitIds, arrOrgUnitIds);
    } else {
        listOrgUnitIds.add(organisationUnitId);
    }
    if (listOrgUnitIds.size() == 0) {
        throw new WebMessageException(WebMessageUtils.conflict(" OrganisationUnit ID is invalid."));
    }
    for (String id : listOrgUnitIds) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(id);
        if (organisationUnit == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Can't find OrganisationUnit with id =" + id));
        }
        if (organisationUnit.getDataSets().contains(dataSet)) {
            LockException lockException = new LockException();
            lockException.setOrganisationUnit(organisationUnit);
            lockException.setDataSet(dataSet);
            lockException.setPeriod(period);
            dataSetService.addLockException(lockException);
            created = true;
        }
    }
    if (created) {
        webMessageService.send(WebMessageUtils.created("LockException created successfully."), response, request);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) LockException(org.hisp.dhis.dataset.LockException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)134 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)118 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)31 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)28 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)27 DataSet (org.hisp.dhis.dataset.DataSet)21 Period (org.hisp.dhis.period.Period)21 User (org.hisp.dhis.user.User)20 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)18 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)15 ArrayList (java.util.ArrayList)14 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)14 Interpretation (org.hisp.dhis.interpretation.Interpretation)13 Date (java.util.Date)9 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)9 InputStream (java.io.InputStream)8 Grid (org.hisp.dhis.common.Grid)8 Event (org.hisp.dhis.dxf2.events.event.Event)8 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)8 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)8