mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
refactor GroupResolver + GroupCollector
This commit is contained in:
@@ -1,70 +1,72 @@
|
||||
package sonia.scm.group;
|
||||
|
||||
import com.cronutils.utils.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.cache.Cache;
|
||||
import sonia.scm.cache.CacheManager;
|
||||
import sonia.scm.security.GroupCollector;
|
||||
import sonia.scm.security.GroupResolver;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Collect groups for a certain principal.
|
||||
* <strong>Warning</strong>: The class is only for internal use and should never used directly.
|
||||
*/
|
||||
class DefaultGroupCollector implements GroupCollector {
|
||||
@Singleton
|
||||
public class DefaultGroupCollector implements GroupCollector {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DefaultGroupCollector.class);
|
||||
|
||||
/** Field description */
|
||||
public static final String CACHE_NAME = "sonia.cache.externalGroups";
|
||||
|
||||
/** Field description */
|
||||
private final Cache<String, Set<String>> cache;
|
||||
private Set<GroupResolver> groupResolvers;
|
||||
@VisibleForTesting
|
||||
static final String CACHE_NAME = "sonia.cache.externalGroups";
|
||||
|
||||
private final GroupDAO groupDAO;
|
||||
private final Cache<String, Set<String>> cache;
|
||||
private final Set<GroupResolver> groupResolvers;
|
||||
|
||||
DefaultGroupCollector(GroupDAO groupDAO, CacheManager cacheManager, Set<GroupResolver> groupResolvers) {
|
||||
@Inject
|
||||
public DefaultGroupCollector(GroupDAO groupDAO, CacheManager cacheManager, Set<GroupResolver> groupResolvers) {
|
||||
this.groupDAO = groupDAO;
|
||||
this.cache = cacheManager.getCache(CACHE_NAME);
|
||||
this.cache = cacheManager.getCache(CACHE_NAME);
|
||||
this.groupResolvers = groupResolvers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> collect(String principal) {
|
||||
public Set<String> collect(String principal) {
|
||||
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
|
||||
|
||||
builder.add(AUTHENTICATED);
|
||||
builder.addAll(resolveExternalGroups(principal));
|
||||
appendInternalGroups(principal, builder);
|
||||
|
||||
Set<String> groups = builder.build();
|
||||
LOG.debug("collected following groups for principal {}: {}", principal, groups);
|
||||
return groups;
|
||||
}
|
||||
|
||||
private void appendInternalGroups(String principal, ImmutableSet.Builder<String> builder) {
|
||||
for (Group group : groupDAO.getAll()) {
|
||||
if (group.isMember(principal)) {
|
||||
builder.add(group.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> resolveExternalGroups(String principal) {
|
||||
Set<String> externalGroups = cache.get(principal);
|
||||
|
||||
if (externalGroups == null) {
|
||||
ImmutableSet.Builder<String> newExternalGroups = ImmutableSet.builder();
|
||||
|
||||
for (GroupResolver groupResolver : groupResolvers) {
|
||||
Iterable<String> groups = groupResolver.resolveGroups(principal);
|
||||
groups.forEach(newExternalGroups::add);
|
||||
newExternalGroups.addAll(groupResolver.resolve(principal));
|
||||
}
|
||||
|
||||
cache.put(principal, newExternalGroups.build());
|
||||
externalGroups = newExternalGroups.build();
|
||||
cache.put(principal, externalGroups);
|
||||
}
|
||||
|
||||
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
|
||||
|
||||
builder.add(GroupNames.AUTHENTICATED);
|
||||
|
||||
for (String group : externalGroups) {
|
||||
builder.add(group);
|
||||
}
|
||||
|
||||
for (Group group : groupDAO.getAll()) {
|
||||
if (group.isMember(principal)) {
|
||||
builder.add(group.getName());
|
||||
}
|
||||
}
|
||||
|
||||
GroupNames groups = new GroupNames(builder.build());
|
||||
LOG.debug("collected following groups for principal {}: {}", principal, groups);
|
||||
return groups;
|
||||
return externalGroups;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user