3 * Copyright (C) 2009-2023 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.permission;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.annotation.CheckForNull;
31 import javax.annotation.Nullable;
32 import org.apache.commons.lang.StringUtils;
33 import org.sonar.api.resources.Qualifiers;
34 import org.sonar.api.server.ServerSide;
35 import org.sonar.core.util.UuidFactory;
36 import org.sonar.db.DbClient;
37 import org.sonar.db.DbSession;
38 import org.sonar.db.entity.EntityDto;
39 import org.sonar.db.permission.GroupPermissionDto;
40 import org.sonar.db.permission.UserPermissionDto;
41 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
42 import org.sonar.db.permission.template.PermissionTemplateDto;
43 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
44 import org.sonar.db.permission.template.PermissionTemplateUserDto;
45 import org.sonar.db.project.ProjectDto;
46 import org.sonar.db.user.UserDto;
47 import org.sonar.db.user.UserId;
48 import org.sonar.server.es.ProjectIndexer;
49 import org.sonar.server.es.ProjectIndexers;
50 import org.sonar.server.exceptions.TemplateMatchingKeyException;
51 import org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates;
52 import org.sonar.server.user.UserSession;
54 import static com.google.common.base.Preconditions.checkArgument;
55 import static java.lang.String.format;
56 import static java.util.Collections.singletonList;
57 import static org.sonar.api.security.DefaultGroups.isAnyone;
58 import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS;
59 import static org.sonar.db.permission.GlobalPermission.SCAN;
62 public class PermissionTemplateService {
64 private final DbClient dbClient;
65 private final ProjectIndexers projectIndexers;
66 private final UserSession userSession;
67 private final DefaultTemplatesResolver defaultTemplatesResolver;
68 private final UuidFactory uuidFactory;
70 public PermissionTemplateService(DbClient dbClient, ProjectIndexers projectIndexers, UserSession userSession,
71 DefaultTemplatesResolver defaultTemplatesResolver, UuidFactory uuidFactory) {
72 this.dbClient = dbClient;
73 this.projectIndexers = projectIndexers;
74 this.userSession = userSession;
75 this.defaultTemplatesResolver = defaultTemplatesResolver;
76 this.uuidFactory = uuidFactory;
79 public boolean wouldUserHaveScanPermissionWithDefaultTemplate(DbSession dbSession, @Nullable String userUuid, String projectKey) {
80 if (userSession.hasPermission(SCAN)) {
84 ProjectDto projectDto = new ProjectDto().setKey(projectKey).setQualifier(Qualifiers.PROJECT);
85 PermissionTemplateDto template = findTemplate(dbSession, projectDto);
86 if (template == null) {
90 List<String> potentialPermissions = dbClient.permissionTemplateDao().selectPotentialPermissionsByUserUuidAndTemplateUuid(dbSession, userUuid, template.getUuid());
91 return potentialPermissions.contains(SCAN.getKey());
95 * Apply a permission template to a set of projects. Authorization to administrate these projects
96 * is not verified. The projects must exist, so the "project creator" permissions defined in the
97 * template are ignored.
99 public void applyAndCommit(DbSession dbSession, PermissionTemplateDto template, Collection<EntityDto> entities) {
100 if (entities.isEmpty()) {
104 for (EntityDto entity : entities) {
105 dbClient.groupPermissionDao().deleteByEntityUuid(dbSession, entity);
106 dbClient.userPermissionDao().deleteEntityPermissions(dbSession, entity);
107 copyPermissions(dbSession, template, entity, null);
109 projectIndexers.commitAndIndexEntities(dbSession, entities, ProjectIndexer.Cause.PERMISSION_CHANGE);
113 * Apply the default permission template to a new project (has no permissions yet).
114 * @param projectCreatorUserId id of the user creating the project.
116 public void applyDefaultToNewComponent(DbSession dbSession, EntityDto entityDto, @Nullable String projectCreatorUserId) {
117 PermissionTemplateDto template = findTemplate(dbSession, entityDto);
118 checkArgument(template != null, "Cannot retrieve default permission template");
119 copyPermissions(dbSession, template, entityDto, projectCreatorUserId);
122 public boolean hasDefaultTemplateWithPermissionOnProjectCreator(DbSession dbSession, ProjectDto projectDto) {
123 PermissionTemplateDto template = findTemplate(dbSession, projectDto);
124 return hasProjectCreatorPermission(dbSession, template);
127 private boolean hasProjectCreatorPermission(DbSession dbSession, @Nullable PermissionTemplateDto template) {
128 return template != null && dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid())).stream()
129 .anyMatch(PermissionTemplateCharacteristicDto::getWithProjectCreator);
132 private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, EntityDto entity, @Nullable String projectCreatorUserUuid) {
133 List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid());
134 Set<String> permissionTemplateUserUuids = usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet());
135 Map<String, UserId> userIdByUuid = dbClient.userDao().selectByUuids(dbSession, permissionTemplateUserUuids).stream().collect(Collectors.toMap(UserDto::getUuid, u -> u));
138 .filter(up -> permissionValidForProject(entity.isPrivate(), up.getPermission()))
140 UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), up.getPermission(), up.getUserUuid(), entity.getUuid());
141 dbClient.userPermissionDao().insert(dbSession, dto, entity, userIdByUuid.get(up.getUserUuid()), template);
144 List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateUuid(dbSession, template.getUuid());
147 .filter(gp -> groupNameValidForProject(entity.isPrivate(), gp.getGroupName()))
148 .filter(gp -> permissionValidForProject(entity.isPrivate(), gp.getPermission()))
150 String groupUuid = isAnyone(gp.getGroupName()) ? null : gp.getGroupUuid();
151 String groupName = groupUuid == null ? null : dbClient.groupDao().selectByUuid(dbSession, groupUuid).getName();
152 GroupPermissionDto dto = new GroupPermissionDto()
153 .setUuid(uuidFactory.create())
154 .setGroupUuid(groupUuid)
155 .setGroupName(groupName)
156 .setRole(gp.getPermission())
157 .setEntityUuid(entity.getUuid())
158 .setEntityName(entity.getName());
160 dbClient.groupPermissionDao().insert(dbSession, dto, entity, template);
163 List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid()));
164 if (projectCreatorUserUuid != null) {
165 Set<String> permissionsForCurrentUserAlreadyInDb = usersPermissions.stream()
166 .filter(userPermission -> projectCreatorUserUuid.equals(userPermission.getUserUuid()))
167 .map(PermissionTemplateUserDto::getPermission)
168 .collect(java.util.stream.Collectors.toSet());
170 UserDto userDto = dbClient.userDao().selectByUuid(dbSession, projectCreatorUserUuid);
171 characteristics.stream()
172 .filter(PermissionTemplateCharacteristicDto::getWithProjectCreator)
173 .filter(up -> permissionValidForProject(entity.isPrivate(), up.getPermission()))
174 .filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission()))
176 UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), c.getPermission(), userDto.getUuid(), entity.getUuid());
177 dbClient.userPermissionDao().insert(dbSession, dto, entity, userDto, template);
182 private static boolean permissionValidForProject(boolean isPrivateEntity, String permission) {
183 return isPrivateEntity || !PUBLIC_PERMISSIONS.contains(permission);
186 private static boolean groupNameValidForProject(boolean isPrivateEntity, String groupName) {
187 return !isPrivateEntity || !isAnyone(groupName);
191 * Return the permission template for the given component. If no template key pattern match then consider default
192 * template for the component qualifier.
195 private PermissionTemplateDto findTemplate(DbSession dbSession, EntityDto entityDto) {
196 List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, null);
197 List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
198 for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
199 String keyPattern = permissionTemplateDto.getKeyPattern();
200 if (StringUtils.isNotBlank(keyPattern) && entityDto.getKey().matches(keyPattern)) {
201 matchingTemplates.add(permissionTemplateDto);
204 checkAtMostOneMatchForComponentKey(entityDto.getKey(), matchingTemplates);
205 if (matchingTemplates.size() == 1) {
206 return matchingTemplates.get(0);
209 String qualifier = entityDto.getQualifier();
210 ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(dbSession);
212 case Qualifiers.PROJECT:
213 return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
214 case Qualifiers.VIEW:
215 String portDefaultTemplateUuid = resolvedDefaultTemplates.getPortfolio().orElseThrow(
216 () -> new IllegalStateException("Failed to find default template for portfolios"));
217 return dbClient.permissionTemplateDao().selectByUuid(dbSession, portDefaultTemplateUuid);
219 String appDefaultTemplateUuid = resolvedDefaultTemplates.getApplication().orElseThrow(
220 () -> new IllegalStateException("Failed to find default template for applications"));
221 return dbClient.permissionTemplateDao().selectByUuid(dbSession, appDefaultTemplateUuid);
223 throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
227 private static void checkAtMostOneMatchForComponentKey(String componentKey, List<PermissionTemplateDto> matchingTemplates) {
228 if (matchingTemplates.size() > 1) {
229 StringBuilder templatesNames = new StringBuilder();
230 for (Iterator<PermissionTemplateDto> it = matchingTemplates.iterator(); it.hasNext(); ) {
231 templatesNames.append("\"").append(it.next().getName()).append("\"");
233 templatesNames.append(", ");
236 throw new TemplateMatchingKeyException(MessageFormat.format(
237 "The \"{0}\" key matches multiple permission templates: {1}."
238 + " A system administrator must update these templates so that only one of them matches the key.",
240 templatesNames.toString()));