use of javax.naming.directory.InitialDirContext in project polymap4-core by Polymap4.
the class GetRole method scanRole.
/**
* The Method scanRole search with ldap for the users domain roles
*
* @param userName userPrincipalName of the loginuser
*/
public void scanRole(String userName) {
String principal = "yourPrincipal";
String credentials = "yourCredential";
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://yourServer:yourPort/yourDomain");
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, credentials);
try {
// Erstellt eine Initial Directory Context
DirContext ctx = new InitialDirContext(env);
Attributes attr = new BasicAttributes("userPrincipalName", userName);
NamingEnumeration userData = ctx.search("cn=Users", attr);
while (userData.hasMoreElements()) {
SearchResult sr = (SearchResult) userData.next();
sr.getAttributes();
Attributes userAttributes = sr.getAttributes();
Attribute at = userAttributes.get("memberOf");
if (at != null) {
role = at.getAll();
} else {
role = null;
}
}
} catch (NamingException e) {
e.printStackTrace();
role = null;
}
}
use of javax.naming.directory.InitialDirContext in project traccar by traccar.
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.InitialDirContext in project traccar by traccar.
the class LdapProvider method isAdmin.
private boolean isAdmin(String accountName) {
if (this.adminFilter != null) {
try {
InitialDirContext context = initContext();
String searchString = adminFilter.replace(":login", accountName);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
if (results.hasMoreElements()) {
results.nextElement();
if (results.hasMoreElements()) {
Log.warning("Matched multiple users for the accountName: " + accountName);
return false;
}
return true;
}
} catch (NamingException e) {
return false;
}
}
return false;
}
use of javax.naming.directory.InitialDirContext in project tdq-studio-se by Talend.
the class EMailValidationIndicator method reset.
/*
* (non-Javadoc)
*
* @see org.talend.dataquality.indicators.impl.IndicatorImpl#reset()
*/
@Override
public boolean reset() {
boolean retValue = super.reset();
matchingValueCount = new Long(0L);
// Prepare naming directory context.
Hashtable<String, String> env = new Hashtable<String, String>();
// $NON-NLS-1$ //$NON-NLS-2$
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
// if the user add the paramter for: java.naming.provider.url, if has then add it to env
// Added TDQ-6918 Allow user add parameter: java.naming.provider.url
String dnsUrl = getDNSUrl();
if (dnsUrl != null) {
env.put(Context.PROVIDER_URL, dnsUrl);
}
try {
ictx = new InitialDirContext(env);
} catch (NamingException e) {
// $NON-NLS-1$
log.error("Invalid DNS in the user defined indicator: " + this.getName(), e);
retValue = false;
}
retValue = retValue && this.initParameters();
return retValue;
}
use of javax.naming.directory.InitialDirContext in project cas by apereo.
the class ValidateLdapConnectionCommand method getContext.
private static Pair<String, DirContext> getContext(final String ldapUrl, final String bindDn, final String bindCredential) {
val urls = StringUtils.commaDelimitedListToSet(ldapUrl);
for (val url : urls) {
if (ldapUrl != null && !ldapUrl.isEmpty()) {
LOGGER.info("Attempting connect to LDAP instance [{}]", url);
val env = getLdapDirectoryContextSettings(bindDn, bindCredential, url);
try {
return Pair.of(ldapUrl, new InitialDirContext(env));
} catch (final Exception e) {
LOGGER.error("Failed to connect to ldap instance [{}]", ldapUrl);
}
}
}
return null;
}
Aggregations