3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import org.sonar.db.DbClient;
23 import org.sonar.db.DbSession;
24 import org.sonar.db.permission.PermissionRepository;
25 import org.sonar.server.computation.task.projectanalysis.component.Component;
26 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
27 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepository;
28 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
29 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
30 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
31 import org.sonar.server.computation.task.step.ComputationStep;
32 import org.sonar.server.permission.index.PermissionIndexer;
34 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
35 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
36 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
39 * Apply default permissions on new projects and index issues/authorization
41 public class ApplyPermissionsStep implements ComputationStep {
43 private final DbClient dbClient;
44 private final DbIdsRepository dbIdsRepository;
45 private final PermissionIndexer indexer;
46 private final PermissionRepository permissionRepository;
47 private final TreeRootHolder treeRootHolder;
49 public ApplyPermissionsStep(DbClient dbClient, DbIdsRepository dbIdsRepository, PermissionIndexer indexer, PermissionRepository permissionRepository,
50 TreeRootHolder treeRootHolder) {
51 this.dbClient = dbClient;
52 this.dbIdsRepository = dbIdsRepository;
53 this.indexer = indexer;
54 this.permissionRepository = permissionRepository;
55 this.treeRootHolder = treeRootHolder;
59 public void execute() {
60 new DepthTraversalTypeAwareCrawler(
61 new TypeAwareVisitorAdapter(CrawlerDepthLimit.reportMaxDepth(PROJECT).withViewsMaxDepth(VIEW), PRE_ORDER) {
63 public void visitProject(Component project) {
68 public void visitView(Component view) {
71 }).visit(treeRootHolder.getRoot());
74 private void execute(Component project) {
75 try (DbSession dbSession = dbClient.openSession(false)) {
76 long projectId = dbIdsRepository.getComponentId(project);
77 if (hasNoPermissions(dbSession, projectId)) {
78 permissionRepository.applyDefaultPermissionTemplate(dbSession, projectId);
80 indexer.index(dbSession, project.getUuid());
85 private boolean hasNoPermissions(DbSession dbSession, long projectId) {
86 return !dbClient.groupPermissionDao().hasRootComponentPermissions(dbSession, projectId) &&
87 !dbClient.userPermissionDao().hasRootComponentPermissions(dbSession, projectId);
91 public String getDescription() {
92 return "Apply permissions";