下面列出了javax.naming.directory.DirContext#modifyAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
// }
}
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);
}
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();
}
/**
* 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()]));
}
}
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);
}
}
/**
* 修改
*
* @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;
}
}