Search in sources :

Example 6 with ModifyRequestImpl

use of org.apache.directory.api.ldap.model.message.ModifyRequestImpl in project directory-ldap-api by apache.

the class LdapNetworkConnection method modify.

/**
 * {@inheritDoc}
 */
@Override
public void modify(Dn dn, Modification... modifications) throws LdapException {
    if (dn == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(I18n.msg(I18n.MSG_03240_NULL_DN_MODIFY));
        }
        throw new IllegalArgumentException("The Dn to be modified cannot be null");
    }
    if ((modifications == null) || (modifications.length == 0)) {
        String msg = "Cannot process a ModifyRequest without any modification";
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    ModifyRequest modReq = new ModifyRequestImpl();
    modReq.setName(dn);
    for (Modification modification : modifications) {
        modReq.addModification(modification);
    }
    ModifyResponse modifyResponse = modify(modReq);
    processResponse(modifyResponse);
}
Also used : Modification(org.apache.directory.api.ldap.model.entry.Modification) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Example 7 with ModifyRequestImpl

use of org.apache.directory.api.ldap.model.message.ModifyRequestImpl in project directory-ldap-api by apache.

the class LdapNetworkConnection method modify.

/**
 * {@inheritDoc}
 */
@Override
public void modify(Entry entry, ModificationOperation modOp) throws LdapException {
    if (entry == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(I18n.msg(I18n.MSG_03241_NULL_ENTRY_MODIFY));
        }
        throw new IllegalArgumentException("Entry to be modified cannot be null");
    }
    ModifyRequest modReq = new ModifyRequestImpl();
    modReq.setName(entry.getDn());
    Iterator<Attribute> itr = entry.iterator();
    while (itr.hasNext()) {
        modReq.addModification(itr.next(), modOp);
    }
    ModifyResponse modifyResponse = modify(modReq);
    processResponse(modifyResponse);
}
Also used : ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Example 8 with ModifyRequestImpl

use of org.apache.directory.api.ldap.model.message.ModifyRequestImpl in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method deleteStringAttributeValue.

public void deleteStringAttributeValue(final String entryDN, final String attributeName, final String value) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().deleteStringAttributeValue(entryDN, attributeName, value);
    try {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName(new Dn(entryDN));
        if (value == null) {
            final Modification modification = new DefaultModification(ModificationOperation.REMOVE_ATTRIBUTE, attributeName);
            modifyRequest.addModification(modification);
        } else {
            final Modification modification = new DefaultModification();
            modification.setOperation(ModificationOperation.REMOVE_ATTRIBUTE);
            modification.setAttribute(new DefaultAttribute(attributeName, value));
        }
        final ModifyResponse response = connection.modify(modifyRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Example 9 with ModifyRequestImpl

use of org.apache.directory.api.ldap.model.message.ModifyRequestImpl in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method writeBinaryAttribute.

public void writeBinaryAttribute(final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite, final ChaiRequestControl[] controls) throws ChaiUnavailableException, ChaiOperationException {
    try {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName(new Dn(entryDN));
        modifyRequest.addAllControls(figureControls(controls));
        {
            final Modification modification = new DefaultModification();
            modification.setOperation(overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE);
            modification.setAttribute(new DefaultAttribute(attributeName, values));
            modifyRequest.addModification(modification);
        }
        final ModifyResponse response = connection.modify(modifyRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Example 10 with ModifyRequestImpl

use of org.apache.directory.api.ldap.model.message.ModifyRequestImpl in project airavata by apache.

the class IULdapSSHAccountProvisioner method installSSHKey.

@Override
public String installSSHKey(String userId, String sshPublicKey) throws InvalidUsernameException {
    String username = getUsername(userId);
    String finalSSHPublicKey = sshPublicKey.trim();
    boolean success = withLdapConnection(ldapConnection -> {
        try {
            String dn = "uid=" + username + "," + ldapBaseDN;
            Entry entry = ldapConnection.lookup(dn);
            if (entry == null) {
                throw new RuntimeException("User [" + username + "] has no entry for " + dn);
            }
            boolean hasLdapPublicKey = entry.hasObjectClass(LDAP_PUBLIC_KEY_OBJECT_CLASS);
            ModifyRequest modifyRequest = new ModifyRequestImpl();
            modifyRequest.setName(new Dn(dn));
            // Add or Replace, depending on whether there is already an ldapPublicKey on the entry
            if (!hasLdapPublicKey) {
                modifyRequest.addModification(new DefaultAttribute("objectclass", LDAP_PUBLIC_KEY_OBJECT_CLASS), ModificationOperation.ADD_ATTRIBUTE);
                modifyRequest.addModification(new DefaultAttribute(SSH_PUBLIC_KEY_ATTRIBUTE_NAME, finalSSHPublicKey), ModificationOperation.ADD_ATTRIBUTE);
            } else {
                String oldSshPublicKey = entry.get(SSH_PUBLIC_KEY_ATTRIBUTE_NAME).getString();
                if (!oldSshPublicKey.equals(finalSSHPublicKey)) {
                    // Disallow overwriting the SSH key
                    throw new RuntimeException("User [" + username + "] already has an SSH public key in LDAP for [" + ldapBaseDN + "] and overwriting it isn't allowed.");
                // modifyRequest.addModification(new DefaultAttribute(SSH_PUBLIC_KEY_ATTRIBUTE_NAME,
                // sshPublicKey), ModificationOperation.REPLACE_ATTRIBUTE);
                } else {
                    // SSH key is already installed so just return
                    return true;
                }
            }
            ModifyResponse modifyResponse = ldapConnection.modify(modifyRequest);
            if (modifyResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
                logger.warn("installSSHKey ldap operation reported not being successful: " + modifyResponse);
            } else {
                logger.debug("installSSHKey ldap operation was successful: " + modifyResponse);
            }
            return true;
        } catch (LdapException e) {
            throw new RuntimeException(e);
        }
    });
    return username;
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Aggregations

ModifyRequest (org.apache.directory.api.ldap.model.message.ModifyRequest)11 ModifyRequestImpl (org.apache.directory.api.ldap.model.message.ModifyRequestImpl)11 ModifyResponse (org.apache.directory.api.ldap.model.message.ModifyResponse)9 Dn (org.apache.directory.api.ldap.model.name.Dn)8 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)7 Modification (org.apache.directory.api.ldap.model.entry.Modification)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)6 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)1 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)1 Entry (org.apache.directory.api.ldap.model.entry.Entry)1 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)1 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)1 Before (org.junit.Before)1