Search in sources :

Example 16 with CurrentUser

use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.

the class MessageConversationController method markMessageConversationFollowup.

// --------------------------------------------------------------------------
// Mark conversations for follow up
// --------------------------------------------------------------------------
@PostMapping(value = "followup", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode markMessageConversationFollowup(@RequestParam(value = "user", required = false) String userUid, @RequestBody List<String> uids, HttpServletResponse response, @CurrentUser User currentUser) {
    RootNode responseNode = new RootNode("response");
    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("markedFollowup"));
    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) CurrentUser(org.hisp.dhis.user.CurrentUser) 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) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 17 with CurrentUser

use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.

the class MessageConversationController method modifyMessageConversationRead.

/**
 * Internal handler for setting the read property of MessageConversation.
 *
 * @param readValue true when setting as read, false when setting unread.
 */
private RootNode modifyMessageConversationRead(String userUid, List<String> uids, HttpServletResponse response, boolean readValue, User currentUser) {
    RootNode responseNode = new RootNode("response");
    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 IDs."));
        return responseNode;
    }
    CollectionNode marked = responseNode.addChild(new CollectionNode(readValue ? "markedRead" : "markedUnread"));
    marked.setWrapping(false);
    for (org.hisp.dhis.message.MessageConversation conversation : messageConversations) {
        boolean success = (readValue ? conversation.markRead(user) : conversation.markUnread(user));
        if (success) {
            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) CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation)

Example 18 with CurrentUser

use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.

the class MeController method updateCurrentUser.

@PutMapping(value = "", consumes = APPLICATION_JSON_VALUE)
public void updateCurrentUser(HttpServletRequest request, HttpServletResponse response, @CurrentUser(required = true) User currentUser) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    User user = renderService.fromJson(request.getInputStream(), User.class);
    merge(currentUser, user);
    if (user.getWhatsApp() != null && !ValidationUtils.validateWhatsapp(user.getWhatsApp())) {
        throw new WebMessageException(conflict("Invalid format for WhatsApp value '" + user.getWhatsApp() + "'"));
    }
    manager.update(currentUser);
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    CollectionNode collectionNode = oldFieldFilterService.toCollectionNode(User.class, new org.hisp.dhis.fieldfilter.FieldFilterParams(Collections.singletonList(currentUser), fields));
    response.setContentType(APPLICATION_JSON_VALUE);
    nodeService.serialize(NodeUtils.createRootNode(collectionNode.getChildren().get(0)), APPLICATION_JSON_VALUE, response.getOutputStream());
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 19 with CurrentUser

use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.

the class UserController method putJsonObject.

@Override
@PutMapping(value = "/{uid}", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage putJsonObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    User parsed = renderService.fromJson(request.getInputStream(), getEntityClass());
    populateUserCredentialsDtoFields(parsed);
    return importReport(updateUser(pvUid, parsed)).withPlainResponseBefore(DhisApiVersion.V38);
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 20 with CurrentUser

use of org.hisp.dhis.user.CurrentUser in project dhis2-core by dhis2.

the class UserRoleController method removeUserFromRole.

@DeleteMapping("/{id}/users/{userId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removeUserFromRole(@PathVariable(value = "id") String pvId, @PathVariable("userId") String pvUserId, @CurrentUser User currentUser, HttpServletResponse response) throws WebMessageException {
    UserAuthorityGroup userAuthorityGroup = userService.getUserAuthorityGroup(pvId);
    if (userAuthorityGroup == null) {
        throw new WebMessageException(notFound("UserRole does not exist: " + pvId));
    }
    User user = userService.getUser(pvUserId);
    if (user == null) {
        throw new WebMessageException(notFound("User does not exist: " + pvId));
    }
    if (!aclService.canUpdate(currentUser, userAuthorityGroup)) {
        throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
    }
    if (user.getUserAuthorityGroups().contains(userAuthorityGroup)) {
        user.getUserAuthorityGroups().remove(userAuthorityGroup);
        userService.updateUser(user);
    }
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Aggregations

CurrentUser (org.hisp.dhis.user.CurrentUser)21 User (org.hisp.dhis.user.User)21 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)9 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)8 CollectionNode (org.hisp.dhis.node.types.CollectionNode)8 RootNode (org.hisp.dhis.node.types.RootNode)8 SimpleNode (org.hisp.dhis.node.types.SimpleNode)8 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)7 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)2 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)2 Enums (com.google.common.base.Enums)2 Joiner (com.google.common.base.Joiner)2 Optional (com.google.common.base.Optional)2 Lists (com.google.common.collect.Lists)2 IOException (java.io.IOException)2