Search in sources :

Example 6 with UpdateAccessDeniedException

use of org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException in project dhis2-core by dhis2.

the class MessageConversationController method unmarkMessageConversationFollowup.

//--------------------------------------------------------------------------
// Clear follow up
//--------------------------------------------------------------------------
@RequestMapping(value = "unfollowup", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode unmarkMessageConversationFollowup(@RequestParam(value = "user", required = false) String userUid, @RequestBody List<String> uids, HttpServletResponse response) {
    RootNode responseNode = new RootNode("response");
    User currentUser = currentUserService.getCurrentUser();
    User user = userUid != null ? userService.getUser(userUid) : currentUser;
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
        return responseNode;
    }
    if (!canModifyUserConversation(currentUser, user)) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, uids);
    if (messageConversations.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given UIDs"));
        return responseNode;
    }
    CollectionNode marked = responseNode.addChild(new CollectionNode("unmarkedFollowup"));
    marked.setWrapping(false);
    for (org.hisp.dhis.message.MessageConversation conversation : messageConversations) {
        if (conversation.isFollowUp()) {
            conversation.toggleFollowUp(user);
            messageService.updateMessageConversation(conversation);
        }
        marked.addChild(new SimpleNode("uid", conversation.getUid()));
    }
    response.setStatus(HttpServletResponse.SC_OK);
    return responseNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with UpdateAccessDeniedException

use of org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException in project dhis2-core by dhis2.

the class MessageConversationController method removeUserAssigned.

//--------------------------------------------------------------------------
// Remove assigned user
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/assign", method = RequestMethod.DELETE, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode removeUserAssigned(@PathVariable String uid, HttpServletResponse response) {
    RootNode responseNode = new RootNode("response");
    User user = currentUserService.getCurrentUser();
    if (!canModifyUserConversation(user, user) && (messageService.hasAccessToManageFeedbackMessages(user))) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(uid);
    if (messageConversation == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversation found for the given ID."));
        return responseNode;
    }
    messageConversation.setAssignee(null);
    messageService.updateMessageConversation(messageConversation);
    responseNode.addChild(new SimpleNode("message", "Message is no longer assigned to user"));
    response.setStatus(HttpServletResponse.SC_OK);
    return responseNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) SimpleNode(org.hisp.dhis.node.types.SimpleNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with UpdateAccessDeniedException

use of org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException in project dhis2-core by dhis2.

the class DefaultCollectionService method delCollectionItems.

@Override
@SuppressWarnings("unchecked")
public void delCollectionItems(IdentifiableObject object, String propertyName, List<IdentifiableObject> objects) throws Exception {
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), object)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    if (!schema.haveProperty(propertyName)) {
        throw new WebMessageException(WebMessageUtils.notFound("Property " + propertyName + " does not exist on " + object.getClass().getName()));
    }
    Property property = schema.getProperty(propertyName);
    if (!property.isCollection() || !property.isIdentifiableObject()) {
        throw new WebMessageException(WebMessageUtils.conflict("Only identifiable object collections can be removed from."));
    }
    Collection<String> itemCodes = objects.stream().map(IdentifiableObject::getUid).collect(Collectors.toList());
    if (itemCodes.isEmpty()) {
        return;
    }
    List<? extends IdentifiableObject> items = manager.get(((Class<? extends IdentifiableObject>) property.getItemKlass()), itemCodes);
    manager.refresh(object);
    if (property.isOwner()) {
        Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) property.getGetterMethod().invoke(object);
        for (IdentifiableObject item : items) {
            if (collection.contains(item))
                collection.remove(item);
        }
    } else {
        Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
        Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
        for (IdentifiableObject item : items) {
            try {
                Collection<IdentifiableObject> collection = (Collection<IdentifiableObject>) owningProperty.getGetterMethod().invoke(item);
                if (collection.contains(object)) {
                    collection.remove(object);
                    manager.update(item);
                }
            } catch (Exception ex) {
            }
        }
    }
    manager.update(object);
    dbmsManager.clearSession();
    cacheManager.clearCache();
}
Also used : UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Schema(org.hisp.dhis.schema.Schema) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 9 with UpdateAccessDeniedException

use of org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException 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 10 with UpdateAccessDeniedException

use of org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException in project dhis2-core by dhis2.

the class MessageConversationController method setUserAssigned.

//--------------------------------------------------------------------------
// Assign user
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/assign", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode setUserAssigned(@PathVariable String uid, @RequestParam(required = false) String userId, HttpServletResponse response) {
    RootNode responseNode = new RootNode("response");
    User user = currentUserService.getCurrentUser();
    if (!canModifyUserConversation(user, user) && (messageService.hasAccessToManageFeedbackMessages(user))) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(uid);
    if (messageConversation == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversation found for the given ID."));
        return responseNode;
    }
    User userToAssign;
    if ((userToAssign = userService.getUser(userId)) == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "Could not find user to assign"));
        return responseNode;
    }
    if (!configurationService.isUserInFeedbackRecipientUserGroup(userToAssign)) {
        response.setStatus(HttpServletResponse.SC_CONFLICT);
        responseNode.addChild(new SimpleNode("message", "User provided is not a member of the system's feedback recipient group"));
        return responseNode;
    }
    messageConversation.setAssignee(userToAssign);
    messageService.updateMessageConversation(messageConversation);
    responseNode.addChild(new SimpleNode("message", "User " + userToAssign.getName() + " was assigned to ticket"));
    response.setStatus(HttpServletResponse.SC_OK);
    return responseNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) SimpleNode(org.hisp.dhis.node.types.SimpleNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)17 User (org.hisp.dhis.user.User)14 Dashboard (org.hisp.dhis.dashboard.Dashboard)7 RootNode (org.hisp.dhis.node.types.RootNode)7 SimpleNode (org.hisp.dhis.node.types.SimpleNode)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)5 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)5 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)5 CollectionNode (org.hisp.dhis.node.types.CollectionNode)5 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)4 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)4 Property (org.hisp.dhis.schema.Property)4 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)3 Schema (org.hisp.dhis.schema.Schema)3 MessageConversation (org.hisp.dhis.webapi.webdomain.MessageConversation)3 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2