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, ModificationItem[] mods) throws NamingException;

Source Link

Document

Modifies the attributes associated with a named object using an ordered list of modifications.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ModificationItem[] mods = new ModificationItem[3];

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", "g@w.com"));

    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("number", "5555"));

    mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpeg"));
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    DirContext ctx = new InitialDirContext(env);
    ctx.modifyAttributes("cn=Name, ou=People", mods);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
    env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

    DirContext ctx = new InitialDirContext(env);

    ModificationItem[] mods = new ModificationItem[2];

    Attribute mod0 = new BasicAttribute("number", "555-555-5555");
    Attribute mod1 = new BasicAttribute("1", "AAA");

    mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
    mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);

    ctx.modifyAttributes("uid=mewilcox, ou=People, o=airius.com", mods);
}

From source file:Modify2Example.java

public static void main(String args[]) {
    Hashtable env = new Hashtable(11);

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    try {//  w w w. ja  va 2  s.c om
        DirContext dctx = new InitialDirContext(env);

        ModificationItem[] mods = new ModificationItem[3];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("department", "sales"));
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("quota", "$1"));
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("assistant"));

        dctx.modifyAttributes("cn=Name, ou=People", mods);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:ModAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*from   ww  w .  j av a  2  s  .c o  m*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        String name = "cn=Ted Geisel, ou=People";

        // Save original attributes
        Attributes orig = ctx.getAttributes(name, new String[] { "mail", "telephonenumber", "jpegphoto" });

        // Specify the changes to make
        ModificationItem[] mods = new ModificationItem[3];

        // Replace the "mail" attribute with a new value
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("mail", "geisel@wizards.com"));

        // Add additional value to "telephonenumber"
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("telephonenumber", "+1 555 555 5555"));

        // Remove the "jpegphoto" attribute
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));

        // Perform the requested modifications on the named object
        ctx.modifyAttributes(name, mods);

        // Check attributes
        System.out.println("**** new attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Revert changes
        ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

        // Check that the attributes got restored
        System.out.println("**** reverted to original attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public static boolean addMail(LDAPUser user, String newMail) {
    DirContext ctx = null;
    try {/* w  w  w.  j  a  v  a  2  s .  co  m*/
        ctx = getAuthContext(user.getUsername(), user.getPassword());

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("mail", newMail));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + user.getUsername() + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

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

public static boolean resetPassword(String cn, String newPassword) {
    DirContext ctx = null;
    try {//w w  w.  ja  v  a2s  .c  om
        ctx = getMainAuthContext();

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + cn + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

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

public static boolean updatePassword(LDAPUser user, String newPassword) {
    DirContext ctx = null;
    try {//from   ww w.  j  ava  2  s .co  m
        ctx = getAuthContext(user.getUsername(), user.getPassword());

        ModificationItem[] modItems = new ModificationItem[1];
        modItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", newPassword));

        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        ctx.modifyAttributes("cn=" + user.getUsername() + "," + rb.getString("peopleRoot"), modItems);
    } catch (NamingException ex) {
        _log.error(ex);
        return false;
    }

    return true;
}

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;
    try {/*from  w  w w .java  2  s  .  c o m*/
        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:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java

/**
 * Removes the given user from the given role.
 *
 * @param ctx the directory context/*from w  w  w  .  j  av a2 s . c o  m*/
 * @param username the user to be removed
 * @param existingRole the role to be removed from
 * @throws NamingException for any errors encountered
 */
private void removeRoleAssignment(DirContext ctx, String username, String existingRole) throws NamingException {
    ModificationItem[] mods = new ModificationItem[1];
    BasicAttribute m = new BasicAttribute(groupMemberAttr, MessageFormat.format(userDNPattern, username));
    mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, m);
    ctx.modifyAttributes(MessageFormat.format(groupDNPattern, existingRole), mods);
}

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

/**
 * Resets the password for the given user.
 *
 * @param username the username//  w w  w. j  a va2  s. co  m
 * @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);
    }
}