Search in sources :

Example 1 with LdapSchema

use of com.google.gerrit.server.auth.ldap.Helper.LdapSchema in project gerrit by GerritCodeReview.

the class LdapGroupBackend method suggestLdap.

private Set<GroupReference> suggestLdap(String name) {
    if (name.isEmpty()) {
        return Collections.emptySet();
    }
    Set<GroupReference> out = Sets.newTreeSet(GROUP_REF_NAME_COMPARATOR);
    try {
        DirContext ctx = helper.open();
        try {
            // Do exact lookups until there are at least 3 characters.
            name = Rdn.escapeValue(name) + ((name.length() >= 3) ? "*" : "");
            LdapSchema schema = helper.getSchema(ctx);
            ParameterizedString filter = ParameterizedString.asis(schema.groupPattern.replace(GROUPNAME, name).toString());
            Set<String> returnAttrs = new HashSet<>(schema.groupName.getParameterNames());
            Map<String, String> params = Collections.emptyMap();
            for (String groupBase : schema.groupBases) {
                LdapQuery query = new LdapQuery(groupBase, schema.groupScope, filter, returnAttrs);
                for (LdapQuery.Result res : query.query(ctx, params)) {
                    out.add(groupReference(schema.groupName, res));
                }
            }
        } finally {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.warn("Cannot close LDAP query handle", e);
            }
        }
    } catch (NamingException | LoginException e) {
        log.warn("Cannot query LDAP for groups matching requested name", e);
    }
    return out;
}
Also used : DirContext(javax.naming.directory.DirContext) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) LdapSchema(com.google.gerrit.server.auth.ldap.Helper.LdapSchema) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) GroupReference(com.google.gerrit.common.data.GroupReference) HashSet(java.util.HashSet)

Aggregations

GroupReference (com.google.gerrit.common.data.GroupReference)1 ParameterizedString (com.google.gerrit.common.data.ParameterizedString)1 LdapSchema (com.google.gerrit.server.auth.ldap.Helper.LdapSchema)1 HashSet (java.util.HashSet)1 NamingException (javax.naming.NamingException)1 DirContext (javax.naming.directory.DirContext)1 LoginException (javax.security.auth.login.LoginException)1