]> source.dussan.org Git - sonarqube.git/blob
dad6f4ac6d23acdf06438ac69bd43a5b5455a953
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info 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.common.management;
21
22 import org.sonar.db.DbSession;
23 import org.sonar.server.exceptions.BadRequestException;
24 import org.sonar.server.management.ManagedInstanceService;
25 import org.sonar.server.management.ManagedProjectService;
26
27 public class ManagedInstanceChecker {
28
29   private static final String INSTANCE_EXCEPTION_MESSAGE = "Operation not allowed when the instance is externally managed.";
30   private static final String PROJECT_EXCEPTION_MESSAGE = "Operation not allowed when the project is externally managed.";
31
32   private final ManagedInstanceService managedInstanceService;
33   private final ManagedProjectService managedProjectService;
34
35   public ManagedInstanceChecker(ManagedInstanceService managedInstanceService, ManagedProjectService managedProjectService) {
36     this.managedInstanceService = managedInstanceService;
37     this.managedProjectService = managedProjectService;
38   }
39
40   public void throwIfInstanceIsManaged() {
41     BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), INSTANCE_EXCEPTION_MESSAGE);
42   }
43
44   public void throwIfProjectIsManaged(DbSession dbSession, String projectUuid) {
45     BadRequestException.checkRequest(!managedProjectService.isProjectManaged(dbSession, projectUuid), PROJECT_EXCEPTION_MESSAGE);
46   }
47
48   public void throwIfUserIsManaged(DbSession dbSession, String userUuid) {
49     BadRequestException.checkRequest(!managedInstanceService.isUserManaged(dbSession, userUuid), INSTANCE_EXCEPTION_MESSAGE);
50   }
51
52   public void throwIfGroupIsManaged(DbSession dbSession, String groupUuid) {
53     BadRequestException.checkRequest(!managedInstanceService.isGroupManaged(dbSession, groupUuid), INSTANCE_EXCEPTION_MESSAGE);
54   }
55
56   public void throwIfUserAndProjectAreManaged(DbSession dbSession, String userUuid, String projectUuid) {
57     boolean isUserManaged = managedInstanceService.isUserManaged(dbSession, userUuid);
58     boolean isProjectManaged = managedProjectService.isProjectManaged(dbSession, projectUuid);
59     BadRequestException.checkRequest(!(isUserManaged && isProjectManaged), PROJECT_EXCEPTION_MESSAGE);
60   }
61
62   public void throwIfGroupAndProjectAreManaged(DbSession dbSession, String groupUuid, String projectUuid) {
63     boolean isGroupManaged = managedInstanceService.isGroupManaged(dbSession, groupUuid);
64     boolean isProjectManaged = managedProjectService.isProjectManaged(dbSession, projectUuid);
65     BadRequestException.checkRequest(!(isGroupManaged && isProjectManaged), PROJECT_EXCEPTION_MESSAGE);
66   }
67
68 }