From: Teryk Bellahsene Date: Tue, 14 Jun 2016 17:32:34 +0000 (+0200) Subject: SONAR-7747 Replace project creator by current user when creating a project X-Git-Tag: 6.0-RC1~290 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5940540dd829e383249a74763c1b1aabf5761d4e;p=sonarqube.git SONAR-7747 Replace project creator by current user when creating a project --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java index bfa7d74e1c8..03c9c8e5c52 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java @@ -20,6 +20,7 @@ package org.sonar.server.permission; import java.util.List; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ServerSide; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; @@ -73,7 +74,10 @@ public class PermissionService { } else { userSession.checkPermission(GlobalPermissions.PROVISIONING); } - permissionRepository.applyDefaultPermissionTemplate(session, component); + + Integer currentUserId = userSession.getUserId(); + Long userId = Qualifiers.PROJECT.equals(component.qualifier()) && currentUserId != null ? currentUserId.longValue() : null; + permissionRepository.applyDefaultPermissionTemplate(session, component, userId); session.commit(); indexProjectPermissions(); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java index 22f62301b47..7e0361a3588 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java @@ -23,6 +23,8 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.config.Settings; @@ -30,10 +32,13 @@ import org.sonar.api.security.DefaultGroups; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupRoleDto; import org.sonar.db.user.UserRoleDto; +import static java.util.Objects.requireNonNull; + /** * This facade wraps db operations related to permissions *

@@ -137,23 +142,30 @@ public class PermissionRepository { } public void applyPermissionTemplate(DbSession session, String templateUuid, long resourceId) { + applyPermissionTemplate(session, templateUuid, resourceId, null); + } + + private void applyPermissionTemplate(DbSession session, String templateUuid, long componentId, @Nullable Long currentUserId) { PermissionTemplateDto permissionTemplate = dbClient.permissionTemplateDao().selectPermissionTemplateWithPermissions(session, templateUuid); - updateProjectAuthorizationDate(session, resourceId); - dbClient.roleDao().removeAllPermissions(session, resourceId); - List usersPermissions = permissionTemplate.getUsersPermissions(); - //TODO should return an empty list if there's no user permissions - if (usersPermissions != null) { - for (PermissionTemplateUserDto userPermission : usersPermissions) { - insertUserPermission(resourceId, userPermission.getUserId(), userPermission.getPermission(), false, session); - } - } - List groupsPermissions = permissionTemplate.getGroupsPermissions(); - //TODO should return an empty list if there's no group permission - if (groupsPermissions != null) { - for (PermissionTemplateGroupDto groupPermission : groupsPermissions) { - Long groupId = groupPermission.getGroupId() == null ? null : groupPermission.getGroupId(); - insertGroupPermission(resourceId, groupId, groupPermission.getPermission(), false, session); - } + updateProjectAuthorizationDate(session, componentId); + dbClient.roleDao().removeAllPermissions(session, componentId); + + List usersPermissions = requireNonNull(permissionTemplate.getUsersPermissions()); + usersPermissions.forEach(userPermission -> insertUserPermission(componentId, userPermission.getUserId(), userPermission.getPermission(), false, session)); + + List groupsPermissions = requireNonNull(permissionTemplate.getGroupsPermissions()); + groupsPermissions.forEach(groupPermission -> insertGroupPermission(componentId, groupPermission.getGroupId(), groupPermission.getPermission(), false, session)); + + List characteristics = requireNonNull(permissionTemplate.getCharacteristics()); + if (currentUserId != null) { + Set permissionsForCurrentUserAlreadyInDb = usersPermissions.stream() + .filter(userPermission -> currentUserId.equals(userPermission.getUserId())) + .map(PermissionTemplateUserDto::getPermission) + .collect(Collectors.toSet()); + characteristics.stream() + .filter(PermissionTemplateCharacteristicDto::getWithProjectCreator) + .filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())) + .forEach(characteristic -> insertUserPermission(componentId, currentUserId, characteristic.getPermission(), false, session)); } } @@ -162,12 +174,12 @@ public class PermissionRepository { */ public void applyDefaultPermissionTemplate(DbSession session, long componentId) { ComponentDto component = dbClient.componentDao().selectOrFailById(session, componentId); - applyDefaultPermissionTemplate(session, component); + applyDefaultPermissionTemplate(session, component, null); } - public void applyDefaultPermissionTemplate(DbSession session, ComponentDto componentDto) { - String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(session, componentDto.getKey(), componentDto.qualifier()); - applyPermissionTemplate(session, applicablePermissionTemplateKey, componentDto.getId()); + public void applyDefaultPermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable Long userId) { + String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(dbSession, componentDto.getKey(), componentDto.qualifier()); + applyPermissionTemplate(dbSession, applicablePermissionTemplateKey, componentDto.getId(), userId); } /** @@ -210,7 +222,8 @@ public class PermissionRepository { } throw new IllegalStateException(MessageFormat.format( "The \"{0}\" key matches multiple permission templates: {1}." - + " A system administrator must update these templates so that only one of them matches the key.", componentKey, + + " A system administrator must update these templates so that only one of them matches the key.", + componentKey, templatesNames.toString())); } } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java index 61869c93115..d327cee447c 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java @@ -19,10 +19,6 @@ */ package org.sonar.db.permission; -import static com.google.common.collect.Maps.newHashMap; -import static java.lang.String.format; -import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; - import com.google.common.annotations.VisibleForTesting; import java.util.Date; import java.util.HashMap; @@ -40,8 +36,14 @@ import org.sonar.api.web.UserRole; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper; +import static com.google.common.collect.Maps.newHashMap; +import static java.lang.String.format; +import static java.util.Collections.emptyList; +import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; + public class PermissionTemplateDao implements Dao { public static final String QUERY_PARAMETER = "query"; @@ -155,18 +157,26 @@ public class PermissionTemplateDao implements Dao { @CheckForNull public PermissionTemplateDto selectByUuidWithUserAndGroupPermissions(DbSession session, String templateUuid) { - PermissionTemplateDto permissionTemplate; + PermissionTemplateDto template; PermissionTemplateMapper mapper = mapper(session); - permissionTemplate = mapper.selectByUuid(templateUuid); - PermissionTemplateDto templateUsersPermissions = mapper.selectTemplateUsersPermissions(templateUuid); - if (templateUsersPermissions != null) { - permissionTemplate.setUsersPermissions(templateUsersPermissions.getUsersPermissions()); + template = mapper.selectByUuid(templateUuid); + if (template == null) { + return null; } - PermissionTemplateDto templateGroupsPermissions = mapper.selectTemplateGroupsPermissions(templateUuid); - if (templateGroupsPermissions != null) { - permissionTemplate.setGroupsByPermission(templateGroupsPermissions.getGroupsPermissions()); - } - return permissionTemplate; + + PermissionTemplateDto templateWithUserPermissions = mapper.selectTemplateUsersPermissions(templateUuid); + List userPermissions = templateWithUserPermissions == null ? emptyList() : templateWithUserPermissions.getUsersPermissions(); + template.setUsersPermissions(userPermissions); + + PermissionTemplateDto templateWithGroupPermissions = mapper.selectTemplateGroupsPermissions(templateUuid); + List groupPermissions = templateWithGroupPermissions == null ? emptyList() : templateWithGroupPermissions.getGroupsPermissions(); + template.setGroupsByPermission(groupPermissions); + + PermissionTemplateDto templateWithCharacteristics = mapper.selectTemplateCharacteristics(templateUuid); + List characteristics = templateWithCharacteristics == null ? emptyList() : templateWithCharacteristics.getCharacteristics(); + template.setCharacteristics(characteristics); + + return template; } @CheckForNull @@ -384,15 +394,14 @@ public class PermissionTemplateDao implements Dao { } /** - * Load permission template and load associated collections of users and groups permissions + * Load permission template and load associated collections of users and groups permissions, and characteristics */ - @VisibleForTesting PermissionTemplateDto selectPermissionTemplateWithPermissions(DbSession session, String templateUuid) { - PermissionTemplateDto permissionTemplateDto = selectByUuid(session, templateUuid); - if (permissionTemplateDto == null) { + PermissionTemplateDto template = selectByUuid(session, templateUuid); + if (template == null) { throw new IllegalArgumentException("Could not retrieve permission template with uuid " + templateUuid); } - PermissionTemplateDto templateWithPermissions = selectByUuidWithUserAndGroupPermissions(session, permissionTemplateDto.getUuid()); + PermissionTemplateDto templateWithPermissions = selectByUuidWithUserAndGroupPermissions(session, template.getUuid()); if (templateWithPermissions == null) { throw new IllegalArgumentException("Could not retrieve permissions for template with uuid " + templateUuid); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDto.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDto.java index ad14008096f..e268ffa0075 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDto.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDto.java @@ -23,6 +23,7 @@ import java.util.Date; import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; public class PermissionTemplateDto { @@ -33,6 +34,7 @@ public class PermissionTemplateDto { private String keyPattern; private List usersPermissions; private List groupsPermissions; + private List characteristics; private Date createdAt; private Date updatedAt; @@ -143,4 +145,13 @@ public class PermissionTemplateDto { this.updatedAt = updatedAt; return this; } + + public List getCharacteristics() { + return characteristics; + } + + public PermissionTemplateDto setCharacteristics(List characteristics) { + this.characteristics = characteristics; + return this; + } } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java index afb01562a94..1551cef2adb 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java @@ -50,6 +50,8 @@ public interface PermissionTemplateMapper { PermissionTemplateDto selectTemplateGroupsPermissions(String templateKey); + PermissionTemplateDto selectTemplateCharacteristics(String templateKey); + void insertUserPermission(PermissionTemplateUserDto permissionTemplateUser); void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml index e7df62ba258..f84b0231441 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml @@ -254,6 +254,21 @@ AND (g.name IS NOT NULL OR ptg.group_id IS NULL) + +