Search in sources :

Example 1 with NotAuthenticatedException

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

the class CurrentUserController method getInboxInterpretations.

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

Example 2 with NotAuthenticatedException

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

the class CurrentUserController method getAssignedOrganisationUnits.

@RequestMapping(value = { "/assignedOrganisationUnits", "/organisationUnits" }, produces = { "application/json", "text/*" })
public void getAssignedOrganisationUnits(HttpServletResponse response, @RequestParam Map<String, String> parameters) throws IOException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Set<OrganisationUnit> userOrganisationUnits = new HashSet<>();
    userOrganisationUnits.add(currentUser.getOrganisationUnit());
    if (parameters.containsKey("includeChildren") && Boolean.parseBoolean(parameters.get("includeChildren"))) {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnit.getChildren());
        }
        userOrganisationUnits.addAll(children);
    } else 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);
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), userOrganisationUnits);
}
Also used : FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with NotAuthenticatedException

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

the class CurrentUserController method postUserAccountJson.

@RequestMapping(value = { "/profile", "/user-account" }, method = RequestMethod.POST, consumes = "application/json")
public void postUserAccountJson(HttpServletResponse response, HttpServletRequest request) throws Exception {
    UserAccount userAccount = renderService.fromJson(request.getInputStream(), UserAccount.class);
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    // basic user account
    currentUser.setFirstName(userAccount.getFirstName());
    currentUser.setSurname(userAccount.getSurname());
    currentUser.setEmail(userAccount.getEmail());
    currentUser.setPhoneNumber(userAccount.getPhoneNumber());
    // profile
    currentUser.setIntroduction(userAccount.getIntroduction());
    currentUser.setJobTitle(userAccount.getJobTitle());
    currentUser.setGender(userAccount.getGender());
    if (userAccount.getBirthday() != null && !userAccount.getBirthday().isEmpty()) {
        currentUser.setBirthday(DateUtils.getMediumDate(userAccount.getBirthday()));
    }
    currentUser.setNationality(userAccount.getNationality());
    currentUser.setEmployer(userAccount.getEmployer());
    currentUser.setEducation(userAccount.getEducation());
    currentUser.setInterests(userAccount.getInterests());
    currentUser.setLanguages(userAccount.getLanguages());
    userService.updateUser(currentUser);
}
Also used : User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) UserAccount(org.hisp.dhis.webapi.webdomain.user.UserAccount) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with NotAuthenticatedException

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

the class CurrentUserController method getDataSets.

@RequestMapping(value = { "/assignedDataSets", "/dataSets" }, produces = { "application/json", "text/*" })
public void getDataSets(@RequestParam(defaultValue = "false") boolean optionSets, @RequestParam(defaultValue = "50") int maxOptions, HttpServletResponse response, @RequestParam Map<String, String> parameters) throws IOException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Forms forms = new Forms();
    Set<OrganisationUnit> organisationUnits = new HashSet<>();
    Set<DataSet> userDataSets;
    Set<OrganisationUnit> userOrganisationUnits = new HashSet<>(currentUser.getOrganisationUnits());
    if (currentUser.getUserCredentials().getAllAuthorities().contains("ALL")) {
        userDataSets = new HashSet<>(dataSetService.getAllDataSets());
        if (userOrganisationUnits.isEmpty()) {
            userOrganisationUnits = new HashSet<>(organisationUnitService.getRootOrganisationUnits());
        }
    } else {
        userDataSets = currentUser.getUserCredentials().getAllDataSets();
    }
    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 ou : userOrganisationUnits) {
        Set<DataSet> dataSets = new HashSet<>(Sets.intersection(ou.getDataSets(), userDataSets));
        if (dataSets.size() > 0) {
            organisationUnits.add(ou);
        }
    }
    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());
        }
        Set<DataSet> dataSets = new HashSet<>(Sets.intersection(organisationUnit.getDataSets(), userDataSets));
        for (DataSet dataSet : dataSets) {
            String uid = dataSet.getUid();
            FormDataSet formDataSet = new FormDataSet();
            formDataSet.setId(uid);
            formDataSet.setLabel(dataSet.getDisplayName());
            dataSet.getCategoryCombo().getCategories().forEach(cat -> {
                cat.setAccess(aclService.getAccess(cat, currentUser));
                cat.getCategoryOptions().forEach(catOpts -> catOpts.setAccess(aclService.getAccess(catOpts, currentUser)));
            });
            forms.getForms().put(uid, FormUtils.fromDataSet(dataSet, false, userOrganisationUnits));
            formOrganisationUnit.getDataSets().add(formDataSet);
            if (optionSets) {
                for (DataElement dataElement : dataSet.getDataElements()) {
                    if (dataElement.hasOptionSet()) {
                        int size = maxOptions;
                        if (size >= dataElement.getOptionSet().getOptions().size()) {
                            size = dataElement.getOptionSet().getOptions().size();
                        }
                        forms.getOptionSets().put(dataElement.getOptionSet().getUid(), dataElement.getOptionSet().getOptionValues().subList(0, size - 1));
                    }
                }
            }
        }
        forms.getOrganisationUnits().put(formOrganisationUnit.getId(), formOrganisationUnit);
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), forms);
}
Also used : Forms(org.hisp.dhis.webapi.webdomain.Forms) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) FormDataSet(org.hisp.dhis.webapi.webdomain.FormDataSet) ArrayList(java.util.ArrayList) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) FormDataSet(org.hisp.dhis.webapi.webdomain.FormDataSet) DataElement(org.hisp.dhis.dataelement.DataElement) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with NotAuthenticatedException

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

the class CurrentUserController method getUserAccount.

private UserAccount getUserAccount() throws NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    UserAccount userAccount = new UserAccount();
    // user account
    userAccount.setId(currentUser.getUid());
    userAccount.setUsername(currentUser.getUsername());
    userAccount.setFirstName(currentUser.getFirstName());
    userAccount.setSurname(currentUser.getSurname());
    userAccount.setEmail(currentUser.getEmail());
    userAccount.setPhoneNumber(currentUser.getPhoneNumber());
    // profile
    userAccount.setIntroduction(currentUser.getIntroduction());
    userAccount.setJobTitle(currentUser.getJobTitle());
    userAccount.setGender(currentUser.getGender());
    if (currentUser.getBirthday() != null) {
        userAccount.setBirthday(DateUtils.getMediumDateString(currentUser.getBirthday()));
    }
    userAccount.setNationality(currentUser.getNationality());
    userAccount.setEmployer(currentUser.getEmployer());
    userAccount.setEducation(currentUser.getEducation());
    userAccount.setInterests(currentUser.getInterests());
    userAccount.setLanguages(currentUser.getLanguages());
    userAccount.getSettings().put(UserSettingKey.UI_LOCALE.getName(), TextUtils.toString(userSettingService.getUserSetting(UserSettingKey.UI_LOCALE)));
    userAccount.getSettings().put(UserSettingKey.DB_LOCALE.getName(), TextUtils.toString(userSettingService.getUserSetting(UserSettingKey.DB_LOCALE)));
    userAccount.getSettings().put(UserSettingKey.MESSAGE_EMAIL_NOTIFICATION.getName(), TextUtils.toString(userSettingService.getUserSetting(UserSettingKey.MESSAGE_EMAIL_NOTIFICATION)));
    userAccount.getSettings().put(UserSettingKey.MESSAGE_SMS_NOTIFICATION.getName(), TextUtils.toString(userSettingService.getUserSetting(UserSettingKey.MESSAGE_SMS_NOTIFICATION)));
    userAccount.getSettings().put(UserSettingKey.ANALYSIS_DISPLAY_PROPERTY.getName(), TextUtils.toString(userSettingService.getUserSetting(UserSettingKey.ANALYSIS_DISPLAY_PROPERTY)));
    return userAccount;
}
Also used : User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) UserAccount(org.hisp.dhis.webapi.webdomain.user.UserAccount)

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