Search in sources :

Example 6 with PermissionRange

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

the class RefControlTest method unblockInLocalRange_Fails.

@Test
public void unblockInLocalRange_Fails() throws Exception {
    projectOperations.project(parentKey).forUpdate().add(blockLabel(LabelId.CODE_REVIEW).ref("refs/heads/*").group(ANONYMOUS_USERS).range(-1, 1)).update();
    projectOperations.project(localKey).forUpdate().add(allowLabel(LabelId.CODE_REVIEW).ref("refs/heads/*").group(DEVS).range(-2, +2)).update();
    ProjectControl u = user(localKey, DEVS);
    PermissionRange range = u.controlForRef("refs/heads/master").getRange(LABEL + LabelId.CODE_REVIEW);
    assertCannotVote(-2, range);
    assertCannotVote(2, range);
}
Also used : PermissionRange(com.google.gerrit.entities.PermissionRange) Test(org.junit.Test)

Example 7 with PermissionRange

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

the class RefControlTest method blockPartialRangeLocally.

@Test
public void blockPartialRangeLocally() throws Exception {
    projectOperations.project(localKey).forUpdate().add(blockLabel(LabelId.CODE_REVIEW).ref("refs/heads/master").group(DEVS).range(+1, +2)).update();
    ProjectControl u = user(localKey, DEVS);
    PermissionRange range = u.controlForRef("refs/heads/master").getRange(LABEL + LabelId.CODE_REVIEW);
    assertCannotVote(2, range);
}
Also used : PermissionRange(com.google.gerrit.entities.PermissionRange) Test(org.junit.Test)

Example 8 with PermissionRange

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

the class AccountLimits method toRange.

private static PermissionRange toRange(String permissionName, List<PermissionRule> ruleList) {
    int min = 0;
    int max = 0;
    if (ruleList.isEmpty()) {
        PermissionRange.WithDefaults defaultRange = GlobalCapability.getRange(permissionName);
        if (defaultRange != null) {
            min = defaultRange.getDefaultMin();
            max = defaultRange.getDefaultMax();
        }
    } else {
        for (PermissionRule rule : ruleList) {
            min = Math.min(min, rule.getMin());
            max = Math.max(max, rule.getMax());
        }
    }
    return new PermissionRange(permissionName, min, max);
}
Also used : PermissionRange(com.google.gerrit.entities.PermissionRange) PermissionRule(com.google.gerrit.entities.PermissionRule)

Example 9 with PermissionRange

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

the class RefControl method toRange.

private PermissionRange toRange(String permissionName, boolean isChangeOwner) {
    int blockAllowMin = Integer.MIN_VALUE, blockAllowMax = Integer.MAX_VALUE;
    projectLoop: for (List<Permission> ps : relevant.getBlockRules(permissionName)) {
        boolean blockFound = false;
        int projectBlockAllowMin = Integer.MIN_VALUE, projectBlockAllowMax = Integer.MAX_VALUE;
        for (Permission p : ps) {
            if (p.getExclusiveGroup()) {
                for (PermissionRule pr : p.getRules()) {
                    if (pr.getAction() == Action.ALLOW && projectControl.match(pr, isChangeOwner)) {
                        // exclusive override, usually for a more specific ref.
                        continue projectLoop;
                    }
                }
            }
            for (PermissionRule pr : p.getRules()) {
                if (pr.getAction() == Action.BLOCK && projectControl.match(pr, isChangeOwner)) {
                    projectBlockAllowMin = pr.getMin() + 1;
                    projectBlockAllowMax = pr.getMax() - 1;
                    blockFound = true;
                }
            }
            if (blockFound) {
                for (PermissionRule pr : p.getRules()) {
                    if (pr.getAction() == Action.ALLOW && projectControl.match(pr, isChangeOwner)) {
                        projectBlockAllowMin = pr.getMin();
                        projectBlockAllowMax = pr.getMax();
                        break;
                    }
                }
                break;
            }
        }
        blockAllowMin = Math.max(projectBlockAllowMin, blockAllowMin);
        blockAllowMax = Math.min(projectBlockAllowMax, blockAllowMax);
    }
    int voteMin = 0, voteMax = 0;
    for (PermissionRule pr : relevant.getAllowRules(permissionName)) {
        if (pr.getAction() == PermissionRule.Action.ALLOW && projectControl.match(pr, isChangeOwner)) {
            // For votes, contrary to normal permissions, we aggregate all applicable rules.
            voteMin = Math.min(voteMin, pr.getMin());
            voteMax = Math.max(voteMax, pr.getMax());
        }
    }
    return new PermissionRange(permissionName, /* min= */
    Math.max(voteMin, blockAllowMin), /* max= */
    Math.min(voteMax, blockAllowMax));
}
Also used : PermissionRange(com.google.gerrit.entities.PermissionRange) PermissionRule(com.google.gerrit.entities.PermissionRule) Permission(com.google.gerrit.entities.Permission) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 10 with PermissionRange

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

the class RefControlTest method unblockRangeOnMoreSpecificRef_Fails.

@Test
public void unblockRangeOnMoreSpecificRef_Fails() throws Exception {
    projectOperations.project(localKey).forUpdate().add(blockLabel(LabelId.CODE_REVIEW).ref("refs/heads/*").group(ANONYMOUS_USERS).range(-1, +1)).add(allowLabel(LabelId.CODE_REVIEW).ref("refs/heads/master").group(DEVS).range(-2, +2)).update();
    ProjectControl u = user(localKey, DEVS);
    PermissionRange range = u.controlForRef("refs/heads/master").getRange(LABEL + LabelId.CODE_REVIEW);
    assertCannotVote(-2, range);
    assertCannotVote(2, range);
}
Also used : PermissionRange(com.google.gerrit.entities.PermissionRange) Test(org.junit.Test)

Aggregations

PermissionRange (com.google.gerrit.entities.PermissionRange)17 Test (org.junit.Test)15 PermissionRule (com.google.gerrit.entities.PermissionRule)2 ImmutableList (com.google.common.collect.ImmutableList)1 Permission (com.google.gerrit.entities.Permission)1 List (java.util.List)1