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);
}
}
Aggregations