use of javax.naming.ldap.LdapContext in project jackrabbit-oak by apache.
the class InternalLdapServer method addMembers.
public void addMembers(String groupDN, Iterable<String> memberDNs) throws Exception {
LdapContext ctxt = getWiredContext();
Attribute attr = new BasicAttribute("member");
for (String dn : memberDNs) {
attr.add(dn);
}
BasicAttributes attrs = new BasicAttributes();
attrs.put(attr);
ctxt.modifyAttributes(groupDN, DirContext.ADD_ATTRIBUTE, attrs);
}
use of javax.naming.ldap.LdapContext in project jackrabbit-oak by apache.
the class InternalLdapServer method removeMember.
public void removeMember(String groupDN, String memberDN) throws Exception {
LdapContext ctxt = getWiredContext();
BasicAttributes attrs = new BasicAttributes();
attrs.put("member", memberDN);
ctxt.modifyAttributes(groupDN, DirContext.REMOVE_ATTRIBUTE, attrs);
}
use of javax.naming.ldap.LdapContext in project wildfly by wildfly.
the class LdapUrlTestServlet method runSearch.
/**
* Try to search in LDAP with search base containing URL. Also try to retrieve RequestControls from LdapContext.
*
* @param hostname
* @return
* @throws Exception
*/
public static String runSearch(final String hostname, boolean testLdapCtx) throws Exception {
final StringBuilder result = new StringBuilder();
final String ldapUrl = "ldap://" + (hostname == null ? "localhost" : hostname) + ":10389";
final Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "secret");
final SearchControls ctl = new SearchControls();
ctl.setReturningAttributes(new String[] { "cn" });
DirContext dirCtx = null;
if (testLdapCtx) {
// LdapContext must also work
LdapContext ldapCtx = new InitialLdapContext(env, null);
// next line tests if the LdapContext works
ldapCtx.getRequestControls();
dirCtx = ldapCtx;
} else {
dirCtx = new InitialDirContext(env);
}
final NamingEnumeration<SearchResult> nenum = dirCtx.search(ldapUrl + "/dc=jboss,dc=org", "(uid=jduke)", ctl);
while (nenum.hasMore()) {
SearchResult sr = nenum.next();
Attributes attrs = sr.getAttributes();
result.append("cn=").append(attrs.get("cn").get());
}
dirCtx.close();
return result.toString();
}
use of javax.naming.ldap.LdapContext in project cloudstack by apache.
the class LdapManagerImpl method addConfiguration.
@Override
public LdapConfigurationResponse addConfiguration(final String hostname, final int port) throws InvalidParameterValueException {
LdapConfigurationVO configuration = _ldapConfigurationDao.findByHostname(hostname);
if (configuration == null) {
LdapContext context = null;
try {
final String providerUrl = "ldap://" + hostname + ":" + port;
context = _ldapContextFactory.createBindContext(providerUrl);
configuration = new LdapConfigurationVO(hostname, port);
_ldapConfigurationDao.persist(configuration);
s_logger.info("Added new ldap server with hostname: " + hostname);
return new LdapConfigurationResponse(hostname, port);
} catch (NamingException | IOException e) {
s_logger.debug("NamingException while doing an LDAP bind", e);
throw new InvalidParameterValueException("Unable to bind to the given LDAP server");
} finally {
closeContext(context);
}
} else {
throw new InvalidParameterValueException("Duplicate configuration");
}
}
use of javax.naming.ldap.LdapContext in project cloudstack by apache.
the class LdapManagerImpl method getUser.
@Override
public LdapUser getUser(final String username) throws NoLdapUserMatchingQueryException {
LdapContext context = null;
try {
context = _ldapContextFactory.createBindContext();
final String escapedUsername = LdapUtils.escapeLDAPSearchFilter(username);
return _ldapUserManagerFactory.getInstance(_ldapConfiguration.getLdapProvider()).getUser(escapedUsername, context);
} catch (NamingException | IOException e) {
s_logger.debug("ldap Exception: ", e);
throw new NoLdapUserMatchingQueryException("No Ldap User found for username: " + username);
} finally {
closeContext(context);
}
}
Aggregations