Search in sources :

Example 11 with UserCredentials

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

the class DhisBindAuthenticator method authenticate.

@Override
public DirContextOperations authenticate(Authentication authentication) {
    boolean ldapConf = configurationProvider.isLdapConfigured();
    if (!ldapConf) {
        throw new BadCredentialsException("LDAP authentication is not configured");
    }
    UserCredentials userCredentials = userService.getUserCredentialsByUsername(authentication.getName());
    if (userCredentials == null) {
        throw new BadCredentialsException("Incorrect user credentials");
    }
    if (userCredentials.hasLdapId()) {
        authentication = new UsernamePasswordAuthenticationToken(userCredentials.getLdapId(), authentication.getCredentials());
    }
    return super.authenticate(authentication);
}
Also used : UserCredentials(org.hisp.dhis.user.UserCredentials) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 12 with UserCredentials

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

the class UserSettingController method getUserSetting.

@RequestMapping(value = "/{key}", method = RequestMethod.GET)
@ResponseBody
public String getUserSetting(@PathVariable("key") String key, @RequestParam(value = "user", required = false) String username, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
    Optional<UserSettingKey> keyEnum = UserSettingKey.getByName(key);
    if (!keyEnum.isPresent()) {
        throw new WebMessageException(WebMessageUtils.conflict("Key is not supported: " + key));
    }
    User user = null;
    if (username != null) {
        UserCredentials credentials = userService.getUserCredentialsByUsername(username);
        if (credentials != null) {
            user = credentials.getUserInfo();
        } else {
            throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + username));
        }
    }
    Serializable value = userSettingService.getUserSetting(keyEnum.get(), user);
    if (value == null) {
        throw new WebMessageException(WebMessageUtils.notFound("User setting not found for key: " + key));
    }
    return String.valueOf(value);
}
Also used : Serializable(java.io.Serializable) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UserSettingKey(org.hisp.dhis.user.UserSettingKey) UserCredentials(org.hisp.dhis.user.UserCredentials) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 13 with UserCredentials

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

the class HibernateUserCredentialsStore method getUserCredentialsByUsername.

@Override
public UserCredentials getUserCredentialsByUsername(String username) {
    Query query = getQuery("from UserCredentials uc where uc.username = :username");
    query.setString("username", username);
    return (UserCredentials) query.uniqueResult();
}
Also used : Query(org.hibernate.Query) UserCredentials(org.hisp.dhis.user.UserCredentials)

Example 14 with UserCredentials

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

Example 15 with UserCredentials

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

the class UserObjectBundleHook method postUpdate.

@Override
public void postUpdate(IdentifiableObject persistedObject, ObjectBundle bundle) {
    if (!User.class.isInstance(persistedObject) || !bundle.hasExtras(persistedObject, "uc"))
        return;
    User user = (User) persistedObject;
    final UserCredentials userCredentials = (UserCredentials) bundle.getExtras(persistedObject, "uc");
    final UserCredentials persistedUserCredentials = bundle.getPreheat().get(bundle.getPreheatIdentifier(), UserCredentials.class, user);
    if (!StringUtils.isEmpty(userCredentials.getPassword())) {
        userService.encodeAndSetPassword(userCredentials, userCredentials.getPassword());
    }
    mergeService.merge(new MergeParams<>(userCredentials, persistedUserCredentials).setMergeMode(bundle.getMergeMode()));
    preheatService.connectReferences(persistedUserCredentials, bundle.getPreheat(), bundle.getPreheatIdentifier());
    persistedUserCredentials.setUserInfo(user);
    user.setUserCredentials(persistedUserCredentials);
    sessionFactory.getCurrentSession().update(user.getUserCredentials());
    bundle.removeExtras(persistedObject, "uc");
}
Also used : User(org.hisp.dhis.user.User) MergeParams(org.hisp.dhis.schema.MergeParams) UserCredentials(org.hisp.dhis.user.UserCredentials)

Aggregations

UserCredentials (org.hisp.dhis.user.UserCredentials)29 User (org.hisp.dhis.user.User)15 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 Query (org.hibernate.Query)4 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)4 CategoryOptionGroupSet (org.hisp.dhis.dataelement.CategoryOptionGroupSet)3 DataSet (org.hisp.dhis.dataset.DataSet)3 UserGroup (org.hisp.dhis.user.UserGroup)3 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)2 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)2 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 Program (org.hisp.dhis.program.Program)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 List (java.util.List)1