Friday, 11 December 2015

Java code to Create/Delete user in LDAP

//Java code to Create/Delete user in LDAP

// Establish a LDAP connection

public static LdapContext getInitialLdapContext()
        throws Exception
    {
        LdapContext ctx = null;
        String dnusername = "cn=root,ou=users, o=sample,c=country"; //Admin user name
        String dnpwd = "abcd@123"; //Admin Password
        try
        {
            Control rctls[] = {new PasswordPolicyControl(true)};
            String ldapHostName = bundle.getString("LDAP_IP");
            String ldapHostRMIPort = bundle.getString("LDAP_PORT");
            String providerURL = (new StringBuilder("ldap://")).append(ldapHostName).append(":").append(ldapHostRMIPort).toString();
            System.out.println("URL  =  "+ providerURL);
            Hashtable env = new Hashtable();
            env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
            env.put("java.naming.provider.url", providerURL);
            env.put("java.naming.security.authentication", "simple");
            env.put("java.naming.security.principal", dnusername);
            env.put("java.naming.security.credentials", dnpwd);
            ctx = new InitialLdapContext(env, rctls);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return ctx;
    }
    private static String getUserDN(String username)
    {
        return (new StringBuilder("cn=")).append(username).append(",").append("ou=users, o=sample,c=country").toString();
    }
   
   
    //Create a user
   private static void addUser(String username, String surname,  String titleprop, String mailprop, String employeenumberprop, String mobileprop, String displayNameprop, String password)
       // throws NamingException
    {
        private static DirContext context;
        context = getInitialLdapContext();
        Attributes container = new BasicAttributes();
        Attribute objClasses = new BasicAttribute("objectClass");
        objClasses.add("organizationalPerson");
        objClasses.add("inetOrgPerson");
        objClasses.add("person");
        objClasses.add("top");
        Attribute cn = new BasicAttribute("cn", username);
        Attribute sn = new BasicAttribute("sn", surname);
        Attribute uid = new BasicAttribute("uid", username);
        Attribute title = new BasicAttribute("title", titleprop);
        Attribute mail = new BasicAttribute("mail", mailprop);
        Attribute mobile = new BasicAttribute("mobile", mobileprop);
        Attribute employeeNumber = new BasicAttribute("employeeNumber", employeenumberprop);
        Attribute userPassword = new BasicAttribute("userpassword", password);
        Attribute  displayName = new BasicAttribute("displayName",displayNameprop);
        container.put(objClasses);
        container.put(sn);
        container.put(uid);
        container.put(title);
        container.put(mail);
        container.put(mobile);
        container.put(employeeNumber);
        container.put(countryCode);
        container.put(displayName);

        container.put(userPassword);
       try{
        context.createSubcontext(getUserDN(username), container);
       }
       catch (Exception e) {
        // TODO: handle exception
           log.info(e);
    }
       
       System.out.println((new StringBuilder(String.valueOf(password))).append("- is the Password for user- ").append(username).toString());
       
    }
   
    //Delete a user
    private static void deleteUser(String username)
        throws NamingException
    {
        try
        {
            context.destroySubcontext(getUserDN(username));
            System.out.println((new StringBuilder("User has been deleted successfully - ")).append(username).toString());
        }
        catch(NameNotFoundException namenotfoundexception) {
            log.info(namenotfoundexception);
        }
    }
   

   

No comments:

Post a Comment