@Override public void createImapUser(LdapUser user, String parentDn) { String cn = buildCn(user.getFirstName(), user.getLastName()); int uid = getMaxUid() + 1; BasicAttributes attrs = getBasicAttributesForUser(user, cn); attrs.get("objectclass").add("posixAccount"); attrs.put("gidnumber", props.getLdapGidNumber()); attrs.put(props.getLdapUidAttribute(), uid + ""); attrs.put("homedirectory", props.getHomeDirBase() + "/" + cn); attrs.put("uid", cn); attrs.put("forward", cn + "@localhost"); try { DirContext ctxt = connect(); ctxt.bind("cn=" + cn + "," + parentDn, null, attrs); } catch (NamingException e) { log.error("Error creating user", e); throw new RuntimeException(e); } }
public LdapAccountServiceImpl() { super(); props = AppProps.getDefaultInstance(); this.ldapUrl = props.getLdapUrl(); this.basedn = props.getLdapBaseDN(); this.binddn = props.getLdapBindDN(); this.bindpw = props.getBindPw(); this.incubatorDn = props.getLdapIncubatorDn(); }
private int getMaxUid() { int ret = -2; SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); sc.setReturningAttributes(new String[] {props.getLdapUidAttribute()}); String filter = "objectclass=posixAccount"; DirContext ctxt; try { ctxt = connect(); NamingEnumeration<SearchResult> results = ctxt.search(basedn, filter, sc); while (results.hasMore()) { Attributes attrs = results.next().getAttributes(); int uidNumber = Integer.parseInt(attrs.get(props.getLdapUidAttribute()).get().toString()); if (uidNumber > ret) { ret = uidNumber; } } } catch (Exception e) { log.fatal(e); throw new RuntimeException(e); } return ret; }
@Override public List<String> getValidParents() { LinkedList<String> ret = new LinkedList<String>(); SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); DirContext ctxt; try { ctxt = connect(); String filter = props.getLdapFilterForValidParents(); NamingEnumeration<SearchResult> results = ctxt.search(basedn, filter, sc); while (results.hasMore()) { SearchResult result = results.next(); String dn = result.getNameInNamespace(); ret.add(dn); } } catch (NamingException e) { log.fatal(e); throw new RuntimeException(e); } return ret; }