Search in sources :

Example 46 with PermissionRule

use of com.google.gerrit.common.data.PermissionRule in project gerrit by GerritCodeReview.

the class RefControl method toRange.

private PermissionRange toRange(String permissionName, List<PermissionRule> ruleList) {
    Map<ProjectRef, AllowedRange> ranges = new HashMap<>();
    for (PermissionRule rule : ruleList) {
        ProjectRef p = relevant.getRuleProps(rule);
        AllowedRange r = ranges.get(p);
        if (r == null) {
            r = new AllowedRange();
            ranges.put(p, r);
        }
        r.update(rule);
    }
    int allowMin = 0;
    int allowMax = 0;
    int blockMin = Integer.MIN_VALUE;
    int blockMax = Integer.MAX_VALUE;
    for (AllowedRange r : ranges.values()) {
        allowMin = Math.min(allowMin, r.getAllowMin());
        allowMax = Math.max(allowMax, r.getAllowMax());
        blockMin = Math.max(blockMin, r.getBlockMin());
        blockMax = Math.min(blockMax, r.getBlockMax());
    }
    // BLOCK wins over ALLOW across projects
    int min = Math.max(allowMin, blockMin + 1);
    int max = Math.min(allowMax, blockMax - 1);
    return new PermissionRange(permissionName, min, max);
}
Also used : PermissionRange(com.google.gerrit.common.data.PermissionRange) HashMap(java.util.HashMap) PermissionRule(com.google.gerrit.common.data.PermissionRule)

Example 47 with PermissionRule

use of com.google.gerrit.common.data.PermissionRule in project gerrit by GerritCodeReview.

the class RefControl method isVisibleByRegisteredUsers.

/** True if this reference is visible by all REGISTERED_USERS */
public boolean isVisibleByRegisteredUsers() {
    List<PermissionRule> access = relevant.getPermission(Permission.READ);
    List<PermissionRule> overridden = relevant.getOverridden(Permission.READ);
    Set<ProjectRef> allows = new HashSet<>();
    Set<ProjectRef> blocks = new HashSet<>();
    for (PermissionRule rule : access) {
        if (rule.isBlock()) {
            blocks.add(relevant.getRuleProps(rule));
        } else if (SystemGroupBackend.isAnonymousOrRegistered(rule.getGroup())) {
            allows.add(relevant.getRuleProps(rule));
        }
    }
    for (PermissionRule rule : overridden) {
        if (SystemGroupBackend.isAnonymousOrRegistered(rule.getGroup())) {
            blocks.remove(relevant.getRuleProps(rule));
        }
    }
    blocks.removeAll(allows);
    return blocks.isEmpty() && !allows.isEmpty();
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) HashSet(java.util.HashSet)

Example 48 with PermissionRule

use of com.google.gerrit.common.data.PermissionRule in project gerrit by GerritCodeReview.

the class RefControl method doCanPerform.

private boolean doCanPerform(String permissionName, boolean isChangeOwner, boolean blockOnly) {
    List<PermissionRule> access = access(permissionName, isChangeOwner);
    List<PermissionRule> overridden = relevant.getOverridden(permissionName);
    Set<ProjectRef> allows = new HashSet<>();
    Set<ProjectRef> blocks = new HashSet<>();
    for (PermissionRule rule : access) {
        if (rule.isBlock() && !rule.getForce()) {
            blocks.add(relevant.getRuleProps(rule));
        } else {
            allows.add(relevant.getRuleProps(rule));
        }
    }
    for (PermissionRule rule : overridden) {
        blocks.remove(relevant.getRuleProps(rule));
    }
    blocks.removeAll(allows);
    return blocks.isEmpty() && (!allows.isEmpty() || blockOnly);
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) HashSet(java.util.HashSet)

Example 49 with PermissionRule

use of com.google.gerrit.common.data.PermissionRule in project gerrit by GerritCodeReview.

the class RefControl method canForcePerform.

/** True if the user has force this permission. Works only for non labels. */
private boolean canForcePerform(String permissionName) {
    List<PermissionRule> access = access(permissionName);
    List<PermissionRule> overridden = relevant.getOverridden(permissionName);
    Set<ProjectRef> allows = new HashSet<>();
    Set<ProjectRef> blocks = new HashSet<>();
    for (PermissionRule rule : access) {
        if (rule.isBlock()) {
            blocks.add(relevant.getRuleProps(rule));
        } else if (rule.getForce()) {
            allows.add(relevant.getRuleProps(rule));
        }
    }
    for (PermissionRule rule : overridden) {
        if (rule.getForce()) {
            blocks.remove(relevant.getRuleProps(rule));
        }
    }
    blocks.removeAll(allows);
    return blocks.isEmpty() && !allows.isEmpty();
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) HashSet(java.util.HashSet)

Example 50 with PermissionRule

use of com.google.gerrit.common.data.PermissionRule in project gerrit by GerritCodeReview.

the class RefControl method getLabelRanges.

/** All value ranges of any allowed label permission. */
List<PermissionRange> getLabelRanges(boolean isChangeOwner) {
    List<PermissionRange> r = new ArrayList<>();
    for (Map.Entry<String, List<PermissionRule>> e : relevant.getDeclaredPermissions()) {
        if (Permission.isLabel(e.getKey())) {
            int min = 0;
            int max = 0;
            for (PermissionRule rule : e.getValue()) {
                if (projectControl.match(rule, isChangeOwner)) {
                    min = Math.min(min, rule.getMin());
                    max = Math.max(max, rule.getMax());
                }
            }
            if (min != 0 || max != 0) {
                r.add(new PermissionRange(e.getKey(), min, max));
            }
        }
    }
    return r;
}
Also used : PermissionRange(com.google.gerrit.common.data.PermissionRange) PermissionRule(com.google.gerrit.common.data.PermissionRule) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PermissionRule (com.google.gerrit.common.data.PermissionRule)51 Permission (com.google.gerrit.common.data.Permission)18 AccessSection (com.google.gerrit.common.data.AccessSection)14 GroupReference (com.google.gerrit.common.data.GroupReference)11 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)10 ArrayList (java.util.ArrayList)9 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)8 ContributorAgreement (com.google.gerrit.common.data.ContributorAgreement)6 HashSet (java.util.HashSet)6 PermissionRange (com.google.gerrit.common.data.PermissionRange)5 AccountGroup (com.google.gerrit.reviewdb.client.AccountGroup)5 Project (com.google.gerrit.reviewdb.client.Project)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 GroupDescription (com.google.gerrit.common.data.GroupDescription)3 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)3 NoSuchGroupException (com.google.gerrit.common.errors.NoSuchGroupException)2 AccessSectionInfo (com.google.gerrit.extensions.api.access.AccessSectionInfo)2 PermissionInfo (com.google.gerrit.extensions.api.access.PermissionInfo)2 PermissionRuleInfo (com.google.gerrit.extensions.api.access.PermissionRuleInfo)2