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.db.permission.template;
22 import java.util.function.Consumer;
23 import javax.annotation.Nullable;
24 import org.sonar.db.DbClient;
25 import org.sonar.db.DbSession;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.user.GroupDto;
28 import org.sonar.db.user.UserDto;
30 import static java.util.Optional.ofNullable;
31 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
32 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
34 public class PermissionTemplateDbTester {
35 private final DbTester db;
36 private final DbClient dbClient;
37 private final DbSession dbSession;
39 public PermissionTemplateDbTester(DbTester db) {
41 this.dbClient = db.getDbClient();
42 this.dbSession = db.getSession();
45 public void setDefaultTemplates(String projectDefaultTemplateUuid, @Nullable String applicationDefaultTemplateUuid, @Nullable String portfoliosDefaultTemplateUuid) {
46 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.prj", projectDefaultTemplateUuid);
47 if (applicationDefaultTemplateUuid != null) {
48 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.app", applicationDefaultTemplateUuid);
50 if (portfoliosDefaultTemplateUuid != null) {
51 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.port", portfoliosDefaultTemplateUuid);
56 public void setDefaultTemplates(PermissionTemplateDto projectDefaultTemplate, @Nullable PermissionTemplateDto applicationDefaultTemplate,
57 @Nullable PermissionTemplateDto portfoliosDefaultTemplate) {
58 setDefaultTemplates(projectDefaultTemplate.getUuid(),
59 ofNullable(applicationDefaultTemplate).map(PermissionTemplateDto::getUuid).orElse(null),
60 ofNullable(portfoliosDefaultTemplate).map(PermissionTemplateDto::getUuid).orElse(null));
64 public final PermissionTemplateDto insertTemplate(Consumer<PermissionTemplateDto>... populators) {
65 return insertTemplate(newPermissionTemplateDto(populators));
68 public PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
69 PermissionTemplateDto templateInDb = dbClient.permissionTemplateDao().insert(dbSession, template);
74 public void addGroupToTemplate(PermissionTemplateDto permissionTemplate, GroupDto group, String permission) {
75 addGroupToTemplate(permissionTemplate.getUuid(), group.getUuid(), permission, permissionTemplate.getName(), group.getName());
78 public void addGroupToTemplate(String templateUuid, @Nullable String groupUuid, String permission, String templateName, @Nullable String groupName) {
79 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateUuid, groupUuid, permission, templateName, groupName);
83 public void addAnyoneToTemplate(PermissionTemplateDto permissionTemplate, String permission) {
84 addGroupToTemplate(permissionTemplate.getUuid(), null, permission, permissionTemplate.getName(), null);
87 public void addUserToTemplate(PermissionTemplateDto permissionTemplate, UserDto user, String permission) {
88 addUserToTemplate(permissionTemplate.getUuid(), user.getUuid(), permission, permissionTemplate.getName(), user.getName());
91 public void addUserToTemplate(String templateUuid, String userUuid, String permission, String templateName, String userLogin) {
92 dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateUuid, userUuid, permission, templateName, userLogin);
96 public void addProjectCreatorToTemplate(PermissionTemplateDto permissionTemplate, String permission) {
97 addProjectCreatorToTemplate(permissionTemplate.getUuid(), permission, permissionTemplate.getName());
100 public void addProjectCreatorToTemplate(String templateUuid, String permission, String templateName) {
101 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, newPermissionTemplateCharacteristicDto()
102 .setWithProjectCreator(true)
103 .setTemplateUuid(templateUuid)
104 .setPermission(permission),