3 * Copyright (C) 2009-2020 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.component.ComponentDto;
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.user.UserDto;
46 import org.sonar.server.es.ProjectIndexer;
47 import org.sonar.server.es.ProjectIndexers;
48 import org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates;
49 import org.sonar.server.user.UserSession;
51 import static com.google.common.base.Preconditions.checkArgument;
52 import static java.lang.String.format;
53 import static java.util.Collections.singletonList;
54 import static org.sonar.api.security.DefaultGroups.isAnyone;
55 import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS;
56 import static org.sonar.db.permission.GlobalPermission.SCAN;
59 public class PermissionTemplateService {
61 private final DbClient dbClient;
62 private final ProjectIndexers projectIndexers;
63 private final UserSession userSession;
64 private final DefaultTemplatesResolver defaultTemplatesResolver;
65 private final UuidFactory uuidFactory;
67 public PermissionTemplateService(DbClient dbClient, ProjectIndexers projectIndexers, UserSession userSession,
68 DefaultTemplatesResolver defaultTemplatesResolver, UuidFactory uuidFactory) {
69 this.dbClient = dbClient;
70 this.projectIndexers = projectIndexers;
71 this.userSession = userSession;
72 this.defaultTemplatesResolver = defaultTemplatesResolver;
73 this.uuidFactory = uuidFactory;
76 public boolean wouldUserHaveScanPermissionWithDefaultTemplate(DbSession dbSession, @Nullable String userUuid, String projectKey) {
77 if (userSession.hasPermission(SCAN)) {
81 ComponentDto dto = new ComponentDto().setDbKey(projectKey).setQualifier(Qualifiers.PROJECT);
82 PermissionTemplateDto template = findTemplate(dbSession, dto);
83 if (template == null) {
87 List<String> potentialPermissions = dbClient.permissionTemplateDao().selectPotentialPermissionsByUserUuidAndTemplateUuid(dbSession, userUuid, template.getUuid());
88 return potentialPermissions.contains(SCAN.getKey());
92 * Apply a permission template to a set of projects. Authorization to administrate these projects
93 * is not verified. The projects must exist, so the "project creator" permissions defined in the
94 * template are ignored.
96 public void applyAndCommit(DbSession dbSession, PermissionTemplateDto template, Collection<ComponentDto> projects) {
97 if (projects.isEmpty()) {
101 for (ComponentDto project : projects) {
102 copyPermissions(dbSession, template, project, null);
104 projectIndexers.commitAndIndexComponents(dbSession, projects, ProjectIndexer.Cause.PERMISSION_CHANGE);
108 * Apply the default permission template to project. The project can already exist (so it has permissions) or
109 * can be provisioned (so has no permissions yet).
110 * @param projectCreatorUserId id of the user who creates the project, only if project is provisioned. He will
112 public void applyDefault(DbSession dbSession, ComponentDto component, @Nullable String projectCreatorUserId) {
113 PermissionTemplateDto template = findTemplate(dbSession, component);
114 checkArgument(template != null, "Cannot retrieve default permission template");
115 copyPermissions(dbSession, template, component, projectCreatorUserId);
118 public boolean hasDefaultTemplateWithPermissionOnProjectCreator(DbSession dbSession, ComponentDto component) {
119 PermissionTemplateDto template = findTemplate(dbSession, component);
120 return hasProjectCreatorPermission(dbSession, template);
123 private boolean hasProjectCreatorPermission(DbSession dbSession, @Nullable PermissionTemplateDto template) {
124 return template != null && dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid())).stream()
125 .anyMatch(PermissionTemplateCharacteristicDto::getWithProjectCreator);
128 private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable String projectCreatorUserUuid) {
129 dbClient.groupPermissionDao().deleteByRootComponentUuid(dbSession, project.uuid());
130 dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project.uuid());
132 List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid());
133 Map<String, String> userDtoMap = dbClient.userDao().selectByUuids(dbSession, usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet()))
134 .stream().collect(Collectors.toMap(UserDto::getUuid, UserDto::getUuid));
137 .filter(up -> permissionValidForProject(project, up.getPermission()))
139 UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), up.getPermission(), userDtoMap.get(up.getUserUuid()), project.uuid());
140 dbClient.userPermissionDao().insert(dbSession, dto);
143 List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateUuid(dbSession, template.getUuid());
146 .filter(gp -> groupNameValidForProject(project, gp.getGroupName()))
147 .filter(gp -> permissionValidForProject(project, gp.getPermission()))
149 GroupPermissionDto dto = new GroupPermissionDto()
150 .setUuid(uuidFactory.create())
151 .setGroupUuid(isAnyone(gp.getGroupName()) ? null : gp.getGroupUuid())
152 .setRole(gp.getPermission())
153 .setComponentUuid(project.uuid());
154 dbClient.groupPermissionDao().insert(dbSession, dto);
157 List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid()));
158 if (projectCreatorUserUuid != null) {
159 Set<String> permissionsForCurrentUserAlreadyInDb = usersPermissions.stream()
160 .filter(userPermission -> projectCreatorUserUuid.equals(userPermission.getUserUuid()))
161 .map(PermissionTemplateUserDto::getPermission)
162 .collect(java.util.stream.Collectors.toSet());
164 UserDto userDto = dbClient.userDao().selectByUuid(dbSession, projectCreatorUserUuid);
165 characteristics.stream()
166 .filter(PermissionTemplateCharacteristicDto::getWithProjectCreator)
167 .filter(up -> permissionValidForProject(project, up.getPermission()))
168 .filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission()))
170 UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), c.getPermission(), userDto.getUuid(), project.uuid());
171 dbClient.userPermissionDao().insert(dbSession, dto);
176 private static boolean permissionValidForProject(ComponentDto project, String permission) {
177 return project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission);
180 private static boolean groupNameValidForProject(ComponentDto project, String groupName) {
181 return !project.isPrivate() || !isAnyone(groupName);
185 * Return the permission template for the given component. If no template key pattern match then consider default
186 * template for the component qualifier.
189 private PermissionTemplateDto findTemplate(DbSession dbSession, ComponentDto component) {
190 List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, null);
191 List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
192 for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
193 String keyPattern = permissionTemplateDto.getKeyPattern();
194 if (StringUtils.isNotBlank(keyPattern) && component.getDbKey().matches(keyPattern)) {
195 matchingTemplates.add(permissionTemplateDto);
198 checkAtMostOneMatchForComponentKey(component.getDbKey(), matchingTemplates);
199 if (matchingTemplates.size() == 1) {
200 return matchingTemplates.get(0);
203 String qualifier = component.qualifier();
204 ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(dbSession);
206 case Qualifiers.PROJECT:
207 return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
208 case Qualifiers.VIEW:
209 String portDefaultTemplateUuid = resolvedDefaultTemplates.getPortfolio().orElseThrow(
210 () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
211 return dbClient.permissionTemplateDao().selectByUuid(dbSession, portDefaultTemplateUuid);
213 String appDefaultTemplateUuid = resolvedDefaultTemplates.getApplication().orElseThrow(
214 () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
215 return dbClient.permissionTemplateDao().selectByUuid(dbSession, appDefaultTemplateUuid);
217 throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
221 private static void checkAtMostOneMatchForComponentKey(String componentKey, List<PermissionTemplateDto> matchingTemplates) {
222 if (matchingTemplates.size() > 1) {
223 StringBuilder templatesNames = new StringBuilder();
224 for (Iterator<PermissionTemplateDto> it = matchingTemplates.iterator(); it.hasNext();) {
225 templatesNames.append("\"").append(it.next().getName()).append("\"");
227 templatesNames.append(", ");
230 throw new IllegalStateException(MessageFormat.format(
231 "The \"{0}\" key matches multiple permission templates: {1}."
232 + " A system administrator must update these templates so that only one of them matches the key.",
234 templatesNames.toString()));