下面列出了javax.naming.directory.DirContext#createSubcontext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create ldap AD group and add user to newly created AD group
*
* @param adGroupName ldap AD group name to create
* @param userId uid of existing ldap user to be added to newly created AD group
* @throws NamingException
*/
public static void createAdGroup(String adGroupName, String userId) throws NamingException {
DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
String groupDn = constructGroupDn(adGroupName, OU_GROUPS);
String memberDn = constructEntryCn(userId, OU_PEOPLE);
//Create attributes to be associated with the new group
Attributes attrs = new BasicAttributes(true);
Attribute objclass = new BasicAttribute("objectClass");
objclass.add("top");
objclass.add("groupOfNames");
attrs.put("cn", adGroupName);
attrs.put(objclass);
BasicAttribute member = new BasicAttribute("member", memberDn);
attrs.put(member);
ldapContext.createSubcontext(groupDn, attrs);
LOGGER.info("Created group: " + adGroupName);
}
/**
* create ldap user with provided user id and user password
*
* @param user new ldap user to create
* @throws NamingException
*/
public static void addEntry(User user) throws NamingException {
String username = user.getUsername();
Attribute userCn = new BasicAttribute("cn", user.getUsername());
Attribute userSn = new BasicAttribute("sn", "null");
Attribute uid = new BasicAttribute("uid", user.getUsername());
Attribute uidNumber = new BasicAttribute("uidNumber", String.valueOf(listEntries() + 1));
Attribute gidNumber = new BasicAttribute("gidNumber", String.valueOf(1001));
Attribute homeDirectory = new BasicAttribute("homeDirectory", "/home/" + username);
Attribute mail = new BasicAttribute("mail", username + "@" + DOMAIN_NAME);
Attribute loginShell = new BasicAttribute("loginShell", "/bin/bash");
Attribute userUserPassword = new BasicAttribute("userPassword", user.getPassword());
//ObjectClass attributes
Attribute objectClass = new BasicAttribute("objectClass");
objectClass.add("inetOrgPerson");
objectClass.add("posixAccount");
Attributes entry = new BasicAttributes();
entry.put(userCn);
entry.put(userSn);
entry.put(userUserPassword);
entry.put(objectClass);
entry.put(uid);
entry.put(uidNumber);
entry.put(gidNumber);
entry.put(homeDirectory);
entry.put(mail);
entry.put(loginShell);
String ou = user.getOu() == null ? "People" : user.getOu();
String entryDN = constructEntryCn(user.getUsername(), ou);
DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
ldapContext.createSubcontext(entryDN, entry);
LOGGER.info("Added Entry :" + entryDN);
}
@LdapOperation
@ModifyOperation
public final void createEntry( final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes )
throws ChaiOperationException, ChaiUnavailableException
{
activityPreCheck();
getInputValidator().createEntry( entryDN, baseObjectClasses, stringAttributes );
final Attributes attrs = new BasicAttributes();
//Put in the base object class an attribute
final BasicAttribute objectClassAttr = new BasicAttribute( ChaiConstant.ATTR_LDAP_OBJECTCLASS );
for ( final String loopClass : baseObjectClasses )
{
objectClassAttr.add( loopClass );
}
attrs.put( objectClassAttr );
//Add each of the attributes required.
for ( final Map.Entry<String, String> entry : stringAttributes.entrySet() )
{
attrs.put( entry.getKey(), entry.getValue() );
}
// Create the object.
final DirContext ldapConnection = getLdapConnection();
try
{
ldapConnection.createSubcontext( addJndiEscape( entryDN ), attrs );
}
catch ( NamingException e )
{
convertNamingException( e );
}
}
/**
* 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()]));
}
}
/**
* 添加
*/
public static void add(String newUserName, DirContext dc) {
try {
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("sAMAccountName");
objclassSet.add("employeeID");
attrs.put(objclassSet);
attrs.put("ou", newUserName);
dc.createSubcontext("ou=" + newUserName + "," + ROOT, attrs);
} catch (Exception e) {
e.printStackTrace();
//System.out.println("Exception in add():" + e);
}
}