3 * Copyright (C) 2009-2021 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 javax.annotation.Nullable;
23 import org.sonar.db.DbClient;
24 import org.sonar.db.DbSession;
25 import org.sonar.db.DbTester;
26 import org.sonar.db.user.GroupDto;
27 import org.sonar.db.user.UserDto;
29 import static java.util.Optional.ofNullable;
30 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
31 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
33 public class PermissionTemplateDbTester {
34 private final DbTester db;
35 private final DbClient dbClient;
36 private final DbSession dbSession;
38 public PermissionTemplateDbTester(DbTester db) {
40 this.dbClient = db.getDbClient();
41 this.dbSession = db.getSession();
44 public void setDefaultTemplates(String projectDefaultTemplateUuid, @Nullable String applicationDefaultTemplateUuid, @Nullable String portfoliosDefaultTemplateUuid) {
45 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.prj", projectDefaultTemplateUuid);
46 if (applicationDefaultTemplateUuid != null) {
47 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.app", applicationDefaultTemplateUuid);
49 if (portfoliosDefaultTemplateUuid != null) {
50 db.getDbClient().internalPropertiesDao().save(dbSession, "defaultTemplate.port", portfoliosDefaultTemplateUuid);
55 public void setDefaultTemplates(PermissionTemplateDto projectDefaultTemplate, @Nullable PermissionTemplateDto applicationDefaultTemplate,
56 @Nullable PermissionTemplateDto portfoliosDefaultTemplate) {
57 setDefaultTemplates(projectDefaultTemplate.getUuid(),
58 ofNullable(applicationDefaultTemplate).map(PermissionTemplateDto::getUuid).orElse(null),
59 ofNullable(portfoliosDefaultTemplate).map(PermissionTemplateDto::getUuid).orElse(null));
62 public PermissionTemplateDto insertTemplate() {
63 return insertTemplate(newPermissionTemplateDto());
66 public PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
67 PermissionTemplateDto templateInDb = dbClient.permissionTemplateDao().insert(dbSession, template);
72 public void addGroupToTemplate(PermissionTemplateDto permissionTemplate, GroupDto group, String permission) {
73 addGroupToTemplate(permissionTemplate.getUuid(), group.getUuid(), permission, permissionTemplate.getName(), group.getName());
76 public void addGroupToTemplate(String templateUuid, @Nullable String groupUuid, String permission, String templateName, @Nullable String groupName) {
77 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateUuid, groupUuid, permission, templateName, groupName);
81 public void addAnyoneToTemplate(PermissionTemplateDto permissionTemplate, String permission) {
82 addGroupToTemplate(permissionTemplate.getUuid(), null, permission, permissionTemplate.getName(), null);
85 public void addUserToTemplate(PermissionTemplateDto permissionTemplate, UserDto user, String permission) {
86 addUserToTemplate(permissionTemplate.getUuid(), user.getUuid(), permission, permissionTemplate.getName(), user.getName());
89 public void addUserToTemplate(String templateUuid, String userUuid, String permission, String templateName, String userLogin) {
90 dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateUuid, userUuid, permission, templateName, userLogin);
94 public void addProjectCreatorToTemplate(PermissionTemplateDto permissionTemplate, String permission) {
95 addProjectCreatorToTemplate(permissionTemplate.getUuid(), permission, permissionTemplate.getName());
98 public void addProjectCreatorToTemplate(String templateUuid, String permission, String templateName) {
99 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, newPermissionTemplateCharacteristicDto()
100 .setWithProjectCreator(true)
101 .setTemplateUuid(templateUuid)
102 .setPermission(permission),