]> source.dussan.org Git - sonarqube.git/blob
29c79d1168671d57d96237b39aae7bb3922cbdea
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
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;
33
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;
37
38 /**
39  * Apply default permissions on new projects and index issues/authorization
40  */
41 public class ApplyPermissionsStep implements ComputationStep {
42
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;
48
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;
56   }
57
58   @Override
59   public void execute() {
60     new DepthTraversalTypeAwareCrawler(
61       new TypeAwareVisitorAdapter(CrawlerDepthLimit.reportMaxDepth(PROJECT).withViewsMaxDepth(VIEW), PRE_ORDER) {
62         @Override
63         public void visitProject(Component project) {
64           execute(project);
65         }
66
67         @Override
68         public void visitView(Component view) {
69           execute(view);
70         }
71       }).visit(treeRootHolder.getRoot());
72   }
73
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);
79         dbSession.commit();
80         indexer.index(dbSession, project.getUuid());
81       }
82     }
83   }
84
85   private boolean hasNoPermissions(DbSession dbSession, long projectId) {
86     return !dbClient.groupPermissionDao().hasRootComponentPermissions(dbSession, projectId) &&
87       !dbClient.userPermissionDao().hasRootComponentPermissions(dbSession, projectId);
88   }
89
90   @Override
91   public String getDescription() {
92     return "Apply permissions";
93   }
94
95 }