Search in sources :

Example 1 with CurrentUser

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

the class MessageConversationController method removeUserFromMessageConversations.

// --------------------------------------------------------------------------
// Remove a user from one or more MessageConversations (batch operation)
// --------------------------------------------------------------------------
@DeleteMapping(produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode removeUserFromMessageConversations(@RequestParam("mc") List<String> mcUids, @RequestParam(value = "user", required = false) String userUid, HttpServletResponse response, @CurrentUser User currentUser) throws DeleteAccessDeniedException {
    RootNode responseNode = new RootNode("response");
    User user = userUid == null ? currentUser : userService.getUser(userUid);
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "User does not exist: " + userUid));
        return responseNode;
    }
    if (!canModifyUserConversation(currentUser, user)) {
        throw new DeleteAccessDeniedException("Not authorized to modify user: " + user.getUid());
    }
    Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, mcUids);
    if (messageConversations.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given UIDs."));
        return responseNode;
    }
    CollectionNode removed = responseNode.addChild(new CollectionNode("removed"));
    for (org.hisp.dhis.message.MessageConversation mc : messageConversations) {
        if (mc.remove(user)) {
            messageService.updateMessageConversation(mc);
            removed.addChild(new SimpleNode("uid", mc.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) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with CurrentUser

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

the class MessageConversationController method removeUserFromMessageConversation.

// --------------------------------------------------------------------------
// Remove a user from a MessageConversation
// In practice a DELETE on MessageConversation <-> User relationship
// --------------------------------------------------------------------------
@DeleteMapping(value = "/{mc-uid}/{user-uid}", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode removeUserFromMessageConversation(@PathVariable(value = "mc-uid") String mcUid, @PathVariable(value = "user-uid") String userUid, @CurrentUser User currentUser, HttpServletResponse response) throws DeleteAccessDeniedException {
    RootNode responseNode = new RootNode("reply");
    User user = userService.getUser(userUid);
    if (user == null) {
        responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return responseNode;
    }
    if (!canModifyUserConversation(currentUser, user)) {
        throw new DeleteAccessDeniedException("Not authorized to modify user: " + user.getUid());
    }
    org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(mcUid);
    if (messageConversation == null) {
        responseNode.addChild(new SimpleNode("message", "No messageConversation with uid: " + mcUid));
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return responseNode;
    }
    CollectionNode removed = responseNode.addChild(new CollectionNode("removed"));
    if (messageConversation.remove(user)) {
        messageService.updateMessageConversation(messageConversation);
        removed.addChild(new SimpleNode("uid", messageConversation.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) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with CurrentUser

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

the class MessageConversationController method unmarkMessageConversationFollowup.

// --------------------------------------------------------------------------
// Clear follow up
// --------------------------------------------------------------------------
@PostMapping(value = "unfollowup", 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, @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("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) 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 4 with CurrentUser

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

the class OrganisationUnitController method getEntityList.

@Override
@SuppressWarnings("unchecked")
protected List<OrganisationUnit> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    List<OrganisationUnit> objects = Lists.newArrayList();
    User currentUser = currentUserService.getCurrentUser();
    boolean anySpecialPropertySet = ObjectUtils.anyIsTrue(options.isTrue("userOnly"), options.isTrue("userDataViewOnly"), options.isTrue("userDataViewFallback"), options.isTrue("levelSorted"));
    boolean anyQueryPropertySet = ObjectUtils.firstNonNull(options.get("query"), options.getInt("level"), options.getInt("maxLevel")) != null || options.isTrue("withinUserHierarchy") || options.isTrue("withinUserSearchHierarchy");
    String memberObject = options.get("memberObject");
    String memberCollection = options.get("memberCollection");
    if (options.isTrue("userOnly")) {
        objects = new ArrayList<>(currentUser.getOrganisationUnits());
    } else if (options.isTrue("userDataViewOnly")) {
        objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
    } else if (options.isTrue("userDataViewFallback")) {
        if (currentUser.hasDataViewOrganisationUnit()) {
            objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
        } else {
            objects = organisationUnitService.getOrganisationUnitsAtLevel(1);
        }
    } else if (options.isTrue("levelSorted")) {
        objects = new ArrayList<>(manager.getAll(getEntityClass()));
        objects.sort(OrganisationUnitByLevelComparator.INSTANCE);
    } else if (anyQueryPropertySet) {
        OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
        params.setQuery(options.get("query"));
        params.setLevel(options.getInt("level"));
        params.setMaxLevels(options.getInt("maxLevel"));
        params.setParents(options.isTrue("withinUserHierarchy") ? currentUser.getOrganisationUnits() : options.isTrue("withinUserSearchHierarchy") ? currentUser.getTeiSearchOrganisationUnitsWithFallback() : Sets.newHashSet());
        objects = organisationUnitService.getOrganisationUnitsByQuery(params);
    }
    // ---------------------------------------------------------------------
    // Standard Query handling
    // ---------------------------------------------------------------------
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, getPaginationData(options), options.getRootJunction());
    query.setUser(currentUser);
    query.setDefaultOrder();
    query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
    if (anySpecialPropertySet || anyQueryPropertySet) {
        query.setObjects(objects);
    }
    List<OrganisationUnit> list = (List<OrganisationUnit>) queryService.query(query);
    // ---------------------------------------------------------------------
    // Collection member count in hierarchy handling
    // ---------------------------------------------------------------------
    IdentifiableObject member;
    if (memberObject != null && memberCollection != null && (member = manager.get(memberObject)) != null) {
        for (OrganisationUnit unit : list) {
            Long count = organisationUnitService.getOrganisationUnitHierarchyMemberCount(unit, member, memberCollection);
            unit.setMemberCount((count != null ? count.intValue() : 0));
        }
    }
    return list;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) OrgUnitSplitQuery(org.hisp.dhis.split.orgunit.OrgUnitSplitQuery) OrgUnitMergeQuery(org.hisp.dhis.merge.orgunit.OrgUnitMergeQuery) ArrayList(java.util.ArrayList) OrganisationUnitQueryParams(org.hisp.dhis.organisationunit.OrganisationUnitQueryParams) List(java.util.List) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 5 with CurrentUser

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

the class SmsInboundController method getUserByPhoneNumber.

// -------------------------------------------------------------------------
// SUPPORTIVE METHOD
// -------------------------------------------------------------------------
private User getUserByPhoneNumber(String phoneNumber, String text, User currentUser) throws WebMessageException {
    SMSCommand unregisteredParser = smsCommandService.getSMSCommand(SmsUtils.getCommandString(text), ParserType.UNREGISTERED_PARSER);
    List<User> users = userService.getUsersByPhoneNumber(phoneNumber);
    if (SmsUtils.isBase64(text)) {
        return handleCompressedCommands(currentUser, phoneNumber);
    }
    if (users == null || users.isEmpty()) {
        if (unregisteredParser != null) {
            return null;
        }
        // No user belong to this phone number
        throw new WebMessageException(conflict("User's phone number is not registered in the system"));
    }
    return users.iterator().next();
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SMSCommand(org.hisp.dhis.sms.command.SMSCommand)

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