// 创建用户 public void createUser(UserInformationObject userobj) throws NamingException { // 用户对象为空 if (userobj == null) { throw new NamingException("No user informationn"); } // 检查uid String uid = userobj.getProperty(UserInformationObject.USER_ID); if (uid == null && uid.length() == 0) { throw new NamingException("No uid you specifyn"); } if (isUserexist(uid)) { throw new NamingException("The user(uid: " + uid + ") is exist!n"); } // 检查firstName String firstName = userobj.getProperty(UserInformationObject.FIRST_NAME); if (firstName == null || firstName.length() == 0) { throw new NamingException("No first name you specify!n"); } // 检查lastName String lastName = userobj.getProperty(UserInformationObject.LAST_NAME); if (lastName == null || lastName.length() == 0) { throw new NamingException("No last name you specify!n"); } // 检查commonName String commonName = userobj.getProperty(UserInformationObject.COMMON_NAME); if (commonName == null || commonName.length() == 0) { throw new NamingException("No common name you specify!n"); } String password = userobj.getProperty(UserInformationObject.PASSWORD); String email = userobj.getProperty(UserInformationObject.EMAIL); String phone = userobj.getProperty(UserInformationObject.PHONE); String fax = userobj.getProperty(UserInformationObject.FAX); Attributes attrs = new BasicAttributes(); // 设置属性 Attribute objclass = new BasicAttribute("objectclass"); objclass.add("top"); objclass.add("person"); objclass.add("organizationalPerson"); objclass.add("inetorgperson"); attrs.put(objclass); putAttribute(attrs, "cn", commonName); putAttribute(attrs, "givenname", firstName); putAttribute(attrs, "sn", lastName); putAttribute(attrs, "uid", uid); putAttribute(attrs, "userpassword", password); putAttribute(attrs, "mail", email); putAttribute(attrs, "telephonenumber", phone); putAttribute(attrs, "facsimiletelephonenumber", fax); // 添加用户节点 ctx.bind("uid=" + uid + "," + BASE_DN, null, attrs); System.out.println("User(uid: " + uid + ") created.n"); }
// 修改用户信息 public void modifyUser(UserInformationObject userobj) throws NamingException { // 用户对象为空 if (userobj == null) { throw new NamingException("No user information!n"); } // 检查uid String uid = userobj.getProperty(UserInformationObject.USER_ID); if (uid == null && uid.length() == 0) { throw new NamingException("No uid you specify!n"); } if (!isUserexist(uid)) { throw new NamingException("The user(uid: " + uid + ") does not exist!n"); } int size = userobj.size(); // 用户属性的个数 // 如果属性个数只有一个,那么只设置了uid,不用修改用户属性 if (size > 1) { String password = userobj.getProperty(UserInformationObject.PASSWORD); String email = userobj.getProperty(UserInformationObject.EMAIL); String phone = userobj.getProperty(UserInformationObject.PHONE); String fax = userobj.getProperty(UserInformationObject.FAX); String commonName = userobj.getProperty(UserInformationObject.COMMON_NAME); String firstName = userobj.getProperty(UserInformationObject.FIRST_NAME); String lastName = userobj.getProperty(UserInformationObject.LAST_NAME); // 设置属性 Attributes attrs = new BasicAttributes(); putAttribute(attrs, "cn", commonName); putAttribute(attrs, "givenname", firstName); putAttribute(attrs, "sn", lastName); putAttribute(attrs, "userpassword", password); putAttribute(attrs, "mail", email); putAttribute(attrs, "telephonenumber", phone); putAttribute(attrs, "facsimiletelephonenumber", fax); // 修改属性 ctx.modifyAttributes("uid=" + uid + "," + BASE_DN, DirContext.REPLACE_ATTRIBUTE, attrs); System.out.println("User(uid: " + uid + ") information modified.n"); } else { throw new NamingException("No modify information you specify!n"); } }