Search in sources :

Example 11 with NotAuthenticatedException

use of org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException in project dhis2-core by dhis2.

the class MeController method updateCurrentUser.

@RequestMapping(value = "", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateCurrentUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    User user = renderService.fromJson(request.getInputStream(), User.class);
    merge(currentUser, user);
    if (user.getUserCredentials() != null) {
        updatePassword(currentUser, user.getUserCredentials().getPassword());
    }
    manager.update(currentUser);
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    CollectionNode collectionNode = fieldFilterService.filter(User.class, Collections.singletonList(currentUser), fields);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    nodeService.serialize(NodeUtils.createRootNode(collectionNode.getChildren().get(0)), "application/json", response.getOutputStream());
}
Also used : NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 12 with NotAuthenticatedException

use of org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException 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 13 with NotAuthenticatedException

use of org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException in project dhis2-core by dhis2.

the class CurrentUserController method getInboxMessageConversations.

@RequestMapping(value = "/inbox/messageConversations", produces = { "application/json", "text/*" })
public void getInboxMessageConversations(HttpServletResponse response) throws Exception {
    User user = currentUserService.getCurrentUser();
    if (user == null) {
        throw new NotAuthenticatedException();
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<MessageConversation> messageConversations = new ArrayList<>(messageService.getMessageConversations(0, MAX_OBJECTS));
    for (org.hisp.dhis.message.MessageConversation messageConversation : messageConversations) {
        messageConversation.setAccess(aclService.getAccess(messageConversation, user));
    }
    renderService.toJson(response.getOutputStream(), messageConversations);
}
Also used : User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) MessageConversation(org.hisp.dhis.message.MessageConversation) ArrayList(java.util.ArrayList) MessageConversation(org.hisp.dhis.message.MessageConversation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with NotAuthenticatedException

use of org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException in project dhis2-core by dhis2.

the class CurrentUserController method getPrograms.

@RequestMapping(value = { "/assignedPrograms", "/programs" }, produces = { "application/json", "text/*" })
public void getPrograms(HttpServletResponse response, @RequestParam Map<String, String> parameters, @RequestParam(required = false) String type) throws IOException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Set<OrganisationUnit> userOrganisationUnits = new HashSet<>();
    Set<OrganisationUnit> organisationUnits = new HashSet<>();
    Set<Program> programs = new HashSet<>();
    Map<String, List<Program>> programAssociations = new HashMap<>();
    Set<Program> userPrograms;
    if (type == null) {
        userPrograms = programService.getUserPrograms();
    } else {
        userPrograms = programService.getUserPrograms(ProgramType.fromValue(type));
    }
    if (currentUserService.currentUserIsSuper() && currentUser.getOrganisationUnits().isEmpty()) {
        userOrganisationUnits.addAll(organisationUnitService.getRootOrganisationUnits());
    } else {
        userOrganisationUnits.addAll(currentUser.getOrganisationUnits());
    }
    if (parameters.containsKey("includeDescendants") && Boolean.parseBoolean(parameters.get("includeDescendants"))) {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnitService.getOrganisationUnitWithChildren(organisationUnit.getUid()));
        }
        userOrganisationUnits.addAll(children);
    } else {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnit.getChildren());
        }
        userOrganisationUnits.addAll(children);
    }
    for (OrganisationUnit organisationUnit : userOrganisationUnits) {
        List<Program> ouPrograms = new ArrayList<>(programService.getPrograms(organisationUnit));
        if (!ouPrograms.isEmpty()) {
            for (Program program : ouPrograms) {
                if (userPrograms.contains(program)) {
                    organisationUnits.add(organisationUnit);
                    programs.add(program);
                    programAssociations.putIfAbsent(organisationUnit.getUid(), new ArrayList<>());
                    programAssociations.get(organisationUnit.getUid()).add(program);
                }
            }
        }
    }
    Forms forms = new Forms();
    for (OrganisationUnit organisationUnit : organisationUnits) {
        FormOrganisationUnit formOrganisationUnit = new FormOrganisationUnit();
        formOrganisationUnit.setId(organisationUnit.getUid());
        formOrganisationUnit.setLabel(organisationUnit.getDisplayName());
        formOrganisationUnit.setLevel(organisationUnit.getLevel());
        if (organisationUnit.getParent() != null) {
            formOrganisationUnit.setParent(organisationUnit.getParent().getUid());
        }
        for (Program program : programAssociations.get(organisationUnit.getUid())) {
            FormProgram formProgram = new FormProgram();
            formProgram.setId(program.getUid());
            formProgram.setLabel(program.getDisplayName());
            formOrganisationUnit.getPrograms().add(formProgram);
        }
        forms.getOrganisationUnits().put(formOrganisationUnit.getId(), formOrganisationUnit);
    }
    for (Program program : programs) {
        forms.getForms().put(program.getUid(), FormUtils.fromProgram(program));
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), forms);
}
Also used : FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Forms(org.hisp.dhis.webapi.webdomain.Forms) FormProgram(org.hisp.dhis.webapi.webdomain.FormProgram) User(org.hisp.dhis.user.User) FormProgram(org.hisp.dhis.webapi.webdomain.FormProgram) Program(org.hisp.dhis.program.Program) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with NotAuthenticatedException

use of org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException in project dhis2-core by dhis2.

the class CurrentUserController method getCurrentUser.

@RequestMapping
@ResponseBody
public RootNode getCurrentUser(HttpServletResponse response) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    if (fields.isEmpty()) {
        fields.add(":all");
    }
    CollectionNode collectionNode = fieldFilterService.filter(User.class, Collections.singletonList(currentUser), fields);
    RootNode rootNode = new RootNode(collectionNode.getChildren().get(0));
    rootNode.setDefaultNamespace(DxfNamespaces.DXF_2_0);
    rootNode.setNamespace(DxfNamespaces.DXF_2_0);
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

NotAuthenticatedException (org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException)16 User (org.hisp.dhis.user.User)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)3 CollectionNode (org.hisp.dhis.node.types.CollectionNode)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 FormOrganisationUnit (org.hisp.dhis.webapi.webdomain.FormOrganisationUnit)3 Dashboard (org.hisp.dhis.webapi.webdomain.user.Dashboard)3 List (java.util.List)2 Interpretation (org.hisp.dhis.interpretation.Interpretation)2 MessageConversation (org.hisp.dhis.message.MessageConversation)2 RootNode (org.hisp.dhis.node.types.RootNode)2 Forms (org.hisp.dhis.webapi.webdomain.Forms)2 UserAccount (org.hisp.dhis.webapi.webdomain.user.UserAccount)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DataSet (org.hisp.dhis.dataset.DataSet)1