Search in sources :

Example 26 with DataElementCategory

use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.

the class UpdateUserAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    if (!userService.canAddOrUpdateUser(ugSelected)) {
        throw new AccessDeniedException("You cannot edit this user");
    }
    User currentUser = currentUserService.getCurrentUser();
    // ---------------------------------------------------------------------
    // User credentials and user
    // ---------------------------------------------------------------------
    User user = userService.getUser(id);
    user.setSurname(StringUtils.trimToNull(surname));
    user.setFirstName(StringUtils.trimToNull(firstName));
    user.setEmail(StringUtils.trimToNull(email));
    user.setPhoneNumber(StringUtils.trimToNull(phoneNumber));
    UserCredentials userCredentials = user.getUserCredentials();
    userCredentials.setExternalAuth(externalAuth);
    userCredentials.setOpenId(StringUtils.trimToNull(openId));
    userCredentials.setLdapId(StringUtils.trimToNull(ldapId));
    if (jsonAttributeValues != null) {
        attributeService.updateAttributeValues(user, jsonAttributeValues);
    }
    // ---------------------------------------------------------------------
    // Organisation units
    // ---------------------------------------------------------------------
    Set<OrganisationUnit> dataCaptureOrgUnits = new HashSet<>(selectionManager.getSelectedOrganisationUnits());
    user.updateOrganisationUnits(dataCaptureOrgUnits);
    Set<OrganisationUnit> dataViewOrgUnits = new HashSet<>(selectionTreeManager.getReloadedSelectedOrganisationUnits());
    user.setDataViewOrganisationUnits(dataViewOrgUnits);
    if (dataViewOrgUnits.size() == 0 && currentUser.getDataViewOrganisationUnits().size() != 0) {
        user.setDataViewOrganisationUnits(new HashSet<>(currentUser.getDataViewOrganisationUnits()));
    }
    // ---------------------------------------------------------------------
    // User roles
    // ---------------------------------------------------------------------
    Set<UserAuthorityGroup> userAuthorityGroups = new HashSet<>();
    for (String id : urSelected) {
        userAuthorityGroups.add(userService.getUserAuthorityGroup(id));
    }
    userService.canIssueFilter(userAuthorityGroups);
    userCredentials.setUserAuthorityGroups(userAuthorityGroups);
    // ---------------------------------------------------------------------
    // Dimension constraints
    //
    // Note that any new user must inherit dimension constraints (if any)
    // from the current user.
    // ---------------------------------------------------------------------
    userCredentials.setCogsDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCogsDimensionConstraints()));
    userCredentials.setCatDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCatDimensionConstraints()));
    for (String id : dcSelected) {
        CategoryOptionGroupSet cogs = categoryService.getCategoryOptionGroupSet(id);
        if (cogs != null) {
            userCredentials.getCogsDimensionConstraints().add(cogs);
            continue;
        }
        DataElementCategory cat = categoryService.getDataElementCategory(id);
        if (cat != null) {
            userCredentials.getCatDimensionConstraints().add(cat);
            continue;
        }
    }
    // ---------------------------------------------------------------------
    // Set password and update user
    // ---------------------------------------------------------------------
    userService.encodeAndSetPassword(userCredentials, rawPassword);
    userService.updateUserCredentials(userCredentials);
    userService.updateUser(user);
    if (user.equals(currentUser) && !dataCaptureOrgUnits.isEmpty()) {
        selectionManager.setRootOrganisationUnits(dataCaptureOrgUnits);
        selectionManager.setSelectedOrganisationUnits(dataCaptureOrgUnits);
    } else {
        selectionManager.setRootOrganisationUnits(currentUser.getOrganisationUnits());
        if (ouwtSelected != null && manager.search(OrganisationUnit.class, ouwtSelected) != null) {
            selectionManager.setSelectedOrganisationUnits(Lists.newArrayList(manager.search(OrganisationUnit.class, ouwtSelected)));
        } else {
            selectionManager.setSelectedOrganisationUnits(currentUser.getOrganisationUnits());
        }
    }
    if (user.equals(currentUser) && !dataViewOrgUnits.isEmpty()) {
        selectionTreeManager.setRootOrganisationUnits(dataViewOrgUnits);
        selectionTreeManager.setSelectedOrganisationUnits(dataViewOrgUnits);
    }
    // ---------------------------------------------------------------------
    // User settings
    // ---------------------------------------------------------------------
    userSettingService.saveUserSetting(UserSettingKey.UI_LOCALE, LocaleUtils.getLocale(localeUi), user);
    userSettingService.saveUserSetting(UserSettingKey.DB_LOCALE, LocaleUtils.getLocale(localeDb), user);
    // ---------------------------------------------------------------------
    // User groups
    // ---------------------------------------------------------------------
    Set<UserGroup> userGroups = new HashSet<>();
    for (String id : ugSelected) {
        userGroups.add(userGroupService.getUserGroup(id));
    }
    for (UserGroup userGroup : new HashSet<>(user.getGroups())) {
        if (!userGroups.contains(userGroup)) {
            userGroup.removeUser(user);
            userGroupService.updateUserGroup(userGroup);
        }
    }
    for (UserGroup userGroup : userGroups) {
        userGroup.addUser(user);
        userGroupService.updateUserGroup(userGroup);
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) UserGroup(org.hisp.dhis.user.UserGroup) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) UserCredentials(org.hisp.dhis.user.UserCredentials) HashSet(java.util.HashSet)

Example 27 with DataElementCategory

use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.

the class DefaultDataApprovalAuditService method retainFromDimensionConstraints.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
     * Retain the DataApprovalAudits that the user can read
     * despite any dimension constraints that the user my have.
     *
     * @param audits the list of audit records.
     */
private void retainFromDimensionConstraints(List<DataApprovalAudit> audits) {
    User user = currentUserService.getCurrentUser();
    Set<CategoryOptionGroupSet> cogDimensionConstraints = user.getUserCredentials().getCogsDimensionConstraints();
    Set<DataElementCategory> catDimensionConstraints = user.getUserCredentials().getCatDimensionConstraints();
    if (currentUserService.currentUserIsSuper() || (CollectionUtils.isEmpty(cogDimensionConstraints) && CollectionUtils.isEmpty(catDimensionConstraints))) {
        return;
    }
    // Local cached results
    Map<DataElementCategoryOptionCombo, Boolean> readableOptionCombos = new HashMap<>();
    for (Iterator<DataApprovalAudit> i = audits.iterator(); i.hasNext(); ) {
        DataElementCategoryOptionCombo optionCombo = i.next().getAttributeOptionCombo();
        Boolean canRead = readableOptionCombos.get(optionCombo);
        if (canRead == null) {
            canRead = canReadOptionCombo(user, optionCombo, cogDimensionConstraints, catDimensionConstraints);
            readableOptionCombos.put(optionCombo, canRead);
        }
        if (!canRead) {
            i.remove();
        }
    }
}
Also used : User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 28 with DataElementCategory

use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.

the class DataElementCategoryControllerDocumentation method testCreate.

@Test
@Override
public void testCreate() throws Exception {
    MockHttpSession session = getSession("F_CATEGORY_PUBLIC_ADD");
    DataElementCategoryOption categoryOptionA = createCategoryOption('A');
    DataElementCategoryOption categoryOptionB = createCategoryOption('B');
    DataElementCategoryOption categoryOptionC = createCategoryOption('C');
    DataElementCategory cat = createDataElementCategory('A', categoryOptionA, categoryOptionB, categoryOptionC);
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(post("/" + ENDPOINT).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(cat))).andExpect(status().is(createdStatus)).andDo(documentPrettyPrint("categories/create", requestFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
    cat = manager.getByName(DataElementCategory.class, "DataElementCategoryA");
    assertNotNull(cat);
}
Also used : DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Schema(org.hisp.dhis.schema.Schema) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) MockHttpSession(org.springframework.mock.web.MockHttpSession) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 29 with DataElementCategory

use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.

the class AddUserAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    if (!userService.canAddOrUpdateUser(ugSelected)) {
        throw new AccessDeniedException("You cannot add this user");
    }
    User currentUser = currentUserService.getCurrentUser();
    // ---------------------------------------------------------------------
    // User credentials and user
    // ---------------------------------------------------------------------
    UserCredentials userCredentials = new UserCredentials();
    User user = new User();
    userCredentials.setUserInfo(user);
    user.setUserCredentials(userCredentials);
    userCredentials.setUsername(StringUtils.trimToNull(username));
    userCredentials.setExternalAuth(externalAuth);
    userCredentials.setOpenId(StringUtils.trimToNull(openId));
    userCredentials.setLdapId(StringUtils.trimToNull(ldapId));
    if (ACCOUNT_ACTION_INVITE.equals(accountAction)) {
        userCredentials.setUsername(StringUtils.trimToNull(inviteUsername));
        userCredentials.setInvitation(true);
        user.setEmail(StringUtils.trimToNull(inviteEmail));
        securityService.prepareUserForInvite(user);
    } else {
        user.setSurname(StringUtils.trimToNull(surname));
        user.setFirstName(StringUtils.trimToNull(firstName));
        user.setEmail(StringUtils.trimToNull(email));
        user.setPhoneNumber(StringUtils.trimToNull(phoneNumber));
        userService.encodeAndSetPassword(userCredentials, StringUtils.trimToNull(rawPassword));
    }
    if (jsonAttributeValues != null) {
        attributeService.updateAttributeValues(user, jsonAttributeValues);
    }
    // ---------------------------------------------------------------------
    // Organisation units
    // ---------------------------------------------------------------------
    Set<OrganisationUnit> dataCaptureOrgUnits = new HashSet<>(selectionManager.getSelectedOrganisationUnits());
    user.updateOrganisationUnits(dataCaptureOrgUnits);
    Set<OrganisationUnit> dataViewOrgUnits = new HashSet<>(selectionTreeManager.getReloadedSelectedOrganisationUnits());
    user.setDataViewOrganisationUnits(dataViewOrgUnits);
    if (dataViewOrgUnits.size() == 0 && currentUser.getDataViewOrganisationUnits().size() != 0) {
        user.setDataViewOrganisationUnits(new HashSet<>(currentUser.getDataViewOrganisationUnits()));
    }
    // ---------------------------------------------------------------------
    // User roles
    // ---------------------------------------------------------------------
    Set<UserAuthorityGroup> userAuthorityGroups = new HashSet<>();
    for (String id : urSelected) {
        userAuthorityGroups.add(userService.getUserAuthorityGroup(id));
    }
    userService.canIssueFilter(userAuthorityGroups);
    userCredentials.setUserAuthorityGroups(userAuthorityGroups);
    // ---------------------------------------------------------------------
    // Dimension constraints. Note that any new user must inherit dimension 
    // constraints if any from the current user.
    // ---------------------------------------------------------------------
    userCredentials.setCogsDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCogsDimensionConstraints()));
    userCredentials.setCatDimensionConstraints(new HashSet<>(currentUser.getUserCredentials().getCatDimensionConstraints()));
    for (String id : dcSelected) {
        CategoryOptionGroupSet cogs = categoryService.getCategoryOptionGroupSet(id);
        if (cogs != null) {
            userCredentials.getCogsDimensionConstraints().add(cogs);
            continue;
        }
        DataElementCategory cat = categoryService.getDataElementCategory(id);
        if (cat != null) {
            userCredentials.getCatDimensionConstraints().add(cat);
            continue;
        }
    }
    // ---------------------------------------------------------------------
    // Add User
    // ---------------------------------------------------------------------
    userService.addUser(user);
    userService.addUserCredentials(userCredentials);
    // ---------------------------------------------------------------------
    // User settings
    // ---------------------------------------------------------------------
    userSettingService.saveUserSetting(UserSettingKey.UI_LOCALE, LocaleUtils.getLocale(localeUi), user);
    userSettingService.saveUserSetting(UserSettingKey.DB_LOCALE, LocaleUtils.getLocale(localeDb), user);
    if (ACCOUNT_ACTION_INVITE.equals(accountAction)) {
        RestoreOptions restoreOptions = inviteUsername == null || inviteUsername.isEmpty() ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
        securityService.sendRestoreMessage(userCredentials, getRootPath(), restoreOptions);
    }
    for (String id : ugSelected) {
        UserGroup userGroup = userGroupService.getUserGroup(id);
        userGroup.addUser(user);
        userGroupService.updateUserGroup(userGroup);
    }
    if (ouwtSelected != null && manager.search(OrganisationUnit.class, ouwtSelected) != null) {
        selectionManager.setSelectedOrganisationUnits(Lists.newArrayList(manager.search(OrganisationUnit.class, ouwtSelected)));
    } else {
        selectionManager.setSelectedOrganisationUnits(currentUser.getOrganisationUnits());
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) RestoreOptions(org.hisp.dhis.security.RestoreOptions) AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) CategoryOptionGroupSet(org.hisp.dhis.dataelement.CategoryOptionGroupSet) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) UserGroup(org.hisp.dhis.user.UserGroup) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) UserCredentials(org.hisp.dhis.user.UserCredentials) HashSet(java.util.HashSet)

Aggregations

DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)29 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)12 HashMap (java.util.HashMap)8 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)8 HashSet (java.util.HashSet)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)7 ArrayList (java.util.ArrayList)6 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)6 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)6 Test (org.junit.Test)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)5 User (org.hisp.dhis.user.User)5 DataSet (org.hisp.dhis.dataset.DataSet)4 Section (org.hisp.dhis.dataset.Section)4 List (java.util.List)3 AnalyticsTableColumn (org.hisp.dhis.analytics.AnalyticsTableColumn)3 DataElementGroupSet (org.hisp.dhis.dataelement.DataElementGroupSet)3 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)3