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.mule.module.ldap.api.jndi.LDAPJNDIConnection.java

/**
 * @param dn/*from   w w  w  .j  a  v a  2  s  .co m*/
 * @param attribute
 * @throws LDAPException
 * @see org.mule.module.ldap.api.LDAPConnection#addAttribute(java.lang.String,
 *      org.mule.module.ldap.api.LDAPEntryAttribute)
 */
public void addAttribute(String dn, LDAPEntryAttribute attribute) throws LDAPException {
    try {
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, buildBasicAttribute(attribute));
        getConn().modifyAttributes(dn, mods);
    } catch (NamingException nex) {
        throw handleNamingException(nex, "Add attribute failed.");
    }
}

From source file:org.mule.module.ldap.api.jndi.LDAPJNDIConnection.java

/**
 * @param dn//from ww  w .j  a v a2s .c o  m
 * @param attribute
 * @throws LDAPException
 * @see org.mule.module.ldap.api.LDAPConnection#updateAttribute(java.lang.String,
 *      org.mule.module.ldap.api.LDAPEntryAttribute)
 */
public void updateAttribute(String dn, LDAPEntryAttribute attribute) throws LDAPException {

    try {
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, buildBasicAttribute(attribute));
        getConn().modifyAttributes(dn, mods);
    } catch (NamingException nex) {
        throw handleNamingException(nex, "Update attribute failed.");
    }
}

From source file:org.mule.module.ldap.api.jndi.LDAPJNDIConnection.java

/**
 * @param dn/*from  w  ww. j av a  2s. c om*/
 * @param attribute
 * @throws LDAPException
 * @see org.mule.module.ldap.api.LDAPConnection#deleteAttribute(java.lang.String,
 *      org.mule.module.ldap.api.LDAPEntryAttribute)
 */
public void deleteAttribute(String dn, LDAPEntryAttribute attribute) throws LDAPException {
    try {
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, buildBasicAttribute(attribute));
        getConn().modifyAttributes(dn, mods);
    } catch (NamingException nex) {
        throw handleNamingException(nex, "Delete attribute failed.");
    }
}

From source file:org.olat.ldap.LDAPLoginManagerImpl.java

/**
 * Change the password on the LDAP server.
 * //from  ww  w.j a va  2s . com
 * @see org.olat.ldap.LDAPLoginManager#changePassword(org.olat.core.id.Identity, java.lang.String, org.olat.ldap.LDAPError)
 */
@Override
public void changePassword(final Identity identity, final String pwd, final LDAPError errors) {
    final String uid = identity.getName();
    final String ldapUserPasswordAttribute = LDAPLoginModule.getLdapUserPasswordAttribute();
    try {
        final DirContext ctx = bindSystem();
        final String dn = searchUserDN(uid, ctx);

        final ModificationItem[] modificationItems = new ModificationItem[1];

        Attribute userPasswordAttribute;
        if (LDAPLoginModule.isActiveDirectory()) {
            // active directory need the password enquoted and unicoded (but little-endian)
            final String quotedPassword = "\"" + pwd + "\"";
            final char unicodePwd[] = quotedPassword.toCharArray();
            final byte pwdArray[] = new byte[unicodePwd.length * 2];
            for (int i = 0; i < unicodePwd.length; i++) {
                pwdArray[i * 2 + 1] = (byte) (unicodePwd[i] >>> 8);
                pwdArray[i * 2 + 0] = (byte) (unicodePwd[i] & 0xff);
            }
            userPasswordAttribute = new BasicAttribute(ldapUserPasswordAttribute, pwdArray);
        } else {
            userPasswordAttribute = new BasicAttribute(ldapUserPasswordAttribute, pwd);
        }

        modificationItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, userPasswordAttribute);
        ctx.modifyAttributes(dn, modificationItems);
        ctx.close();
    } catch (final NamingException e) {
        logError("NamingException when trying to change password with username::" + uid, e);
        errors.insert("Cannot change the password");
    }
}

From source file:org.olat.ldap.manager.LDAPLoginManagerImpl.java

/**
 * Change the password on the LDAP server.
 * @see org.olat.ldap.LDAPLoginManager#changePassword(org.olat.core.id.Identity, java.lang.String, org.olat.ldap.LDAPError)
 *//*from   www . j  a v a  2s  .co  m*/
@Override
public boolean changePassword(Identity identity, String pwd, LDAPError errors) {
    String uid = identity.getName();
    String ldapUserPasswordAttribute = syncConfiguration.getLdapUserPasswordAttribute();
    try {
        DirContext ctx = bindSystem();
        String dn = ldapDao.searchUserDN(uid, ctx);

        ModificationItem[] modificationItems = new ModificationItem[1];

        Attribute userPasswordAttribute;
        if (ldapLoginModule.isActiveDirectory()) {
            //active directory need the password enquoted and unicoded (but little-endian)
            String quotedPassword = "\"" + pwd + "\"";
            char unicodePwd[] = quotedPassword.toCharArray();
            byte pwdArray[] = new byte[unicodePwd.length * 2];
            for (int i = 0; i < unicodePwd.length; i++) {
                pwdArray[i * 2 + 1] = (byte) (unicodePwd[i] >>> 8);
                pwdArray[i * 2 + 0] = (byte) (unicodePwd[i] & 0xff);
            }
            userPasswordAttribute = new BasicAttribute(ldapUserPasswordAttribute, pwdArray);
        } else {
            userPasswordAttribute = new BasicAttribute(ldapUserPasswordAttribute, pwd);
        }

        modificationItems[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, userPasswordAttribute);
        ctx.modifyAttributes(dn, modificationItems);
        ctx.close();
        return true;
    } catch (NamingException e) {
        log.error("NamingException when trying to change password with username::" + uid, e);
        errors.insert("Cannot change the password");
        return false;
    } catch (Exception e) {
        log.error("Unexpected exception when trying to change password with username::" + uid, e);
        errors.insert("Cannot change the password");
        return false;
    }
}

From source file:org.openiam.spml2.spi.ldap.LdapConnectorImpl.java

public ResponseType setPassword(SetPasswordRequestType reqType) {
    log.debug("setPassword request called..");

    ConnectionMgr conMgr = null;/*  ww  w.  j av a 2 s . c  o  m*/

    String requestID = reqType.getRequestID();
    /* PSO - Provisioning Service Object -
       *     -  ID must uniquely specify an object on the target or in the target's namespace
       *     -  Try to make the PSO ID immutable so that there is consistency across changes. */
    PSOIdentifierType psoID = reqType.getPsoID();
    /* targetID -  */
    String targetID = psoID.getTargetID();
    /* ContainerID - May specify the container in which this object should be created
       *      ie. ou=Development, org=Example */
    PSOIdentifierType containerID = psoID.getContainerID();

    /* A) Use the targetID to look up the connection information under managed systems */
    ManagedSys managedSys = managedSysService.getManagedSys(targetID);

    try {
        log.debug("managedSys found for targetID=" + targetID + " " + " Name=" + managedSys.getName());
        conMgr = ConnectionFactory.create(ConnectionManagerConstant.LDAP_CONNECTION);
        LdapContext ldapctx = conMgr.connect(managedSys);

        log.debug("Ldapcontext = " + ldapctx);

        String ldapName = psoID.getID();

        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("userPassword", reqType.getPassword()));
        ldapctx.modifyAttributes(ldapName, mods);

        // check if the request contains additional attributes
        List<ExtensibleObject> extObjList = reqType.getAny();
        if (extObjList != null && extObjList.size() > 0) {
            ExtensibleObject obj = extObjList.get(0);
            if (obj != null) {
                List<ExtensibleAttribute> attrList = obj.getAttributes();
                if (attrList != null && attrList.size() > 0) {
                    mods = new ModificationItem[attrList.size()];
                    for (ExtensibleAttribute a : attrList) {
                        mods[0] = new ModificationItem(a.getOperation(),
                                new BasicAttribute(a.getName(), a.getValue()));
                    }
                    ldapctx.modifyAttributes(ldapName, mods);
                }
            }
        }

    } catch (NamingException ne) {
        log.error(ne.getMessage(), ne);

        ResponseType resp = new ResponseType();
        resp.setStatus(StatusCodeType.FAILURE);
        resp.setError(ErrorCode.NO_SUCH_IDENTIFIER);
        return resp;
    } catch (Exception ne) {
        log.error(ne.getMessage(), ne);

        ResponseType resp = new ResponseType();
        resp.setStatus(StatusCodeType.FAILURE);
        resp.setError(ErrorCode.OTHER_ERROR);
        resp.addErrorMessage(ne.toString());
        return resp;

    } finally {
        /* close the connection to the directory */
        try {
            if (conMgr != null) {
                conMgr.close();
            }

        } catch (NamingException n) {
            log.error(n);
        }

    }

    ResponseType respType = new ResponseType();
    respType.setStatus(StatusCodeType.SUCCESS);
    return respType;

}

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

/**
 * Helper method./*from  w w w .  ja va  2s.c  o  m*/
 * 
 * @param {@link DirContext#REPLACE_ATTRIBUTE}, {@link DirContext#ADD_ATTRIBUTE} or
 *          {@link DirContext#REMOVE_ATTRIBUTE}.
 * @param attrId
 * @param attrValue
 * @return
 */
protected ModificationItem createModificationItem(final int mode, final String attrId, final String attrValue) {
    return new ModificationItem(mode, new BasicAttribute(attrId, attrValue));
}

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//ww  w . jav  a 2  s .c o 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 a 2s .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.sonar.plugins.activedirectory.server.ApacheDS.java

@SuppressWarnings("unused")
private ApacheDS startKerberos() throws Exception {
    Preconditions.checkState(ldapServer.isStarted());

    kdcServer.setDirectoryService(directoryService);
    // FIXME hard-coded ports
    kdcServer.setTransports(new TcpTransport(6088), new UdpTransport(6088));
    kdcServer.setEnabled(true);//from   w  ww  . j  av  a  2  s.c  o  m
    kdcServer.setPrimaryRealm(realm);
    kdcServer.setSearchBaseDn(baseDn);
    kdcServer.setKdcPrincipal("krbtgt/" + realm + "@" + baseDn);
    kdcServer.start();

    // -------------------------------------------------------------------
    // Enable the krb5kdc schema
    // -------------------------------------------------------------------

    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(DirectoryService.JNDI_KEY, directoryService);
    env.put(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName());
    env.put(Context.PROVIDER_URL, ServerDNConstants.OU_SCHEMA_DN);
    InitialLdapContext schemaRoot = new InitialLdapContext(env, null);

    // check if krb5kdc is disabled
    Attributes krb5kdcAttrs = schemaRoot.getAttributes("cn=Krb5kdc");
    boolean isKrb5KdcDisabled = false;
    if (krb5kdcAttrs.get("m-disabled") != null) {
        isKrb5KdcDisabled = ((String) krb5kdcAttrs.get("m-disabled").get()).equalsIgnoreCase("TRUE");
    }

    // if krb5kdc is disabled then enable it
    if (isKrb5KdcDisabled) {
        Attribute disabled = new BasicAttribute("m-disabled");
        ModificationItem[] mods = new ModificationItem[] {
                new ModificationItem(DirContext.REMOVE_ATTRIBUTE, disabled) };
        schemaRoot.modifyAttributes("cn=Krb5kdc", mods);
    }
    return this;
}