Search in sources :

Example 6 with AccessSection

use of com.google.gerrit.entities.AccessSection in project gerrit by GerritCodeReview.

the class SectionSortCacheTest method sortMultiElementsWithDuplicates.

@Test
public void sortMultiElementsWithDuplicates() {
    AccessSection sectionAClone = sectionA.toBuilder().build();
    AccessSection sectionBClone = sectionB.toBuilder().build();
    AccessSection[] input = { sectionBClone, sectionA, sectionAClone, sectionA, sectionB };
    List<AccessSection> sorted = Arrays.asList(input);
    sectionSortCache.sort(REF_BASE, sorted);
    // Cache preserves relative order (reference equality) for identical elements
    AccessSection[] expected = { sectionBClone, sectionB, sectionA, sectionAClone, sectionA };
    for (int i = 0; i < sorted.size(); i++) {
        assertThat(sorted.get(i)).isSameInstanceAs(expected[i]);
    }
}
Also used : AccessSection(com.google.gerrit.entities.AccessSection) Test(org.junit.Test)

Example 7 with AccessSection

use of com.google.gerrit.entities.AccessSection in project gerrit by GerritCodeReview.

the class AccessSectionSerializerTest method roundTripWithMinimalValues.

@Test
public void roundTripWithMinimalValues() {
    AccessSection autoValue = AccessSection.builder("refs/test").build();
    assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
Also used : AccessSection(com.google.gerrit.entities.AccessSection) Test(org.junit.Test)

Example 8 with AccessSection

use of com.google.gerrit.entities.AccessSection in project gerrit by GerritCodeReview.

the class ProjectConfig method saveAccessSections.

private void saveAccessSections(Config rc, Set<AccountGroup.UUID> keepGroups) {
    unsetSection(rc, CAPABILITY);
    AccessSection capability = accessSections.get(AccessSection.GLOBAL_CAPABILITIES);
    if (capability != null) {
        Set<String> have = new HashSet<>();
        for (Permission permission : sort(capability.getPermissions())) {
            have.add(permission.getName().toLowerCase());
            boolean needRange = GlobalCapability.hasRange(permission.getName());
            List<String> rules = new ArrayList<>();
            for (PermissionRule rule : sort(permission.getRules())) {
                GroupReference group = resolve(rule.getGroup());
                if (group.getUUID() != null) {
                    keepGroups.add(group.getUUID());
                }
                rules.add(rule.toBuilder().setGroup(group).build().asString(needRange));
            }
            rc.setStringList(CAPABILITY, null, permission.getName(), rules);
        }
        for (String varName : rc.getNames(CAPABILITY)) {
            if (!have.contains(varName.toLowerCase())) {
                rc.unset(CAPABILITY, null, varName);
            }
        }
    } else {
        rc.unsetSection(CAPABILITY, null);
    }
    for (AccessSection as : sort(accessSections.values())) {
        String refName = as.getName();
        if (AccessSection.GLOBAL_CAPABILITIES.equals(refName)) {
            continue;
        }
        StringBuilder doNotInherit = new StringBuilder();
        for (Permission perm : sort(as.getPermissions())) {
            if (perm.getExclusiveGroup()) {
                if (0 < doNotInherit.length()) {
                    doNotInherit.append(' ');
                }
                doNotInherit.append(perm.getName());
            }
        }
        if (0 < doNotInherit.length()) {
            rc.setString(ACCESS, refName, KEY_GROUP_PERMISSIONS, doNotInherit.toString());
        } else {
            rc.unset(ACCESS, refName, KEY_GROUP_PERMISSIONS);
        }
        Set<String> have = new HashSet<>();
        for (Permission permission : sort(as.getPermissions())) {
            have.add(permission.getName().toLowerCase());
            boolean needRange = Permission.hasRange(permission.getName());
            List<String> rules = new ArrayList<>();
            for (PermissionRule rule : sort(permission.getRules())) {
                GroupReference group = resolve(rule.getGroup());
                if (group.getUUID() != null) {
                    keepGroups.add(group.getUUID());
                }
                rules.add(rule.toBuilder().setGroup(group).build().asString(needRange));
            }
            rc.setStringList(ACCESS, refName, permission.getName(), rules);
        }
        for (String varName : rc.getNames(ACCESS, refName)) {
            if (isCoreOrPluginPermission(convertLegacyPermission(varName)) && !have.contains(varName.toLowerCase())) {
                rc.unset(ACCESS, refName, varName);
            }
        }
    }
    for (String name : rc.getSubsections(ACCESS)) {
        if (AccessSection.isValidRefSectionName(name) && !accessSections.containsKey(name)) {
            rc.unsetSection(ACCESS, name);
        }
    }
}
Also used : PermissionRule(com.google.gerrit.entities.PermissionRule) Permission.isPermission(com.google.gerrit.entities.Permission.isPermission) PluginPermissionsUtil.isValidPluginPermission(com.google.gerrit.server.permissions.PluginPermissionsUtil.isValidPluginPermission) Permission(com.google.gerrit.entities.Permission) ArrayList(java.util.ArrayList) GroupReference(com.google.gerrit.entities.GroupReference) AccessSection(com.google.gerrit.entities.AccessSection) HashSet(java.util.HashSet)

Example 9 with AccessSection

use of com.google.gerrit.entities.AccessSection in project gerrit by GerritCodeReview.

the class ProjectConfig method getCacheable.

/**
 * Returns an immutable, thread-safe representation of this object that can be cached.
 */
public CachedProjectConfig getCacheable() {
    CachedProjectConfig.Builder builder = CachedProjectConfig.builder().setProject(project).setAccountsSection(accountsSection).setBranchOrderSection(Optional.ofNullable(branchOrderSection)).setMimeTypes(mimeTypes).setRulesId(Optional.ofNullable(rulesId)).setRevision(Optional.ofNullable(getRevision())).setMaxObjectSizeLimit(maxObjectSizeLimit).setCheckReceivedObjects(checkReceivedObjects).setExtensionPanelSections(extensionPanelSections);
    groupList.byUUID().values().forEach(g -> builder.addGroup(g));
    contributorAgreements.values().forEach(c -> builder.addContributorAgreement(c));
    notifySections.values().forEach(n -> builder.addNotifySection(n));
    subscribeSections.values().forEach(s -> builder.addSubscribeSection(s));
    commentLinkSections.values().forEach(c -> builder.addCommentLinkSection(c));
    labelSections.values().forEach(l -> builder.addLabelSection(l));
    submitRequirementSections.values().forEach(sr -> builder.addSubmitRequirementSection(sr));
    pluginConfigs.entrySet().forEach(c -> builder.addPluginConfig(c.getKey(), c.getValue().toText()));
    projectLevelConfigs.entrySet().forEach(c -> builder.addProjectLevelConfig(c.getKey(), c.getValue().toText()));
    if (projectName.equals(allProjectsName)) {
        // Filter out permissions that aren't allowed to be set on All-Projects
        accessSections.values().forEach(a -> {
            List<Permission.Builder> copy = new ArrayList<>();
            for (Permission p : a.getPermissions()) {
                if (Permission.canBeOnAllProjects(a.getName(), p.getName())) {
                    copy.add(p.toBuilder());
                }
            }
            AccessSection section = AccessSection.builder(a.getName()).modifyPermissions(permissions -> permissions.addAll(copy)).build();
            builder.addAccessSection(section);
        });
    } else {
        accessSections.values().forEach(a -> builder.addAccessSection(a));
    }
    return builder.build();
}
Also used : GroupBackend(com.google.gerrit.server.account.GroupBackend) Arrays(java.util.Arrays) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) Inject(com.google.inject.Inject) RevWalk(org.eclipse.jgit.revwalk.RevWalk) InheritableBoolean(com.google.gerrit.extensions.client.InheritableBoolean) Config(org.eclipse.jgit.lib.Config) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) VersionedMetaData(com.google.gerrit.server.git.meta.VersionedMetaData) LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) ConfigUtil(com.google.gerrit.server.config.ConfigUtil) Locale(java.util.Locale) Map(java.util.Map) ContributorAgreement(com.google.gerrit.entities.ContributorAgreement) RefNames(com.google.gerrit.entities.RefNames) Splitter(com.google.common.base.Splitter) PluginConfig(com.google.gerrit.server.config.PluginConfig) EnumSet(java.util.EnumSet) Permission.isPermission(com.google.gerrit.entities.Permission.isPermission) ImmutableSet(com.google.common.collect.ImmutableSet) PatternSyntaxException(java.util.regex.PatternSyntaxException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) AccessSection(com.google.gerrit.entities.AccessSection) Collection(java.util.Collection) Set(java.util.Set) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) DEFAULT_SUBMIT_TYPE(com.google.gerrit.entities.Project.DEFAULT_SUBMIT_TYPE) ConfiguredMimeTypes(com.google.gerrit.entities.ConfiguredMimeTypes) NotifyConfig(com.google.gerrit.entities.NotifyConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) AllProjectsConfigProvider(com.google.gerrit.server.config.AllProjectsConfigProvider) FluentLogger(com.google.common.flogger.FluentLogger) Joiner(com.google.common.base.Joiner) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Singleton(com.google.inject.Singleton) ValidationError(com.google.gerrit.server.git.ValidationError) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Shorts(com.google.common.primitives.Shorts) SubmitRequirementExpression(com.google.gerrit.entities.SubmitRequirementExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) PluginPermissionsUtil.isValidPluginPermission(com.google.gerrit.server.permissions.PluginPermissionsUtil.isValidPluginPermission) ImmutableList(com.google.common.collect.ImmutableList) GroupDescription(com.google.gerrit.entities.GroupDescription) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Objects.requireNonNull(java.util.Objects.requireNonNull) LabelFunction(com.google.gerrit.entities.LabelFunction) PermissionRule(com.google.gerrit.entities.PermissionRule) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) UsedAt(com.google.gerrit.common.UsedAt) Address(com.google.gerrit.entities.Address) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) AccountGroup(com.google.gerrit.entities.AccountGroup) AccountsSection(com.google.gerrit.entities.AccountsSection) CharMatcher(com.google.common.base.CharMatcher) Permission(com.google.gerrit.entities.Permission) IOException(java.io.IOException) ProjectState(com.google.gerrit.extensions.client.ProjectState) Maps(com.google.common.collect.Maps) ObjectId(org.eclipse.jgit.lib.ObjectId) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) SubscribeSection(com.google.gerrit.entities.SubscribeSection) Project(com.google.gerrit.entities.Project) StoredConfig(org.eclipse.jgit.lib.StoredConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StoredCommentLinkInfo(com.google.gerrit.entities.StoredCommentLinkInfo) BranchOrderSection(com.google.gerrit.entities.BranchOrderSection) Collections(java.util.Collections) Action(com.google.gerrit.entities.PermissionRule.Action) Repository(org.eclipse.jgit.lib.Repository) InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ArrayList(java.util.ArrayList) Permission.isPermission(com.google.gerrit.entities.Permission.isPermission) PluginPermissionsUtil.isValidPluginPermission(com.google.gerrit.server.permissions.PluginPermissionsUtil.isValidPluginPermission) Permission(com.google.gerrit.entities.Permission) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) AccessSection(com.google.gerrit.entities.AccessSection)

Example 10 with AccessSection

use of com.google.gerrit.entities.AccessSection in project gerrit by GerritCodeReview.

the class ProjectState method getLocalAccessSections.

/**
 * Get the sections that pertain only to this project.
 */
List<SectionMatcher> getLocalAccessSections() {
    if (localAccessSections != null) {
        return localAccessSections;
    }
    List<SectionMatcher> sm = new ArrayList<>(cachedConfig.getAccessSections().values().size());
    for (AccessSection section : cachedConfig.getAccessSections().values()) {
        SectionMatcher matcher = SectionMatcher.wrap(getNameKey(), section);
        if (matcher != null) {
            sm.add(matcher);
        }
    }
    localAccessSections = sm;
    return localAccessSections;
}
Also used : ArrayList(java.util.ArrayList) AccessSection(com.google.gerrit.entities.AccessSection)

Aggregations

AccessSection (com.google.gerrit.entities.AccessSection)22 Permission (com.google.gerrit.entities.Permission)16 PermissionRule (com.google.gerrit.entities.PermissionRule)7 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)6 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)4 Project (com.google.gerrit.entities.Project)3 InvalidNameException (com.google.gerrit.exceptions.InvalidNameException)3 CoreOrPluginProjectPermission (com.google.gerrit.extensions.api.access.CoreOrPluginProjectPermission)3 PluginProjectPermission (com.google.gerrit.extensions.api.access.PluginProjectPermission)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 SectionMatcher (com.google.gerrit.server.project.SectionMatcher)3 ImmutableList (com.google.common.collect.ImmutableList)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 GroupDescription (com.google.gerrit.entities.GroupDescription)2 GroupReference (com.google.gerrit.entities.GroupReference)2 LabelType (com.google.gerrit.entities.LabelType)2