3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info 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.common.management;
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;
27 public class ManagedInstanceChecker {
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.";
32 private final ManagedInstanceService managedInstanceService;
33 private final ManagedProjectService managedProjectService;
35 public ManagedInstanceChecker(ManagedInstanceService managedInstanceService, ManagedProjectService managedProjectService) {
36 this.managedInstanceService = managedInstanceService;
37 this.managedProjectService = managedProjectService;
40 public void throwIfInstanceIsManaged() {
41 BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), INSTANCE_EXCEPTION_MESSAGE);
44 public void throwIfProjectIsManaged(DbSession dbSession, String projectUuid) {
45 BadRequestException.checkRequest(!managedProjectService.isProjectManaged(dbSession, projectUuid), PROJECT_EXCEPTION_MESSAGE);
48 public void throwIfUserIsManaged(DbSession dbSession, String userUuid) {
49 BadRequestException.checkRequest(!managedInstanceService.isUserManaged(dbSession, userUuid), INSTANCE_EXCEPTION_MESSAGE);
52 public void throwIfGroupIsManaged(DbSession dbSession, String groupUuid) {
53 BadRequestException.checkRequest(!managedInstanceService.isGroupManaged(dbSession, groupUuid), INSTANCE_EXCEPTION_MESSAGE);
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);
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);