/**
*
* @param groupName The name of the AD group.
* @return The AD attributes for the group or null if error.
* @throws NamingException
*/
public Attributes getADGroupAttributes(String groupName) throws NamingException {
this.userName = null;
String searchFilter = "(&(objectClass=group)(cn=" + groupName + "))";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search("dc=MYDOMAIN,dc=LOCAL", searchFilter, searchControls);
SearchResult searchResult = null;
if(results.hasMoreElements()) {
searchResult = (SearchResult) results.nextElement();
//make sure there is not another item available, there should be only 1 match
if(results.hasMoreElements()) {
this.strRes = "Matched multiple groups for the group name: " + groupName;
return null;
}
}
else{
this.strRes = "No groups found";
return null;
}
return searchResult.getAttributes();
}
/**
*
* @param groupName The group name to search for. Can use wild cards.
* @return A comma delimited list of the AD group names
* @throws NamingException
*/
public String searchforADGroup(String groupName) throws NamingException {
this.userName = null;
String searchFilter = "(&(objectClass=group)(cn=" + groupName + "))";
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search("dc=MYDOMAIN,dc=LOCAL", searchFilter, searchControls);
SearchResult searchResult = null;
String strGroups = "";
if(results!= null) {
try {
while(results.hasMore()) {
searchResult = (SearchResult) results.nextElement();
strGroups = strGroups + searchResult.getAttributes().get("cn") + ",";
}
} catch (Exception e) {
}
}
else{
this.strRes = "No groups found";
return null;
}
return strGroups;
}
No words wasted! Getting to the point about the work I do, the problems I deal with, and some links to posts about where I work.
Translate
Thursday, October 8, 2015
Java - Search for AD Groups and List Group Attributes
Added the ability to search for AD groups and get a Group's Attributes to my LDAP class:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Thank you for commenting!