Search in sources :

Example 21 with PermissionRule

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

the class ProjectConfig method replace.

public void replace(ContributorAgreement section) {
    section.setAutoVerify(resolve(section.getAutoVerify()));
    for (PermissionRule rule : section.getAccepted()) {
        rule.setGroup(resolve(rule.getGroup()));
    }
    contributorAgreements.put(section.getName(), section);
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule)

Example 22 with PermissionRule

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

the class GetAccess method apply.

@Override
public ProjectAccessInfo apply(ProjectResource rsrc) throws ResourceNotFoundException, ResourceConflictException, IOException {
    // Load the current configuration from the repository, ensuring it's the most
    // recent version available. If it differs from what was in the project
    // state, force a cache flush now.
    //
    Project.NameKey projectName = rsrc.getNameKey();
    ProjectAccessInfo info = new ProjectAccessInfo();
    ProjectConfig config;
    ProjectControl pc = createProjectControl(projectName);
    RefControl metaConfigControl = pc.controlForRef(RefNames.REFS_CONFIG);
    try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
        config = ProjectConfig.read(md);
        if (config.updateGroupNames(groupBackend)) {
            md.setMessage("Update group names\n");
            config.commit(md);
            projectCache.evict(config.getProject());
            pc = createProjectControl(projectName);
        } else if (config.getRevision() != null && !config.getRevision().equals(pc.getProjectState().getConfig().getRevision())) {
            projectCache.evict(config.getProject());
            pc = createProjectControl(projectName);
        }
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(e.getMessage());
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }
    info.local = new HashMap<>();
    info.ownerOf = new HashSet<>();
    Map<AccountGroup.UUID, Boolean> visibleGroups = new HashMap<>();
    for (AccessSection section : config.getAccessSections()) {
        String name = section.getName();
        if (AccessSection.GLOBAL_CAPABILITIES.equals(name)) {
            if (pc.isOwner()) {
                info.local.put(name, createAccessSection(section));
                info.ownerOf.add(name);
            } else if (metaConfigControl.isVisible()) {
                info.local.put(section.getName(), createAccessSection(section));
            }
        } else if (RefConfigSection.isValid(name)) {
            RefControl rc = pc.controlForRef(name);
            if (rc.isOwner()) {
                info.local.put(name, createAccessSection(section));
                info.ownerOf.add(name);
            } else if (metaConfigControl.isVisible()) {
                info.local.put(name, createAccessSection(section));
            } else if (rc.isVisible()) {
                // Filter the section to only add rules describing groups that
                // are visible to the current-user. This includes any group the
                // user is a member of, as well as groups they own or that
                // are visible to all users.
                AccessSection dst = null;
                for (Permission srcPerm : section.getPermissions()) {
                    Permission dstPerm = null;
                    for (PermissionRule srcRule : srcPerm.getRules()) {
                        AccountGroup.UUID group = srcRule.getGroup().getUUID();
                        if (group == null) {
                            continue;
                        }
                        Boolean canSeeGroup = visibleGroups.get(group);
                        if (canSeeGroup == null) {
                            try {
                                canSeeGroup = groupControlFactory.controlFor(group).isVisible();
                            } catch (NoSuchGroupException e) {
                                canSeeGroup = Boolean.FALSE;
                            }
                            visibleGroups.put(group, canSeeGroup);
                        }
                        if (canSeeGroup) {
                            if (dstPerm == null) {
                                if (dst == null) {
                                    dst = new AccessSection(name);
                                    info.local.put(name, createAccessSection(dst));
                                }
                                dstPerm = dst.getPermission(srcPerm.getName(), true);
                            }
                            dstPerm.add(srcRule);
                        }
                    }
                }
            }
        }
    }
    if (info.ownerOf.isEmpty() && pc.isOwnerAnyRef()) {
        // Special case: If the section list is empty, this project has no current
        // access control information. Rely on what ProjectControl determines
        // is ownership, which probably means falling back to site administrators.
        info.ownerOf.add(AccessSection.ALL);
    }
    if (config.getRevision() != null) {
        info.revision = config.getRevision().name();
    }
    ProjectState parent = Iterables.getFirst(pc.getProjectState().parents(), null);
    if (parent != null) {
        info.inheritsFrom = projectJson.format(parent.getProject());
    }
    if (pc.getProject().getNameKey().equals(allProjectsName)) {
        if (pc.isOwner()) {
            info.ownerOf.add(AccessSection.GLOBAL_CAPABILITIES);
        }
    }
    info.isOwner = toBoolean(pc.isOwner());
    info.canUpload = toBoolean(pc.isOwner() || (metaConfigControl.isVisible() && metaConfigControl.canUpload()));
    info.canAdd = toBoolean(pc.canAddRefs());
    info.configVisible = pc.isOwner() || metaConfigControl.isVisible();
    return info;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) PermissionRule(com.google.gerrit.common.data.PermissionRule) ProjectAccessInfo(com.google.gerrit.extensions.api.access.ProjectAccessInfo) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) AccessSection(com.google.gerrit.common.data.AccessSection) NoSuchGroupException(com.google.gerrit.common.errors.NoSuchGroupException) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Permission(com.google.gerrit.common.data.Permission) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 23 with PermissionRule

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

the class GetAccess method createAccessSection.

private AccessSectionInfo createAccessSection(AccessSection section) {
    AccessSectionInfo accessSectionInfo = new AccessSectionInfo();
    accessSectionInfo.permissions = new HashMap<>();
    for (Permission p : section.getPermissions()) {
        PermissionInfo pInfo = new PermissionInfo(p.getLabel(), p.getExclusiveGroup() ? true : null);
        pInfo.rules = new HashMap<>();
        for (PermissionRule r : p.getRules()) {
            PermissionRuleInfo info = new PermissionRuleInfo(ACTION_TYPE.get(r.getAction()), r.getForce());
            if (r.hasRange()) {
                info.max = r.getMax();
                info.min = r.getMin();
            }
            AccountGroup.UUID group = r.getGroup().getUUID();
            if (group != null) {
                pInfo.rules.put(group.get(), info);
            }
        }
        accessSectionInfo.permissions.put(p.getName(), pInfo);
    }
    return accessSectionInfo;
}
Also used : PermissionInfo(com.google.gerrit.extensions.api.access.PermissionInfo) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) PermissionRule(com.google.gerrit.common.data.PermissionRule) Permission(com.google.gerrit.common.data.Permission) PermissionRuleInfo(com.google.gerrit.extensions.api.access.PermissionRuleInfo) AccessSectionInfo(com.google.gerrit.extensions.api.access.AccessSectionInfo)

Example 24 with PermissionRule

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

the class ProjectControl method canPerformOnAnyRef.

private boolean canPerformOnAnyRef(String permissionName) {
    for (SectionMatcher matcher : access()) {
        AccessSection section = matcher.section;
        Permission permission = section.getPermission(permissionName);
        if (permission == null) {
            continue;
        }
        for (PermissionRule rule : permission.getRules()) {
            if (rule.isBlock() || rule.isDeny() || !match(rule)) {
                continue;
            }
            //
            if (controlForRef(section.getName()).canPerform(permissionName)) {
                return true;
            }
            break;
        }
    }
    return false;
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) Permission(com.google.gerrit.common.data.Permission) ProjectPermission(com.google.gerrit.server.permissions.ProjectPermission) AccessSection(com.google.gerrit.common.data.AccessSection)

Example 25 with PermissionRule

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

the class AbstractDaemonTest method block.

protected PermissionRule block(Project.NameKey project, String ref, String permission, AccountGroup.UUID id) throws Exception {
    ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
    PermissionRule rule = Util.block(cfg, permission, id, ref);
    saveProjectConfig(project, cfg);
    return rule;
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) PermissionRule(com.google.gerrit.common.data.PermissionRule)

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