Example usage for javax.naming.directory ModificationItem ModificationItem

List of usage examples for javax.naming.directory ModificationItem ModificationItem

Introduction

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

Prototype

public ModificationItem(int mod_op, Attribute attr) 

Source Link

Document

Creates a new instance of ModificationItem.

Usage

From source file:org.apache.jmeter.protocol.ldap.sampler.LDAPExtSampler.java

/***************************************************************************
 * Collect all the value from the table (Arguments), using this create the
 * basicAttributes This will create the Basic Attributes for the User
 * defined TestCase for Modify test/*from  w  w  w .ja  va2  s  .  c  om*/
 *
 * @return The BasicAttributes
 **************************************************************************/
private ModificationItem[] getUserModAttributes() {
    ModificationItem[] mods = new ModificationItem[getLDAPArguments().getArguments().size()];
    BasicAttribute attr;
    PropertyIterator iter = getLDAPArguments().iterator();
    int count = 0;
    while (iter.hasNext()) {
        LDAPArgument item = (LDAPArgument) iter.next().getObjectValue();
        if ((item.getValue()).length() == 0) {
            attr = new BasicAttribute(item.getName());
        } else {
            attr = getBasicAttribute(item.getName(), item.getValue());
        }

        final String opcode = item.getOpcode();
        if ("add".equals(opcode)) { // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr);
        } else if ("delete".equals(opcode) // $NON-NLS-1$
                || "remove".equals(opcode)) { // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr);
        } else if ("replace".equals(opcode)) { // $NON-NLS-1$
            mods[count++] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
        } else {
            log.warn("Invalid opCode: " + opcode);
        }
    }
    return mods;
}

From source file:org.apache.syncope.fit.AbstractITCase.java

protected void updateLdapRemoteObject(final String bindDn, final String bindPwd, final String objectDn,
        final Pair<String, String> attribute) {

    InitialDirContext ctx = null;
    try {//from w ww .j  a va2  s . c o m
        ctx = getLdapResourceDirContext(bindDn, bindPwd);

        Attribute ldapAttribute = new BasicAttribute(attribute.getKey(), attribute.getValue());
        ModificationItem[] item = new ModificationItem[1];
        item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, ldapAttribute);
        ctx.modifyAttributes(objectDn, item);
    } catch (Exception e) {
        // ignore
    } finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                // ignore
            }
        }
    }
}

From source file:org.bedework.selfreg.common.DirMaintImpl.java

@Override
public void setUserPassword(final String account, final String password) throws SelfregException {
    BasicAttribute attr = new BasicAttribute("userPassword", encodedPassword(password.toCharArray()));
    ModificationItem mi = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

    ModificationItem[] mods = { mi };
    getLdir().modify(accountDn(account), mods);
}

From source file:org.bedework.selfreg.common.DirMaintImpl.java

@Override
public void addGroupMember(final String group, final String account) throws SelfregException {
    //if (!accountExists(account)) {
    //  error("Account " + account + " does not exist");
    //}/*from   w  w w. j  av  a  2  s  . c o m*/

    BasicAttribute attr = new BasicAttribute("member", accountDn(account));
    ModificationItem mi = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr);

    ModificationItem[] mods = { mi };
    getLdir().modify(groupDn(group), mods);
}

From source file:org.easy.ldap.AdminServiceImpl.java

@Override
public void updateUser(LdapUser user, Map<RdnType, String> newData) {
    Preconditions.checkNotNull(newData);
    Preconditions.checkNotNull(user.getTenantId());
    Preconditions.checkNotNull(user.getUserId());
    Preconditions.checkArgument(user.getTenantId().trim().length() > 0);
    Preconditions.checkArgument(user.getUserId().trim().length() > 0);
    Preconditions.checkArgument(newData.size() > 0);

    try {//from   w w  w.  j  a v a2  s .c om
        ModificationItem[] modifications = new ModificationItem[newData.size()];

        int i = 0;

        for (RdnType attributeName : newData.keySet()) {
            /* if (attributeName.equals(RdnType.UID))
            throw new RuntimeException("Cannot change uid.");*/

            modifications[i] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    new BasicAttribute(attributeName.toString(), newData.get(attributeName)));

            i++;
        }

        LdapName rootDn = namingFactory.createUsersDn(user.getTenantId());
        Rdn userRdn = NamingFactory.createRdn(RdnType.UID, user.getUserId());
        LdapName subContextName = NamingFactory.createName(userRdn);

        ldapDao.updateSubContext(rootDn, subContextName, modifications);
    } catch (Exception e) {
        log.error(e);
        throw new java.lang.RuntimeException(e);
    }
}

From source file:org.easy.ldap.LdapDao.java

public void updateRdn(LdapName rootDn, RdnType type, String rdnValue) {
    DirContext ctx = null;/* w  ww.  j  a  v  a  2 s.c  om*/

    try {
        ctx = contextFactory.createContext(rootDn.toString());

        ModificationItem[] modifications = new ModificationItem[1];

        Attribute attribute = new BasicAttribute(type.toString(), rdnValue);

        modifications[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);

        ctx.modifyAttributes("", modifications);

    }

    catch (NamingException e) {
        throw new RuntimeException(type.toString() + "=" + rdnValue + "," + rootDn.toString(), e);
    }

    finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug(e);
            }
        }
    }
}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn//from w w  w.  ja  va2  s .co m
 * @param type
 * @param rdnValue
 */
public void addRdn(LdapName rootDn, RdnType type, String rdnValue) {
    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());

        ModificationItem[] modifications = new ModificationItem[1];

        Attribute attribute = new BasicAttribute(type.toString(), rdnValue);

        modifications[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute);

        ctx.modifyAttributes("", modifications);

    }

    catch (NamingException e) {
        throw new RuntimeException(e);
    }

    finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug(e);
            }
        }
    }

}

From source file:org.easy.ldap.LdapDao.java

/**
 * @param rootDn/*from  w w w . jav a2s  . com*/
 * @param type
 * @param rdnValue
 */
public void removeRdn(LdapName rootDn, RdnType type, String rdnValue) {
    DirContext ctx = null;

    try {
        ctx = contextFactory.createContext(rootDn.toString());

        ModificationItem[] modifications = new ModificationItem[1];

        Attribute attribute = new BasicAttribute(type.toString(), rdnValue);

        modifications[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute);

        ctx.modifyAttributes("", modifications);

    }

    catch (NamingException e) {
        throw new RuntimeException(type.toString() + "=" + rdnValue + "," + rootDn.toString(), e);
    }

    finally {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.debug(e);
            }
        }
    }

}

From source file:org.fao.geonet.kernel.security.ldap.LdapUserDetailsManager.java

/**
 * Changes the password for the current user. The username is obtained from the security
 * context. <p> If the old password is supplied, the update will be made by rebinding as the
 * user, thus modifying the password using the user's permissions. If <code>oldPassword</code>
 * is null, the update will be attempted using a standard read/write context supplied by the
 * context source. </p>/*from   ww  w.  j a  v  a 2  s .c  o  m*/
 *
 * @param oldPassword the old password
 * @param newPassword the new value of the password.
 */
public void changePassword(final String oldPassword, final String newPassword) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Assert.notNull(authentication,
            "No authentication object found in security context. Can't change current user's password!");

    String username = authentication.getName();

    logger.debug("Changing password for user '" + username);

    final DistinguishedName dn = usernameMapper.buildDn(username);
    final ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem(
            DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(passwordAttributeName, newPassword)) };

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

    template.executeReadWrite(new ContextExecutor() {

        public Object executeWithContext(DirContext dirCtx) throws NamingException {
            LdapContext ctx = (LdapContext) dirCtx;
            ctx.removeFromEnvironment("com.sun.jndi.ldap.connect.pool");
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, LdapUtils.getFullDn(dn, 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(dn, passwordChange);

            return null;
        }
    });
}

From source file:org.fao.geonet.kernel.security.ldap.LdapUserDetailsManager.java

private void modifyAuthorities(final DistinguishedName userDn,
        final Collection<? extends GrantedAuthority> authorities, final int modType) {
    template.executeReadWrite(new ContextExecutor() {
        public Object executeWithContext(DirContext ctx) throws NamingException {
            for (GrantedAuthority authority : authorities) {
                String group = convertAuthorityToGroup(authority);
                DistinguishedName fullDn = LdapUtils.getFullDn(userDn, ctx);
                ModificationItem addGroup = new ModificationItem(modType,
                        new BasicAttribute(groupMemberAttributeName, fullDn.toUrl()));

                ctx.modifyAttributes(buildGroupDn(group), new ModificationItem[] { addGroup });
            }/*from   ww  w . j ava 2  s .c  om*/
            return null;
        }
    });
}