Example usage for javax.naming.directory DirContext REPLACE_ATTRIBUTE

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

Introduction

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

Prototype

int REPLACE_ATTRIBUTE

To view the source code for javax.naming.directory DirContext REPLACE_ATTRIBUTE.

Click Source Link

Document

This constant specifies to replace an attribute with specified values.

Usage

From source file:org.projectforge.business.ldap.LdapDao.java

/**
 * Helper method.//ww  w  . j  a v  a2 s.  co  m
 * 
 * @param attrId
 * @param attrValue
 * @return
 */
protected ModificationItem createModificationItem(final String attrId, final String attrValue) {
    return createModificationItem(DirContext.REPLACE_ATTRIBUTE, attrId, attrValue);
}

From source file:org.projectforge.business.ldap.LdapDao.java

/**
 * Helper method for appending modification item(s) to a given list. At least one entry will be added if no attrValue
 * is given.// ww  w. j  a  v a2s  . c o m
 * 
 * @param list
 * @param attrId
 * @param attrValues If null then a null-value will be assumed. If more than one string is given, multiple
 *          modification items will be added.
 * @return
 */
protected void createAndAddModificationItems(final List<ModificationItem> list, final String attrId,
        final String... attrValues) {
    if (attrValues == null) {
        list.add(createModificationItem(attrId, null));
        return;
    }
    boolean added = false;
    for (final String attrValue : attrValues) {
        if (StringUtils.isEmpty(attrValue) == true && added == true) {
            continue;
        }
        final String val = StringUtils.isEmpty(attrValue) == true ? null : attrValue;
        if (added == false) {
            list.add(createModificationItem(DirContext.REPLACE_ATTRIBUTE, attrId, val));
            added = true;
        } else {
            list.add(createModificationItem(DirContext.ADD_ATTRIBUTE, attrId, val));
        }
    }
}

From source file:org.projectforge.business.ldap.LdapDao.java

/**
 * Helper method for appending modification item(s) to a given list. At least one entry will be added if no attrValue
 * is given.//  ww w. j  av  a 2  s  .  c o  m
 * 
 * @param list
 * @param attrId
 * @param attrValues If null then a null-value will be assumed. If more than one string is given, multiple
 *          modification items will be added.
 * @return
 */
protected void createAndAddModificationItems(final List<ModificationItem> list, final String attrId,
        final Set<String> attrValues) {
    if (attrValues == null) {
        list.add(createModificationItem(attrId, null));
        return;
    }
    boolean added = false;
    for (final String attrValue : attrValues) {
        if (StringUtils.isEmpty(attrValue) == true && added == true) {
            continue;
        }
        final String val = StringUtils.isEmpty(attrValue) == true ? null : attrValue;
        if (added == false) {
            list.add(createModificationItem(DirContext.REPLACE_ATTRIBUTE, attrId, val));
            added = true;
        } else {
            list.add(createModificationItem(DirContext.ADD_ATTRIBUTE, attrId, val));
        }
    }
}

From source file:org.projectforge.business.ldap.LdapUserDao.java

public void deactivateUser(final DirContext ctx, final LdapUser user) throws NamingException {
    log.info("Deactivate user: " + buildDn(null, user));
    final List<ModificationItem> modificationItems = new ArrayList<ModificationItem>();
    modificationItems//from w w w . j  av  a 2s  .  co m
            .add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userPassword", null)));
    modificationItems.add(
            new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", DEACTIVATED_MAIL)));
    buildDn(null, user);
    modify(ctx, user, modificationItems);
    final String ou = user.getOrganizationalUnit();
    if (ou.startsWith(DEACTIVATED_SUB_CONTEXT2) == false) {
        // Move user to the sub-context "deactivated".
        final String newOu = LdapUtils.getOu(DEACTIVATED_SUB_CONTEXT, getOuBase());
        move(ctx, user, newOu);
        user.setOrganizationalUnit(newOu);
    }
}

From source file:org.projectforge.business.ldap.LdapUserDao.java

public void changePassword(final LdapUser user, final String oldPassword, final String newPassword) {
    log.info("Change password for " + getObjectClass() + ": " + buildDn(null, user));
    final List<ModificationItem> modificationItems = new ArrayList<ModificationItem>();
    if (oldPassword != null) {
        modificationItems.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                new BasicAttribute("userPassword", oldPassword)));
        modificationItems.add(new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword)));
    } else {//  w  w w  . jav a2 s. c  o  m
        modificationItems.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword)));
    }
    if (isSambaAccountsConfigured() == true && user.getSambaSIDNumber() != null) {
        final String sambaNTPassword = SmbEncrypt.NTUNICODEHash(newPassword);
        modificationItems.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("sambaNTPassword", sambaNTPassword)));
    }
    // Perform the update
    modify(user, modificationItems);
}

From source file:org.springframework.ldap.core.DirContextAdapter.java

/**
 * Collect all modifications for the changed attribute. If no changes have
 * been made, return immediately. If modifications have been made, and the
 * original size as well as the updated size of the attribute is 1, replace
 * the attribute. If the size of the updated attribute is 0, remove the
 * attribute. Otherwise, the attribute is a multi-value attribute; if it's
 * an ordered one it should be replaced in its entirety to preserve the new
 * ordering, if not all modifications to the original value (removals and
 * additions) will be collected individually.
 * /*from  w  w  w.j  a  v  a 2  s .c om*/
 * @param changedAttr the value of the changed attribute.
 * @param modificationList the list in which to add the modifications.
 * @throws NamingException if thrown by called Attribute methods.
 */
private void collectModifications(Attribute changedAttr, List modificationList) throws NamingException {
    Attribute currentAttribute = originalAttrs.get(changedAttr.getID());

    if (changedAttr.equals(currentAttribute)) {
        // No changes
        return;
    } else if (currentAttribute != null && currentAttribute.size() == 1 && changedAttr.size() == 1) {
        // Replace single-vale attribute.
        modificationList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() == 0 && currentAttribute != null) {
        // Attribute has been removed.
        modificationList.add(new ModificationItem(DirContext.REMOVE_ATTRIBUTE, changedAttr));
    } else if ((currentAttribute == null || currentAttribute.size() == 0) && changedAttr.size() > 0) {
        // Attribute has been added.
        modificationList.add(new ModificationItem(DirContext.ADD_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() > 0 && changedAttr.isOrdered()) {
        // This is a multivalue attribute and it is ordered - the original
        // value should be replaced with the new values so that the ordering
        // is preserved.
        modificationList.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
    } else if (changedAttr.size() > 0) {
        // Change of multivalue Attribute. Collect additions and removals
        // individually.
        List myModifications = new LinkedList();
        collectModifications(currentAttribute, changedAttr, myModifications);

        if (myModifications.isEmpty()) {
            // This means that the attributes are not equal, but the
            // actual values are the same - thus the order must have
            // changed. This should result in a REPLACE_ATTRIBUTE operation.
            myModifications.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, changedAttr));
        }

        modificationList.addAll(myModifications);
    }
}

From source file:org.springframework.security.ldap.userdetails.LdapUserDetailsManager.java

private void changePasswordUsingAttributeModification(DistinguishedName userDn, String oldPassword,
        String newPassword) {/*from w  w  w.j av  a 2s.  co  m*/

    final ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(
            DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(passwordAttributeName, newPassword)) };

    if (oldPassword == null) {
        template.modifyAttributes(userDn, passwordChange);
        return;
    }

    template.executeReadWrite(dirCtx -> {
        LdapContext ctx = (LdapContext) dirCtx;
        ctx.removeFromEnvironment("com.sun.jndi.ldap.connect.pool");
        ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, LdapUtils.getFullDn(userDn, ctx).toString());
        ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, oldPassword);
        // TODO: reconnect doesn't appear to actually change the credentials
        try {
            ctx.reconnect(null);
        } catch (javax.naming.AuthenticationException e) {
            throw new BadCredentialsException("Authentication for password change failed.");
        }

        ctx.modifyAttributes(userDn, passwordChange);

        return null;
    });

}

From source file:org.swordess.ldap.util.ModUtils.java

public static ModificationItem replace(String id, Object value) {
    return create(DirContext.REPLACE_ATTRIBUTE, id, value, null);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static <T> ModificationItem replace(String id, Object value, Evaluator<T> evaluator) {
    return create(DirContext.REPLACE_ATTRIBUTE, id, value, evaluator);
}

From source file:org.swordess.ldap.util.ModUtils.java

public static ModificationItem replace(String id, Object[] values) {
    return create(DirContext.REPLACE_ATTRIBUTE, id, values, null);
}