Search in sources :

Example 1 with Users

use of com.b2international.snowowl.core.identity.Users in project snow-owl by b2ihealthcare.

the class LdapIdentityProvider method searchUsers.

@Override
public Promise<Users> searchUsers(Collection<String> usernames, int limit) {
    final ImmutableList.Builder<User> resultBuilder = ImmutableList.builder();
    final String uidProp = conf.getUserIdProperty();
    InitialLdapContext context = null;
    NamingEnumeration<SearchResult> searchResultEnumeration = null;
    try {
        context = createLdapContext();
        Collection<LdapRole> ldapRoles = getAllLdapRoles(context);
        searchResultEnumeration = context.search(conf.getBaseDn(), conf.getUserFilter(), createSearchControls(ATTRIBUTE_DN, uidProp));
        for (final SearchResult searchResult : ImmutableList.copyOf(Iterators.forEnumeration(searchResultEnumeration))) {
            final Attributes attributes = searchResult.getAttributes();
            if (hasAttribute(attributes, uidProp)) {
                final String userName = (String) attributes.get(uidProp).get();
                final List<Role> userRoles = ldapRoles.stream().filter(role -> role.getUniqueMembers().contains(searchResult.getNameInNamespace())).map(role -> new Role(role.getName(), role.getPermissions())).collect(Collectors.toList());
                resultBuilder.add(new User(userName, userRoles));
            }
        }
        final List<User> users = resultBuilder.build().stream().sorted((u1, u2) -> u1.getUsername().compareTo(u2.getUsername())).filter(user -> usernames.isEmpty() || usernames.contains(user.getUsername())).limit(limit).collect(Collectors.toList());
        return Promise.immediate(new Users(users, limit, users.size()));
    } catch (final NamingException e) {
        LOG.error("Couldn't search users/roles due to LDAP communication error: {}", e.getMessage(), e);
        throw new SnowowlRuntimeException(e);
    } finally {
        closeNamingEnumeration(searchResultEnumeration);
        closeLdapContext(context);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Promise(com.b2international.snowowl.core.events.util.Promise) LoggerFactory(org.slf4j.LoggerFactory) NamingException(javax.naming.NamingException) SearchControls(javax.naming.directory.SearchControls) Iterators(com.google.common.collect.Iterators) Attribute(javax.naming.directory.Attribute) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Permission(com.b2international.snowowl.core.identity.Permission) Context(javax.naming.Context) NoSuchElementException(java.util.NoSuchElementException) Hashtable(java.util.Hashtable) IdentityProvider(com.b2international.snowowl.core.identity.IdentityProvider) Logger(org.slf4j.Logger) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DirContext(javax.naming.directory.DirContext) Role(com.b2international.snowowl.core.identity.Role) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TreeMap(java.util.TreeMap) Attributes(javax.naming.directory.Attributes) Users(com.b2international.snowowl.core.identity.Users) NamingEnumeration(javax.naming.NamingEnumeration) User(com.b2international.snowowl.core.identity.User) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) SearchResult(javax.naming.directory.SearchResult) User(com.b2international.snowowl.core.identity.User) ImmutableList(com.google.common.collect.ImmutableList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) Users(com.b2international.snowowl.core.identity.Users) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Role(com.b2international.snowowl.core.identity.Role) InitialLdapContext(javax.naming.ldap.InitialLdapContext) NamingException(javax.naming.NamingException)

Aggregations

SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 Promise (com.b2international.snowowl.core.events.util.Promise)1 IdentityProvider (com.b2international.snowowl.core.identity.IdentityProvider)1 Permission (com.b2international.snowowl.core.identity.Permission)1 Role (com.b2international.snowowl.core.identity.Role)1 User (com.b2international.snowowl.core.identity.User)1 Users (com.b2international.snowowl.core.identity.Users)1 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 TreeMap (java.util.TreeMap)1