use of javax.naming.directory.SearchResult in project uPortal by Jasig.
the class LDAPGroupStore method processLdapResults.
protected void processLdapResults(NamingEnumeration results, ArrayList keys) {
// long loop1=System.currentTimeMillis();
try {
while (results.hasMore()) {
// long loop2 = System.currentTimeMillis();
// long cast1=System.currentTimeMillis();
// looping=looping+loop2-loop1;
SearchResult result = (SearchResult) results.next();
// long cast2 = System.currentTimeMillis();
// long get1 = System.currentTimeMillis();
Attributes ldapattribs = result.getAttributes();
// long get2 = System.currentTimeMillis();
// long set1 = System.currentTimeMillis();
Attribute attrib = ldapattribs.get(keyfield);
if (attrib != null) {
keys.add(String.valueOf(attrib.get()).toLowerCase());
}
// long set2 = System.currentTimeMillis();
// loop1=System.currentTimeMillis();
// casting=casting+cast2-cast1;
// setting=setting+set2-set1;
// getting=getting+get2-get1;
}
} catch (NamingException nex) {
log.error("LDAPGroupStore: error processing results", nex);
} finally {
try {
results.close();
} catch (Exception e) {
}
}
// long time5 = System.currentTimeMillis();
// System.out.println("Result processing took "+(time5-time1)+": "+getting+" for getting, "
// +setting+" for setting, "+casting+" for casting, "+looping+" for looping,"
// +(time5-loop1)+" for closing");
}
use of javax.naming.directory.SearchResult in project cxf by apache.
the class LDAPSearchTest method testSearch.
@Test
@Ignore
public void testSearch() throws URISyntaxException, NamingException {
LdapSearch ldapSearch = new LdapSearch("ldap://localhost:2389", "cn=Directory Manager,dc=example,dc=com", "test", 2);
NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree("dc=example, dc=com", "(cn=Testuser)");
while (answer.hasMore()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
Attribute cn = attrs.get("sn");
System.out.println(cn.get());
}
}
use of javax.naming.directory.SearchResult in project traccar by tananaev.
the class LdapProvider method lookupUser.
private SearchResult lookupUser(String accountName) throws NamingException {
InitialDirContext context = initContext();
String searchString = searchFilter.replace(":login", accountName);
SearchControls searchControls = new SearchControls();
String[] attributeFilter = { idAttribute, nameAttribute, mailAttribute };
searchControls.setReturningAttributes(attributeFilter);
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
SearchResult searchResult = null;
if (results.hasMoreElements()) {
searchResult = results.nextElement();
if (results.hasMoreElements()) {
Log.warning("Matched multiple users for the accountName: " + accountName);
return null;
}
}
return searchResult;
}
use of javax.naming.directory.SearchResult in project Payara by payara.
the class LDAPRealm method dynamicGroupSearch.
/**
* Search for group membership using the given connection.
*/
private List dynamicGroupSearch(DirContext ctx, String baseDN, String memberOfAttr, String filter, String target) throws NamingException {
List groupList = new ArrayList();
String[] targets = new String[] { memberOfAttr };
try {
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(targets);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Set this to false to avoid objects and hence exposing ldap object
// injection.
ctls.setReturningObjFlag(false);
NamingEnumeration e = ctx.search(baseDN, filter, ctls);
while (e.hasMore()) {
SearchResult res = (SearchResult) e.next();
Attribute isMemberOf = res.getAttributes().get(memberOfAttr);
if (isMemberOf != null) {
for (Enumeration values = isMemberOf.getAll(); values.hasMoreElements(); ) {
String groupDN = (String) values.nextElement();
LdapName dn = new LdapName(groupDN);
for (Rdn rdn : dn.getRdns()) {
if (rdn.getType().equalsIgnoreCase(target)) {
groupList.add(rdn.getValue());
break;
}
}
}
}
}
} catch (Exception e) {
_logger.log(Level.WARNING, "ldaprealm.searcherror", filter);
_logger.log(Level.WARNING, "security.exception", e);
}
return groupList;
}
use of javax.naming.directory.SearchResult in project scheduling by ow2-proactive.
the class LDAPLoginModule method getLDAPUserDN.
/**
* Connects anonymously to the LDAP server <code>url</code> and retrieve
* DN of the user <code>username</code>
*
* <p>
* @exception NamingException
* if a naming exception is encountered.
* <p>
*
* @return the String containing the UID of the user or null if the user is
* not found.
*/
private String getLDAPUserDN(String username) throws NamingException {
String userDN = null;
DirContext ctx = null;
try {
// Create the initial directory context
ctx = this.connectAndGetContext();
SearchControls sControl = new SearchControls();
sControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_USER_FILTER), username);
// looking for the user dn (distinguish name)
NamingEnumeration<SearchResult> answer = ctx.search(USERS_DN, filter, sControl);
if (answer.hasMoreElements()) {
SearchResult result = (SearchResult) answer.next();
userDN = result.getNameInNamespace();
if (logger.isDebugEnabled()) {
logger.debug("User " + username + " has LDAP entry " + userDN);
}
subject.getPrincipals().add(new UserNamePrincipal(username));
// looking for the user groups
String groupFilter = String.format(ldapProperties.getProperty(LDAPProperties.LDAP_GROUP_FILTER), userDN);
NamingEnumeration<SearchResult> groupResults = ctx.search(GROUPS_DN, groupFilter, sControl);
while (groupResults.hasMoreElements()) {
SearchResult res = (SearchResult) groupResults.next();
Attribute attr = res.getAttributes().get(ldapProperties.getProperty(LDAPProperties.LDAP_GROUPNAME_ATTR));
if (attr != null) {
String groupName = attr.get().toString();
subject.getPrincipals().add(new GroupNamePrincipal(groupName));
if (logger.isDebugEnabled()) {
logger.debug("User " + username + " is a member of group " + groupName);
}
}
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("User DN not found");
}
}
} catch (NamingException e) {
logger.error("Problem with the search in mode: " + AUTHENTICATION_METHOD + e);
throw e;
} finally {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException e) {
logger.error("", e);
logger.error("Problem closing LDAP connection: " + e.getMessage());
}
}
return userDN;
}
Aggregations