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.MyBatis;
25 import org.sonar.db.permission.PermissionRepository;
26 import org.sonar.server.computation.task.projectanalysis.component.Component;
27 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
28 import org.sonar.server.computation.task.projectanalysis.component.DbIdsRepository;
29 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
30 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
31 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
32 import org.sonar.server.computation.task.step.ComputationStep;
33 import org.sonar.server.issue.index.IssueAuthorizationIndexer;
35 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
36 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
37 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
40 * Apply default permissions on new projects and index issues/authorization
42 public class ApplyPermissionsStep implements ComputationStep {
44 private final DbClient dbClient;
45 private final DbIdsRepository dbIdsRepository;
46 private final IssueAuthorizationIndexer indexer;
47 private final PermissionRepository permissionRepository;
48 private final TreeRootHolder treeRootHolder;
50 public ApplyPermissionsStep(DbClient dbClient, DbIdsRepository dbIdsRepository, IssueAuthorizationIndexer indexer, PermissionRepository permissionRepository,
51 TreeRootHolder treeRootHolder) {
52 this.dbClient = dbClient;
53 this.dbIdsRepository = dbIdsRepository;
54 this.indexer = indexer;
55 this.permissionRepository = permissionRepository;
56 this.treeRootHolder = treeRootHolder;
60 public void execute() {
61 new DepthTraversalTypeAwareCrawler(
62 new TypeAwareVisitorAdapter(CrawlerDepthLimit.reportMaxDepth(PROJECT).withViewsMaxDepth(VIEW), PRE_ORDER) {
64 public void visitProject(Component project) {
69 public void visitView(Component view) {
72 }).visit(treeRootHolder.getRoot());
75 private void execute(Component project) {
76 DbSession session = dbClient.openSession(false);
78 long projectId = dbIdsRepository.getComponentId(project);
79 if (dbClient.roleDao().countComponentPermissions(session, projectId) == 0) {
80 permissionRepository.applyDefaultPermissionTemplate(session, projectId);
85 MyBatis.closeQuietly(session);
90 public String getDescription() {
91 return "Apply permissions";