Search in sources :

Example 91 with InitialDirContext

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;
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) BasicAttributes(javax.naming.directory.BasicAttributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 92 with InitialDirContext

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;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 93 with InitialDirContext

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;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 94 with InitialDirContext

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;
}
Also used : Hashtable(java.util.Hashtable) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext)

Example 95 with InitialDirContext

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;
}
Also used : lombok.val(lombok.val) InitialDirContext(javax.naming.directory.InitialDirContext)

Aggregations

InitialDirContext (javax.naming.directory.InitialDirContext)131 DirContext (javax.naming.directory.DirContext)71 NamingException (javax.naming.NamingException)67 Hashtable (java.util.Hashtable)51 Attributes (javax.naming.directory.Attributes)34 Attribute (javax.naming.directory.Attribute)24 SearchResult (javax.naming.directory.SearchResult)22 SearchControls (javax.naming.directory.SearchControls)19 IOException (java.io.IOException)11 CommunicationException (javax.naming.CommunicationException)11 Test (org.junit.Test)11 Properties (java.util.Properties)10 BasicAttributes (javax.naming.directory.BasicAttributes)9 ArrayList (java.util.ArrayList)8 LoginException (javax.security.auth.login.LoginException)8 NameClassPair (javax.naming.NameClassPair)7 HashSet (java.util.HashSet)6 NamingEnumeration (javax.naming.NamingEnumeration)6 BasicAttribute (javax.naming.directory.BasicAttribute)6 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)5