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:ca.tnt.ldaputils.impl.LdapEntry.java

/**
 * Please note, the preferred method is to call setXXXX() where XXXX is the
 * attribute name, followed by save()./*from w  ww. j  a va  2s.  c o m*/
 * <p/>
 * This sets a batch attribute.  This means that it will be added to a queue
 * for changing LDAP.  You can modify the same attribute multiple times,
 * assuming LDAP supports multivalued attributes for that attribute. You are
 * then required to call modifyBatchAttributes(), which will actually do the
 * operations requested.
 * <p/>
 * You should call this one or more times per attribute, followed by
 * modifyBatchAttributes().
 * <p/>
 * Each time you call this method, for the same attribute, you should
 * specify the same operation, otherwise you will get an
 * IllegalArgumentException, with an appropriate error message.
 *
 * @param operation one of ADD_ATTRIBUTE, REPLACE_ATTRIBUTE,
 *                  REMOVE_ATTRIBUTE
 * @param attribute the name of the attribute
 * @param value     the value of the attribute
 *
 * @see #ADD_ATTRIBUTE ADD_ATTRIBUTE
 * @see #REPLACE_ATTRIBUTE REPLACE_ATTRIBUTE
 * @see #REMOVE_ATTRIBUTE REMOVE_ATTRIBUTE
 */
public void modifyBatchAttribute(final int operation, final String attribute, final Object value) {
    final Attribute newAttribute;
    ModificationItem modItem;
    final int mod_op;

    switch (operation) {
    case ADD_ATTRIBUTE:
        mod_op = DirContext.ADD_ATTRIBUTE;
        break;
    case REPLACE_ATTRIBUTE:
        mod_op = DirContext.REPLACE_ATTRIBUTE;
        break;
    case REMOVE_ATTRIBUTE:
        mod_op = DirContext.REMOVE_ATTRIBUTE;
        break;
    default:
        mod_op = DirContext.ADD_ATTRIBUTE;
    }

    modItem = (ModificationItem) modificationItems.get(attribute);
    if (modItem == null) { // first time we are doing something with this attribute
        newAttribute = new BasicAttribute(attribute, value);
        modItem = new ModificationItem(mod_op, newAttribute);
    } else { // we will add it to the attribute values for this attribute
        if (modItem.getModificationOp() != mod_op) { // make sure they aren't changing their mind on which op
            throw new IllegalArgumentException(
                    "error, operation does not match previous batch items for this attribute");
        }

        modItem.getAttribute().add(value);
    }
    modified = true;
    modificationItems.put(attribute, modItem);
}

From source file:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java

/**
 * Resets the password for the given user.
 *
 * @param username the username//from   www. j av  a  2 s. c  om
 * @param password the new password
 * @throws PortalServiceException for any errors encountered
 */
public void resetPassword(String username, String password) throws PortalServiceException {
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        BasicAttribute pw = new BasicAttribute("userPassword", hash(password));

        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, pw);
        ctx.modifyAttributes(MessageFormat.format(userDNPattern, username), mods);
    } catch (NamingException e) {
        throw new PortalServiceConfigurationException("Unable to reset password.", e);
    } finally {
        closeContext(ctx);
    }
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generate an ModificationItem object for delete user operation. This
 * actually just sets the user status to 'DELETED' instead of deleting the
 * LDAP entry./*from   w  w  w  .j av a  2s  .c o m*/
 */
static ModificationItem getLDAPModificationForDeleteUser() {
    BasicAttribute attr = new BasicAttribute(LDAP_ATTR_STATUS, LDAP_DELETED_STATUS);
    ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

    return mod;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generates an ModificationItem object for deactivating a user.
 *//* w  w  w.jav a 2s  .com*/
static ModificationItem getLDAPModificationForDeactiveUser() {

    BasicAttribute attr = new BasicAttribute(LDAP_ATTR_STATUS, LDAP_DEACTIVE_STATUS);
    ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

    return mod;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generate an ModificationItem object for activating a user. This actually
 * just sets the user status to 'ACTIVE' in the LDAP entry.
 *///from w  w  w  .  j  a  v a 2s.c om
static ModificationItem getLDAPModificationForActivateUser() {

    BasicAttribute attr = new BasicAttribute(LDAP_ATTR_STATUS, LDAP_ACTIVE_STATUS);
    ModificationItem mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

    return mod;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Convert an User object to ModificationItem[] object for updating that
 * User info in LDAP./*from   w  w w . ja  v  a2  s .c o  m*/
 */
static ModificationItem[] convertUserToModificationSet(User p_user) {

    ArrayList attrSet = new ArrayList();
    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_USERID, p_user.getUserId())));

    if (isStringValid(p_user.getTitle())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_TITLE, p_user.getTitle())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_TITLE, "null")));
    }

    if (p_user.getPassword() != null && p_user.isPasswordSet()) {
        /* If the user doesn't set the password, use the original one */
        String password = encyptMD5Password(p_user.getPassword());

        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_PASSWORD, password)));
    }

    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_USER_NAME, p_user.getUserName())));
    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_LAST_NAME, p_user.getLastName())));
    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_FIRST_NAME, p_user.getFirstName())));

    String status = getStateAsString(p_user.getState());
    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_STATUS, status)));

    if (isStringValid(p_user.getEmail())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_EMAIL, p_user.getEmail())));
    }
    if (isStringValid(p_user.getCCEmail())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_CC_EMAIL, p_user.getCCEmail())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_CC_EMAIL, "null")));
    }
    if (isStringValid(p_user.getBCCEmail())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_BCC_EMAIL, p_user.getBCCEmail())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_BCC_EMAIL, "null")));
    }
    if (isStringValid(p_user.getHomePhoneNumber())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_HOME_PHONE, p_user.getHomePhoneNumber())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_HOME_PHONE, "null")));
    }
    if (isStringValid(p_user.getOfficePhoneNumber())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_OFFICE_PHONE, p_user.getOfficePhoneNumber())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_OFFICE_PHONE, "null")));
    }
    if (isStringValid(p_user.getCellPhoneNumber())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_CELL_NUMBER, p_user.getCellPhoneNumber())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_CELL_NUMBER, "null")));
    }
    if (isStringValid(p_user.getFaxPhoneNumber())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_FAX_NUMBER, p_user.getFaxPhoneNumber())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_FAX_NUMBER, "null")));
    }
    if (isStringValid(p_user.getDefaultUILocale())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_DEFAULT_UI_LOCALE, p_user.getDefaultUILocale())));
    }
    if (isStringValid(p_user.getAddress())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_ADDRESS, p_user.getAddress())));
    } else {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_ADDRESS, "null")));
    }
    if (isStringValid(p_user.getCompanyName())) {
        attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                generateLDAPAttribute(LDAP_ATTR_COMPANY, p_user.getCompanyName())));
    }
    attrSet.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
            generateLDAPAttribute(LDAP_ATTR_INALLPROJECTS, p_user.isInAllProjects())));
    // a user can't be changed from anonymous to GlobalSight and back
    // so just leave the type field alone for now
    // LDAP_ATTR_TYPE, LDAP_ANONYMOUS_USER_TYPE
    return (ModificationItem[]) attrSet.toArray(new ModificationItem[] {});
}

From source file:com.adito.ldap.LdapUserDatabase.java

/**
 * (non-Javadoc)/*from  w  ww  .  j av a  2 s.  c om*/
 *
 * @see com.adito.security.DefaultUserDatabase#setPassword(java.lang.String, java.lang.String, boolean, com.adito.security.User, java.lang.String)
 */
public void setPassword(String username, String password, boolean forcePasswordChangeAtLogon, User adminUser,
        String adminPassword) throws UserDatabaseException, InvalidLoginCredentialsException {
    if (!supportsPasswordChange()) {
        throw new InvalidLoginCredentialsException("Database doesn't support password change.");
    }

    LdapUser user;

    try {
        user = getAccount(username);
    } catch (Exception e) {
        throw new UserDatabaseException(e.toString());
    }

    if (forcePasswordChangeAtLogon)
        user.setLastPasswordChange(null);
    else
        user.setLastPasswordChange(new Date());

    LdapTemplate ldapTemplate = new LdapTemplate();
    ldapTemplate.setContextSource(ldapContextSource);
    Attribute attr = new BasicAttribute(PASSWORD_ATTRIBUTE, password);
    ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
    try {
        String dn = getAccount(username).getDn();
        int ind = dn.indexOf(baseDn);
        String rdn = dn.substring(0, ind - 1);
        ldapTemplate.modifyAttributes(rdn, new ModificationItem[] { item });
    } catch (Exception e) {
        throw new UserDatabaseException("Error in LDAP server");
    }

}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

private static boolean toggleUserIDPGroup(String cn, boolean activate) {
    ResourceBundle rb = ResourceBundle.getBundle("ldap");
    String userDN = "cn=" + cn + "," + rb.getString("peopleRoot");
    String idpUser = rb.getString("usersGroup");

    DirContext ctx = null;/*  ww  w  .  j  a v a2  s . c o m*/
    try {
        ctx = getMainAuthContext();

        ModificationItem modAttrs[] = new ModificationItem[1];
        String attrsList[] = { "uniqueMember" };
        Attributes attributes = ctx.getAttributes(idpUser, attrsList);

        Attribute att = attributes.get("uniqueMember");
        if (activate) {
            att.add(userDN);
        } else {
            att.remove(userDN);
        }

        modAttrs[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, att);
        ctx.modifyAttributes(idpUser, modAttrs);
        return true;
    } catch (NamingException ex) {
        _log.error(ex);
    }

    return false;

}

From source file:de.sub.goobi.helper.ldap.Ldap.java

/**
 * Set next free uidNumber.//from w  ww .j a  va  2  s.com
 */
private void setNextUidNumber() {
    Hashtable<String, String> env = getLdapConnectionSettings();
    env.put(Context.SECURITY_PRINCIPAL, ConfigCore.getParameter("ldap_adminLogin"));
    env.put(Context.SECURITY_CREDENTIALS, ConfigCore.getParameter("ldap_adminPassword"));
    DirContext ctx;

    try {
        ctx = new InitialDirContext(env);
        Attributes attrs = ctx.getAttributes(ConfigCore.getParameter("ldap_nextFreeUnixId"));
        Attribute la = attrs.get("uidNumber");
        String oldValue = (String) la.get(0);
        int bla = Integer.parseInt(oldValue) + 1;

        BasicAttribute attrNeu = new BasicAttribute("uidNumber", String.valueOf(bla));
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attrNeu);
        ctx.modifyAttributes(ConfigCore.getParameter("ldap_nextFreeUnixId"), mods);

        ctx.close();
    } catch (NamingException e) {
        logger.error(e);
    }

}