Java Code Examples for javax.naming.directory.DirContext#modifyAttributes()

The following examples show how to use javax.naming.directory.DirContext#modifyAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapDao.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public void modify(final DirContext ctx, final T obj, final List<ModificationItem> modificationItems) throws NamingException
{
  final Object id = getId(obj);
  // The dn is may-be changed, so find the original dn by id:
  final T origObject = findById(ctx, id, obj.getOrganizationalUnit());
  if (origObject == null) {
    throw new RuntimeException("Object with id "
        + id
        + " not found in search base '"
        + StringHelper.listToString(",", obj.getOrganizationalUnit())
        + "'. Can't modify the object: "
        + obj);
  }
  final String dn = origObject.getDn();
  log.info("Modify attributes of " + getObjectClass() + ": " + dn + ": " + getLogInfo(obj));
  final ModificationItem[] items = modificationItems.toArray(new ModificationItem[modificationItems.size()]);
  ctx.modifyAttributes(dn, items);
  // Don't move object.
  // if (obj.getDn() != null && StringUtils.equals(dn, obj.getDn()) == false) {
  // log.info("DN of object is changed from '" + dn + "' to '" + obj.getDn());
  // ctx.rename(dn, obj.getDn());
  // }
}
 
Example 2
Source File: LdapUtil.java    From herd-mdl with Apache License 2.0 5 votes vote down vote up
private static void modifyAttributes(String userId, String ou, String groupName, int modOp) throws NamingException {
    DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
    String memberEntryDN = constructEntryCn(userId, ou);
    String groupDn = String.format("cn=%s,ou=%s,%s", groupName, OU_GROUPS, BASE_DN);

    BasicAttribute member = new BasicAttribute("member", memberEntryDN);
    Attributes atts = new BasicAttributes();
    atts.put(member);
    ldapContext.modifyAttributes(groupDn, modOp, atts);
}
 
Example 3
Source File: ldapConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void modify() throws AttributeModificationException, NamingException{
  //Get a reference to a directory context
  int modType;
  // decode the modification type to one which the context will understand
  switch (modifyType){
    case ldapConnection.MODIFY_REPLACE: // attributes require name=value pairs
      modType = DirContext.REPLACE_ATTRIBUTE;
      break;
    
    case ldapConnection.MODIFY_ADD:
      modType = DirContext.ADD_ATTRIBUTE; // attributes require name=value pairs
      break;

    case ldapConnection.MODIFY_DELETE:
      modType = DirContext.REMOVE_ATTRIBUTE; // attributes require names only
      break;
    default:
      modType = DirContext.REPLACE_ATTRIBUTE;

  }// switch

  DirContext ctx = new InitialDirContext(env);
  Attributes attributes = processAttributes();
  ctx.modifyAttributes(dn, modType, attributes);
  ctx.close();

}
 
Example 4
Source File: LdifScript.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * Adds/modifies ctx using entry information.
 *
 * @param ctx directory context to use for change.
 * @param e   entry with change description.
 * @throws NamingException if operation with directory failed.
 */
static void modify(DirContext ctx, final Entry e) throws NamingException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Processing " + e);
    }
    Attributes atts = e.getAttributes();
    final String rootDn = ctx.getNameInNamespace();
    if (atts != null) { //If add entry
        ctx.createSubcontext(getRelativeDN(rootDn, e.getDn()), e.getAttributes());
    } else if (e.isChangeDelete()) {
        ctx.destroySubcontext(getRelativeDN(rootDn, e.getDn()));
    } else if (e.isChangeModDn() || e.isChangeModRdn()) {
        Name newRdn;
        if (e.getNewSuperior() != null) { //If new superior
            newRdn = getRelativeDN(rootDn, e.getNewSuperior());
        } else { //otherwise use DN as a base
            newRdn = getRelativeDN(rootDn, e.getDn());
            newRdn.remove(newRdn.size() - 1);
        }
        newRdn.add(e.getNewRdn());
        ctx.addToEnvironment("java.naming.ldap.deleteRDN", String.valueOf(e.isDeleteOldRdn()));
        ctx.rename(getRelativeDN(rootDn, e.getDn()), newRdn);
        ctx.removeFromEnvironment("java.naming.ldap.deleteRDN");//a better solution to use the previous value

    } else {
        List<ModificationItem> items = e.getModificationItems();
        ctx.modifyAttributes(getRelativeDN(rootDn, e.getDn()),
                items.toArray(new ModificationItem[items.size()]));
    }
}
 
Example 5
Source File: LdapSender.java    From iaf with Apache License 2.0 5 votes vote down vote up
private String performOperationChangeUnicodePwd(String entryName, IPipeLineSession session, Map paramValueMap) throws SenderException, ParameterException {
	ModificationItem[] modificationItems = new ModificationItem[2];
	modificationItems[0] = new ModificationItem(
			DirContext.REMOVE_ATTRIBUTE,
			new BasicAttribute("unicodePwd", encodeUnicodePwd(paramValueMap.get("oldPassword"))));
	modificationItems[1] = new ModificationItem(
			DirContext.ADD_ATTRIBUTE,
			new BasicAttribute("unicodePwd", encodeUnicodePwd(paramValueMap.get("newPassword"))));
	DirContext dirContext = null;
	try{
		dirContext = getDirContext(paramValueMap);
		dirContext.modifyAttributes(entryName, modificationItems);
		return DEFAULT_RESULT_CHANGE_UNICODE_PWD_OK;
	} catch(NamingException e) {
		// https://wiki.servicenow.com/index.php?title=LDAP_Error_Codes:
		//   19 LDAP_CONSTRAINT_VIOLATION Indicates that the attribute value specified in a modify, add, or modify DN operation violates constraints placed on the attribute. The constraint can be one of size or content (string only, no binary).
		// AD:
		//   [LDAP: error code 19 - 0000052D: AtrErr: DSID-03191041, #1...
		if(e.getMessage().startsWith("[LDAP: error code 19 - ") ) {
			if (log.isDebugEnabled()) log.debug("Operation [" + getOperation()+ "] old password doesn't match or new password doesn't comply with policy for: " + entryName);
			return DEFAULT_RESULT_CHANGE_UNICODE_PWD_NOK;
		} else {
			storeLdapException(e, session);
			throw new SenderException("Exception in operation [" + getOperation()+ "] entryName ["+entryName+"]", e);
		}
	} finally {
		closeDirContext(dirContext);
	}
}
 
Example 6
Source File: LdapUtil.java    From jeecg with Apache License 2.0 4 votes vote down vote up
/**
 * 修改
 * 
 * @return
 * @throws IOException
 */
public static boolean modifyInformation(String dn, String employeeID,
		DirContext dc, String[] employeeArray) throws IOException {
	try {
		String[] modifyAttr = { "telephoneNumber" };
		// employeeArray.length - 1的目的去除员工编号
		ModificationItem[] modifyItems = new ModificationItem[employeeArray.length - 1];

		for (int i = 0; i < modifyAttr.length; i++) {
			String attrName = modifyAttr[i];
			Attribute attr = new BasicAttribute(attrName,
					employeeArray[i + 1]);
			modifyItems[i] = new ModificationItem(
					DirContext.REPLACE_ATTRIBUTE, attr);
		}

		/* 修改属性 */
		// Attribute attr0 = new BasicAttribute("telephoneNumber",
		// telephoneNumber);
		// modifyItems[0] = new
		// ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr0);

		/* 删除属性 */
		// Attribute attr0 = new BasicAttribute("description","陈轶");
		// modifyItems[0] = new
		// ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr0);

		/* 添加属性 */
		// Attribute attr0 = new BasicAttribute("employeeID", employeeID);
		// modifyItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
		// attr0);

		/* 修改属性 */
		dc.modifyAttributes(dn, modifyItems);
		return true;
	} catch (NamingException e) {
		e.printStackTrace();
		System.err.println("Error: " + e.getMessage());
		FileUtil.appendString(errorFile, "Error:" + e.getMessage() + "\n");
		return false;
	}
}