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

The following examples show how to use javax.naming.directory.DirContext#createSubcontext() . 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: LdapUtil.java    From herd-mdl with Apache License 2.0 8 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: LdapUtil.java    From herd-mdl with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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 );
    }
}
 
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: LdapUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 添加
 */
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);
	}
}