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);
}
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);
}
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());
}
}
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());
}
}
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;
}
Aggregations