Search in sources :

Example 11 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class AllUsersCreator method initAllUsers.

private void initAllUsers(Repository git) throws IOException, ConfigInvalidException {
    try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git)) {
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage("Initialized Gerrit Code Review " + Version.getVersion());
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setDescription("Individual user settings and preferences."));
        config.upsertAccessSection(RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", users -> {
            grant(config, users, Permission.READ, false, true, registered);
            grant(config, users, Permission.PUSH, false, true, registered);
            grant(config, users, Permission.SUBMIT, false, true, registered);
            grant(config, users, codeReviewLabel, -2, 2, true, registered);
        });
        // Initialize "Code-Review" label.
        config.upsertLabelType(codeReviewLabel);
        if (admin != null) {
            config.upsertAccessSection(RefNames.REFS_USERS_DEFAULT, defaults -> {
                defaults.upsertPermission(Permission.READ).setExclusiveGroup(true);
                grant(config, defaults, Permission.READ, admin);
                defaults.upsertPermission(Permission.PUSH).setExclusiveGroup(true);
                grant(config, defaults, Permission.PUSH, admin);
                defaults.upsertPermission(Permission.CREATE).setExclusiveGroup(true);
                grant(config, defaults, Permission.CREATE, admin);
            });
        }
        // Grant read permissions on the group branches to all users.
        // This allows group owners to see the group refs. VisibleRefFilter ensures that read
        // permissions for non-group-owners are ignored.
        config.upsertAccessSection(RefNames.REFS_GROUPS + "*", groups -> {
            grant(config, groups, Permission.READ, false, true, registered);
        });
        config.commit(md);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 12 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class SetLabel method apply.

@Override
public Response<LabelDefinitionInfo> apply(LabelResource rsrc, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getProject().getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new LabelDefinitionInput();
    }
    LabelType labelType = rsrc.getLabelType();
    try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        if (updateLabel(config, labelType, input)) {
            if (input.commitMessage != null) {
                md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
            } else {
                md.setMessage("Update label");
            }
            String newName = Strings.nullToEmpty(input.name).trim();
            labelType = config.getLabelSections().get(newName.isEmpty() ? labelType.getName() : newName);
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProject().getProjectState().getProject());
        }
    }
    return Response.ok(LabelDefinitionJson.format(rsrc.getProject().getNameKey(), labelType));
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 13 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class AllProjectsCreator method initAllProjects.

private void initAllProjects(Repository git, AllProjectsInput input) throws ConfigInvalidException, IOException {
    BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
    try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjectsName, git, bru)) {
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage(input.commitMessage().isPresent() ? input.commitMessage().get() : "Initialized Gerrit Code Review " + Version.getVersion());
        // init basic project configs.
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> {
            p.setDescription(input.projectDescription().orElse("Access inherited by all other projects."));
            // init boolean project configs.
            input.booleanProjectConfigs().forEach(p::setBooleanConfig);
        });
        // init labels.
        input.codeReviewLabel().ifPresent(codeReviewLabel -> config.upsertLabelType(codeReviewLabel));
        if (input.initDefaultAcls()) {
            // init access sections.
            initDefaultAcls(config, input);
        }
        // commit all the above configs as a commit in "refs/meta/config" branch of the All-Projects.
        config.commitToNewRef(md, RefNames.REFS_CONFIG);
        // init sequence number.
        initSequences(git, bru, input.firstChangeIdForNoteDb());
        // init schema
        versionManager.init();
        execute(git, bru);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 14 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class AccessIT method grantRevertPermissionByOnNewRefAndDeletingOnOldRef.

@Test
public void grantRevertPermissionByOnNewRefAndDeletingOnOldRef() throws Exception {
    String refsHeads = "refs/heads/*";
    String refsStar = "refs/*";
    String groupId = "global:Registered-Users";
    GroupReference registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS);
    try (Repository repo = repoManager.openRepository(newProjectName)) {
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, newProjectName, repo);
        ProjectConfig projectConfig = projectConfigFactory.read(md);
        projectConfig.upsertAccessSection(AccessSection.HEADS, heads -> {
            grant(projectConfig, heads, Permission.REVERT, registeredUsers);
        });
        md.getCommitBuilder().setAuthor(admin.newIdent());
        md.getCommitBuilder().setCommitter(admin.newIdent());
        md.setMessage("Add revert permission for all registered users\n");
        projectConfig.commit(md);
    }
    grantRevertPermission.execute(newProjectName);
    ProjectAccessInfo info = pApi().access();
    // Revert permission is removed on refs/heads/*.
    assertThat(info.local.containsKey(refsHeads)).isTrue();
    AccessSectionInfo accessSectionInfo = info.local.get(refsHeads);
    assertThat(accessSectionInfo.permissions.containsKey(Permission.REVERT)).isFalse();
    // new permission is added on refs/* with Registered-Users.
    assertThat(info.local.containsKey(refsStar)).isTrue();
    accessSectionInfo = info.local.get(refsStar);
    assertThat(accessSectionInfo.permissions.containsKey(Permission.REVERT)).isTrue();
    PermissionInfo permissionInfo = accessSectionInfo.permissions.get(Permission.REVERT);
    assertThat(permissionInfo.rules.containsKey(groupId)).isTrue();
    PermissionRuleInfo permissionRuleInfo = permissionInfo.rules.get(groupId);
    assertThat(permissionRuleInfo.action).isEqualTo(PermissionRuleInfo.Action.ALLOW);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) PermissionInfo(com.google.gerrit.extensions.api.access.PermissionInfo) ProjectAccessInfo(com.google.gerrit.extensions.api.access.ProjectAccessInfo) PermissionRuleInfo(com.google.gerrit.extensions.api.access.PermissionRuleInfo) AccessSectionInfo(com.google.gerrit.extensions.api.access.AccessSectionInfo) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 15 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class AccessIT method grantRevertPermissionDoesntDeleteAdminsPreferences.

@Test
public void grantRevertPermissionDoesntDeleteAdminsPreferences() throws Exception {
    GroupReference registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS);
    GroupReference otherGroup = systemGroupBackend.getGroup(ANONYMOUS_USERS);
    try (Repository repo = repoManager.openRepository(newProjectName)) {
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, newProjectName, repo);
        ProjectConfig projectConfig = projectConfigFactory.read(md);
        projectConfig.upsertAccessSection(AccessSection.HEADS, heads -> {
            grant(projectConfig, heads, Permission.REVERT, registeredUsers);
            grant(projectConfig, heads, Permission.REVERT, otherGroup);
        });
        md.getCommitBuilder().setAuthor(admin.newIdent());
        md.getCommitBuilder().setCommitter(admin.newIdent());
        md.setMessage("Add revert permission for all registered users\n");
        projectConfig.commit(md);
    }
    projectCache.evict(newProjectName);
    ProjectAccessInfo expected = pApi().access();
    grantRevertPermission.execute(newProjectName);
    projectCache.evict(newProjectName);
    ProjectAccessInfo actual = pApi().access();
    // Permissions don't change
    assertThat(actual.local).isEqualTo(expected.local);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) ProjectAccessInfo(com.google.gerrit.extensions.api.access.ProjectAccessInfo) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ProjectConfig (com.google.gerrit.server.project.ProjectConfig)39 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)26 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)8 Project (com.google.gerrit.entities.Project)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)7 Test (org.junit.Test)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 AccessSection (com.google.gerrit.entities.AccessSection)6 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)6 LabelType (com.google.gerrit.entities.LabelType)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)6 Repository (org.eclipse.jgit.lib.Repository)6 GroupReference (com.google.gerrit.entities.GroupReference)5 Permission (com.google.gerrit.entities.Permission)5 IOException (java.io.IOException)5 ProjectState (com.google.gerrit.server.project.ProjectState)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)4 TestRepository (org.eclipse.jgit.junit.TestRepository)4