Example usage for javax.naming.directory DirContext modifyAttributes

List of usage examples for javax.naming.directory DirContext modifyAttributes

Introduction

In this page you can find the example usage for javax.naming.directory DirContext modifyAttributes.

Prototype

public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException;

Source Link

Document

Modifies the attributes associated with a named object.

Usage

From source file:org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager.java

@Override
public void doDeleteUserClaimValues(String userName, String[] claims, String profileName)
        throws UserStoreException {
    // get the LDAP Directory context
    DirContext dirContext = this.connectionSource.getContext();
    DirContext subDirContext = null;
    // search the relevant user entry by user name
    String userSearchBase = realmConfig.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE);
    String userSearchFilter = realmConfig.getUserStoreProperty(LDAPConstants.USER_NAME_SEARCH_FILTER);
    userSearchFilter = userSearchFilter.replace("?", escapeSpecialCharactersForFilter(userName));

    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    searchControls.setReturningAttributes(null);

    NamingEnumeration<SearchResult> returnedResultList = null;
    String returnedUserEntry = null;

    try {//from www .  ja v a2 s.  c o m

        returnedResultList = dirContext.search(escapeDNForSearch(userSearchBase), userSearchFilter,
                searchControls);
        // assume only one user is returned from the search
        // TODO:what if more than one user is returned
        if (returnedResultList.hasMore()) {
            returnedUserEntry = returnedResultList.next().getName();
        }

    } catch (NamingException e) {
        String errorMessage = "Results could not be retrieved from the directory context for user : "
                + userName;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage, e);
    } finally {
        JNDIUtil.closeNamingEnumeration(returnedResultList);
    }

    try {
        Attributes updatedAttributes = new BasicAttributes(true);
        // if there is no attribute for profile configuration in LDAP, skip
        // updating it.
        // get the claimMapping related to this claimURI

        for (String claimURI : claims) {
            String attributeName = getClaimAtrribute(claimURI, userName, null);
            Attribute currentUpdatedAttribute = new BasicAttribute(attributeName);
            updatedAttributes.put(currentUpdatedAttribute);
        }

        subDirContext = (DirContext) dirContext.lookup(userSearchBase);
        subDirContext.modifyAttributes(returnedUserEntry, DirContext.REMOVE_ATTRIBUTE, updatedAttributes);

    } catch (Exception e) {
        handleException(e, userName);
    } finally {
        JNDIUtil.closeContext(subDirContext);
        JNDIUtil.closeContext(dirContext);
    }
}

From source file:org.wso2.carbon.user.core.ldap.ReadWriteLDAPUserStoreManager.java

/**
 * Either delete or add user from/to group.
 *
 * @param userNameDN : distinguish name of user entry.
 * @param groupRDN   : relative distinguish name of group entry
 * @param modifyType : modify attribute type in DirCOntext.
 * @throws UserStoreException/*from w  w  w .j ava  2 s  .  c om*/
 */
protected void modifyUserInRole(String userNameDN, String groupRDN, int modifyType, String searchBase)
        throws UserStoreException {

    if (log.isDebugEnabled()) {
        logger.debug("Modifying role: " + groupRDN + " with type: " + modifyType + " user: " + userNameDN
                + " in search base: " + searchBase);
    }

    DirContext mainDirContext = null;
    DirContext groupContext = null;
    try {
        mainDirContext = this.connectionSource.getContext();
        groupContext = (DirContext) mainDirContext.lookup(searchBase);
        String memberAttributeName = realmConfig.getUserStoreProperty(LDAPConstants.MEMBERSHIP_ATTRIBUTE);
        Attributes modifyingAttributes = new BasicAttributes(true);
        Attribute memberAttribute = new BasicAttribute(memberAttributeName);
        memberAttribute.add(userNameDN);
        modifyingAttributes.put(memberAttribute);

        groupContext.modifyAttributes(groupRDN, modifyType, modifyingAttributes);
        if (log.isDebugEnabled()) {
            logger.debug("User: " + userNameDN + " was successfully " + "modified in LDAP group: " + groupRDN);
        }
    } catch (NamingException e) {
        String errorMessage = "Error occurred while modifying user entry: " + userNameDN + " in LDAP role: "
                + groupRDN;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(errorMessage);
    } finally {
        JNDIUtil.closeContext(groupContext);
        JNDIUtil.closeContext(mainDirContext);
    }
}