Search in sources :

Example 96 with User

use of org.hisp.dhis.user.User 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 97 with User

use of org.hisp.dhis.user.User 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 98 with User

use of org.hisp.dhis.user.User 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 99 with User

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

the class CurrentUserController method hasAuthorization.

@RequestMapping(value = "/authorization/{auth}", produces = { "application/json", "text/*" })
public void hasAuthorization(@PathVariable String auth, HttpServletResponse response) throws IOException {
    User currentUser = currentUserService.getCurrentUser();
    boolean hasAuth = currentUser != null && currentUser.getUserCredentials().isAuthorized(auth);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), hasAuth);
}
Also used : User(org.hisp.dhis.user.User) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 100 with User

use of org.hisp.dhis.user.User 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)

Aggregations

User (org.hisp.dhis.user.User)259 Test (org.junit.Test)74 DhisSpringTest (org.hisp.dhis.DhisSpringTest)72 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)59 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)58 DataElement (org.hisp.dhis.dataelement.DataElement)51 ArrayList (java.util.ArrayList)35 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)33 UserCredentials (org.hisp.dhis.user.UserCredentials)29 List (java.util.List)27 HashSet (java.util.HashSet)25 UserGroup (org.hisp.dhis.user.UserGroup)25 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)22 DataSet (org.hisp.dhis.dataset.DataSet)21 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)20 Date (java.util.Date)17 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)17 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)15 Period (org.hisp.dhis.period.Period)15 ClassPathResource (org.springframework.core.io.ClassPathResource)15