Oracle Internet Directory Utility class

/* * This class is a usage of the User/ Group class in oracle.ldap.util package * found in ldapjclnt11.jar. This can define a user/ Group using DN, GUID, or * a simple name representing the user. The following methods are exercised * in this sample program: * * - OIDUtils.authenticateUser() - to authenticate a user with the appropriate * credentials * - OIDUtils.createUser() - to create the user * - OIDUtils.createGroup() - to create the Group * - OIDUtils.addUsertoGroup() - to add the user in Group * - OIDUtils.upadteUsers() - to update the user * - OIDUtils.upadteGroups() - to update the Group * */ package utils; /** * * @author ryarakaraju Verion 1.0 */ import java.util.HashMap; import java.util.Map; import oracle.ldap.util.jndi.*; import javax.naming.*; import javax.naming.directory.*; import oracle.ldap.util.UtilException; public class OIDUtils { /* createUser(String[]) will create user in OID. this reuires context value to make a conection with OID. Based on the subscriber it will add user in OID. */ /** * The OU (organizational unit) to add users to */ private static final String USERS_OU = "cn=Users,dc=myorg, dc=com"; /** * The OU (organizational unit) to add groups to */ private static final String GROUPS_OU = "cn=Groups,dc=myorg, dc=com"; /** * The OU (organizational unit) to add permissions to */ private static final String PERMISSIONS_OU = "cn=Permissions,dc=myorg, dc=com"; /** * The connection, through a <code>DirContext</code>, to LDAP */ private final InitialDirContext context; private final String hostname; private final String port; private final String username; private final String password; public OIDUtils(String hostname, String port, String username, String password) throws NamingException { long time = 100000000L; context = ConnectionUtil.getDefaultDirCtx(hostname, port, "cn=orcladmin", "abc1234", time); // Only save data if we got connected this.hostname = hostname; this.port = port; this.username = hostname; this.password = password; } public void createUser(String username, String firstName, String lastName, String password) throws NamingException { // Create a container set of attributes Attributes container = new BasicAttributes(); // Create the objectclass to add Attribute objClasses = new BasicAttribute("objectClass"); objClasses.add("top"); objClasses.add("person"); objClasses.add("organizationalPerson"); objClasses.add("inetOrgPerson"); // Assign the username, first name, and last name String cnValue = new StringBuffer(firstName) .append(" ") .append(lastName) .toString(); Attribute cn = new BasicAttribute("cn", cnValue); Attribute givenName = new BasicAttribute("givenName", firstName); Attribute sn = new BasicAttribute("sn", lastName); Attribute uid = new BasicAttribute("uid", username); // Add password Attribute userPassword = new BasicAttribute("userpassword", password); // Add these to the container container.put(objClasses); container.put(cn); container.put(sn); container.put(givenName); container.put(uid); container.put(userPassword); // Create the entry context.createSubcontext(getUserDN(username), container); } public void deleteUser(String username) throws NamingException { try { context.destroySubcontext(getUserDN(username)); } catch (NameNotFoundException e) { // If the user is not found, ignore the error } } public boolean isValidUser(String username, String password) throws UtilException { try { InitialDirContext context = ConnectionUtil.getDefaultDirCtx("144.243.32.137", "3061", getUserDN(username), password); return true; } catch (javax.naming.NameNotFoundException e) { } catch (NamingException e) { // Any other error indicates couldn't log user in return false; } return false; } public void createGroup(String name, String description) throws NamingException { // Create a container set of attributes Attributes container = new BasicAttributes(); // Create the objectclass to add Attribute objClasses = new BasicAttribute("objectClass"); objClasses.add("top"); objClasses.add("groupOfUniqueNames"); objClasses.add("orclgroup"); // Assign the name and description to the group Attribute cn = new BasicAttribute("cn", name); Attribute desc = new BasicAttribute("description", description); // Add these to the container container.put(objClasses); container.put(cn); container.put(desc); // Create the entry context.createSubcontext(getGroupDN(name), container); } public void deleteGroup(String name) throws NamingException { try { context.destroySubcontext(getGroupDN(name)); } catch (NameNotFoundException e) { // If the group is not found, ignore the error } } private String getUserDN(String username) { return new StringBuffer() .append("uid=") .append(username) .append(",") .append(USERS_OU) .toString(); } private String getUserUID(String userDN) { int start = userDN.indexOf("="); int end = userDN.indexOf(","); if (end == -1) { end = userDN.length(); } return userDN.substring(start+1, end); } private String getGroupDN(String name) { return new StringBuffer() .append("cn=") .append(name) .append(",") .append(GROUPS_OU) .toString(); } private String getGroupCN(String groupDN) { int start = groupDN.indexOf("="); int end = groupDN.indexOf(","); if (end == -1) { end = groupDN.length(); } return groupDN.substring(start+1, end); } private String getPermissionDN(String name) { return new StringBuffer() .append("cn=") .append(name) .append(",") .append(PERMISSIONS_OU) .toString(); } private String getPermissionCN(String permissionDN) { int start = permissionDN.indexOf("="); int end = permissionDN.indexOf(","); if (end == -1) { end = permissionDN.length(); } return permissionDN.substring(start+1, end); } }
* This class is a usage of the User/ Group class in oracle.ldap.util package
* found in ldapjclnt11.jar. This can define a user/ Group using DN, GUID, or
* a simple name representing the user. The following methods are exercised
* in this sample program:
*
* - OIDUtils.authenticateUser() - to authenticate a user with the appropriate
* credentials
* - OIDUtils.createUser() - to create the user
* - OIDUtils.createGroup() - to create the Group
* - OIDUtils.addUsertoGroup() - to add the user in Group
* - OIDUtils.upadteUsers() - to update the user
* - OIDUtils.upadteGroups() - to update the Group

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.