aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2013-09-25 18:27:15 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2013-09-26 14:21:47 +0200
commitb9a5dfe83f80f50ae45b8f0fa0b2096de167e0aa (patch)
tree5b9f2786d6a209f16cf53932c1e2fb7bf61f4737 /sonar-core
parente4635fd7e892cc7f7a91d4d132445c9b85522bf2 (diff)
downloadsonarqube-b9a5dfe83f80f50ae45b8f0fa0b2096de167e0aa.tar.gz
sonarqube-b9a5dfe83f80f50ae45b8f0fa0b2096de167e0aa.zip
SONAR-4711 Allow creation and query of empty projects
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/permission/PermissionFacade.java82
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java88
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java15
-rw-r--r--sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java2
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml13
-rw-r--r--sonar-core/src/test/java/org/sonar/core/permission/PermissionFacadeTest.java10
-rw-r--r--sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java4
7 files changed, 120 insertions, 94 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/permission/PermissionFacade.java b/sonar-core/src/main/java/org/sonar/core/permission/PermissionFacade.java
index 08c5147caeb..d60b8dacbd3 100644
--- a/sonar-core/src/main/java/org/sonar/core/permission/PermissionFacade.java
+++ b/sonar-core/src/main/java/org/sonar/core/permission/PermissionFacade.java
@@ -20,17 +20,26 @@
package org.sonar.core.permission;
+import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.ServerComponent;
+import org.sonar.api.config.Settings;
import org.sonar.api.security.DefaultGroups;
import org.sonar.api.task.TaskComponent;
+import org.sonar.api.web.UserRole;
import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.user.*;
+import org.sonar.core.user.GroupDto;
+import org.sonar.core.user.GroupRoleDto;
+import org.sonar.core.user.RoleDao;
+import org.sonar.core.user.UserDao;
+import org.sonar.core.user.UserRoleDto;
import javax.annotation.Nullable;
import java.util.List;
+import static com.google.common.collect.Lists.newArrayList;
+
/**
* Internal use only
*
@@ -44,12 +53,14 @@ public class PermissionFacade implements TaskComponent, ServerComponent {
private final RoleDao roleDao;
private final UserDao userDao;
private final PermissionTemplateDao permissionTemplateDao;
+ private final Settings settings;
- public PermissionFacade(MyBatis myBatis, RoleDao roleDao, UserDao userDao, PermissionTemplateDao permissionTemplateDao) {
+ public PermissionFacade(MyBatis myBatis, RoleDao roleDao, UserDao userDao, PermissionTemplateDao permissionTemplateDao, Settings settings) {
this.myBatis = myBatis;
this.roleDao = roleDao;
this.userDao = userDao;
this.permissionTemplateDao = permissionTemplateDao;
+ this.settings = settings;
}
public void insertUserPermission(@Nullable Long resourceId, Long userId, String permission, @Nullable SqlSession session) {
@@ -190,4 +201,71 @@ public class PermissionFacade implements TaskComponent, ServerComponent {
public List<String> selectUserPermissions(String user, Long componentId) {
return roleDao.selectUserPermissions(user, componentId);
}
+
+ public void grantDefaultRoles(Long componentId, String qualifier) {
+ SqlSession session = myBatis.openSession();
+ try {
+ removeAllPermissions(componentId, session);
+ grantDefaultRoles(componentId, qualifier, UserRole.ADMIN, session);
+ grantDefaultRoles(componentId, qualifier, UserRole.USER, session);
+ grantDefaultRoles(componentId, qualifier, UserRole.CODEVIEWER, session);
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ private void grantDefaultRoles(Long resourceId, String qualifier, String role, SqlSession session) {
+ PermissionTemplateDto applicablePermissionTemplate = getDefaultPermissionTemplate(qualifier);
+
+ List<Long> groupIds = getEligibleGroups(role, applicablePermissionTemplate);
+ for (Long groupId : groupIds) {
+ insertGroupPermission(resourceId, groupId, role, session);
+ }
+
+ List<Long> userIds = getEligibleUsers(role, applicablePermissionTemplate);
+ for (Long userId : userIds) {
+ insertUserPermission(resourceId, userId, role, session);
+ }
+ }
+
+ private List<Long> getEligibleGroups(String role, PermissionTemplateDto permissionTemplate) {
+ List<Long> eligibleGroups = newArrayList();
+ List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplate.getGroupsPermissions();
+ if (groupsPermissions != null) {
+ for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
+ if (role.equals(groupPermission.getPermission())) {
+ Long groupId = groupPermission.getGroupId() != null ? groupPermission.getGroupId() : null;
+ eligibleGroups.add(groupId);
+ }
+ }
+ }
+ return eligibleGroups;
+ }
+
+ private List<Long> getEligibleUsers(String role, PermissionTemplateDto permissionTemplate) {
+ List<Long> eligibleUsers = newArrayList();
+ List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
+ if (usersPermissions != null) {
+ for (PermissionTemplateUserDto userPermission : usersPermissions) {
+ if (role.equals(userPermission.getPermission())) {
+ eligibleUsers.add(userPermission.getUserId());
+ }
+ }
+ }
+ return eligibleUsers;
+ }
+
+ private PermissionTemplateDto getDefaultPermissionTemplate(String qualifier) {
+ String qualifierTemplateKey = settings.getString("sonar.permission.template." + qualifier + ".default");
+ if (!StringUtils.isBlank(qualifierTemplateKey)) {
+ return getPermissionTemplate(qualifierTemplateKey);
+ }
+
+ String defaultTemplateKey = settings.getString("sonar.permission.template.default");
+ if (StringUtils.isBlank(defaultTemplateKey)) {
+ throw new IllegalStateException("At least one default permission template should be defined");
+ }
+ return getPermissionTemplate(defaultTemplateKey);
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java b/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java
index ffe91a333d4..4a8451af1ad 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java
@@ -19,35 +19,23 @@
*/
package org.sonar.core.resource;
-import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
-import org.sonar.api.config.Settings;
import org.sonar.api.resources.Resource;
import org.sonar.api.security.ResourcePermissions;
-import org.sonar.api.web.UserRole;
import org.sonar.core.permission.PermissionFacade;
-import org.sonar.core.permission.PermissionTemplateDto;
-import org.sonar.core.permission.PermissionTemplateGroupDto;
-import org.sonar.core.permission.PermissionTemplateUserDto;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.user.UserDto;
import org.sonar.core.user.UserMapper;
-import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
-
/**
* @since 3.2
*/
public class DefaultResourcePermissions implements ResourcePermissions {
- private final Settings settings;
private final MyBatis myBatis;
private final PermissionFacade permissionFacade;
- public DefaultResourcePermissions(Settings settings, MyBatis myBatis, PermissionFacade permissionFacade) {
- this.settings = settings;
+ public DefaultResourcePermissions(MyBatis myBatis, PermissionFacade permissionFacade) {
this.myBatis = myBatis;
this.permissionFacade = permissionFacade;
}
@@ -90,78 +78,6 @@ public class DefaultResourcePermissions implements ResourcePermissions {
}
public void grantDefaultRoles(Resource resource) {
- if (resource.getId() != null) {
- SqlSession session = myBatis.openSession();
- try {
- removeRoles(resource, session);
- grantDefaultRoles(resource, UserRole.ADMIN, session);
- grantDefaultRoles(resource, UserRole.USER, session);
- grantDefaultRoles(resource, UserRole.CODEVIEWER, session);
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- }
-
- private void removeRoles(Resource resource, SqlSession session) {
- Long resourceId = Long.valueOf(resource.getId());
- permissionFacade.removeAllPermissions(resourceId, session);
- }
-
- private void grantDefaultRoles(Resource resource, String role, SqlSession session) {
- PermissionTemplateDto applicablePermissionTemplate = getPermissionTemplate(resource.getQualifier());
-
- List<Long> groupIds = getEligibleGroups(role, applicablePermissionTemplate);
- for (Long groupId : groupIds) {
- Long resourceId = Long.valueOf(resource.getId());
- permissionFacade.insertGroupPermission(resourceId, groupId, role, session);
- }
-
- List<Long> userIds = getEligibleUsers(role, applicablePermissionTemplate);
- for (Long userId : userIds) {
- Long resourceId = Long.valueOf(resource.getId());
- permissionFacade.insertUserPermission(resourceId, userId, role, session);
- }
- }
-
- private List<Long> getEligibleGroups(String role, PermissionTemplateDto permissionTemplate) {
- List<Long> eligibleGroups = newArrayList();
- List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplate.getGroupsPermissions();
- if (groupsPermissions != null) {
- for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
- if (role.equals(groupPermission.getPermission())) {
- Long groupId = groupPermission.getGroupId() != null ? groupPermission.getGroupId() : null;
- eligibleGroups.add(groupId);
- }
- }
- }
- return eligibleGroups;
- }
-
- private List<Long> getEligibleUsers(String role, PermissionTemplateDto permissionTemplate) {
- List<Long> eligibleUsers = newArrayList();
- List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
- if (usersPermissions != null) {
- for (PermissionTemplateUserDto userPermission : usersPermissions) {
- if (role.equals(userPermission.getPermission())) {
- eligibleUsers.add(userPermission.getUserId());
- }
- }
- }
- return eligibleUsers;
- }
-
- private PermissionTemplateDto getPermissionTemplate(String qualifier) {
- String qualifierTemplateKey = settings.getString("sonar.permission.template." + qualifier + ".default");
- if (!StringUtils.isBlank(qualifierTemplateKey)) {
- return permissionFacade.getPermissionTemplate(qualifierTemplateKey);
- }
-
- String defaultTemplateKey = settings.getString("sonar.permission.template.default");
- if (StringUtils.isBlank(defaultTemplateKey)) {
- throw new IllegalStateException("At least one default permission template should be defined");
- }
- return permissionFacade.getPermissionTemplate(defaultTemplateKey);
+ permissionFacade.grantDefaultRoles(Long.valueOf(resource.getId()), resource.getQualifier());
}
}
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
index da292f939b0..2b15762cd97 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java
@@ -246,6 +246,21 @@ public class ResourceDao {
}
}
+ /**
+ * Return provisioned projects = enabled projects without snapshot
+ */
+ public List<Component> selectProvisionedProjects(Collection<String> qualifiers) {
+ if (qualifiers.isEmpty()) {
+ return Collections.emptyList();
+ }
+ SqlSession session = mybatis.openSession();
+ try {
+ return toComponents(session.getMapper(ResourceMapper.class).selectProvisionedProjects(qualifiers));
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public static ComponentDto toComponent(ResourceDto resourceDto){
return new ComponentDto()
.setId(resourceDto.getId())
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
index 488dbe38b2b..c3bcc36277c 100644
--- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceMapper.java
@@ -80,6 +80,8 @@ public interface ResourceMapper {
List<ResourceDto> selectGhostsProjects(@Param("qualifiers") Collection<String> qualifier);
+ List<ResourceDto> selectProvisionedProjects(@Param("qualifiers") Collection<String> qualifier);
+
void insert(ResourceDto resource);
void update(ResourceDto resource);
diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
index 3cec051123a..e1ae650a3d1 100644
--- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml
@@ -179,6 +179,19 @@
</where>
</select>
+ <select id="selectProvisionedProjects" parameterType="map" resultMap="resourceResultMap">
+ select p.* from projects p
+ left join snapshots s on s.project_id=p.id
+ <where>
+ and s.id is null
+ <if test="qualifiers != null and qualifiers.size() > 0">
+ and <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator=" or " close=")">p.qualifier=#{qualifier}
+ </foreach>
+ </if>
+ and p.copy_resource_id is null
+ </where>
+ </select>
+
<select id="selectAuthorizedChildrenComponentIds" parameterType="map" resultType="int">
<include refid="selectAuthorizedChildrenComponentIdsQuery" />
</select>
diff --git a/sonar-core/src/test/java/org/sonar/core/permission/PermissionFacadeTest.java b/sonar-core/src/test/java/org/sonar/core/permission/PermissionFacadeTest.java
index 18cb8525186..6c5e3fbacf5 100644
--- a/sonar-core/src/test/java/org/sonar/core/permission/PermissionFacadeTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/permission/PermissionFacadeTest.java
@@ -20,6 +20,8 @@
package org.sonar.core.permission;
+import org.sonar.api.config.Settings;
+
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Rule;
@@ -30,7 +32,6 @@ import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.user.RoleDao;
import org.sonar.core.user.UserDao;
-
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -48,7 +49,8 @@ public class PermissionFacadeTest extends AbstractDaoTestCase {
RoleDao roleDao = new RoleDao(getMyBatis());
UserDao userDao = new UserDao(getMyBatis());
permissionTemplateDao = new PermissionTemplateDao(getMyBatis());
- permissionFacade = new PermissionFacade(getMyBatis(), roleDao, userDao, permissionTemplateDao);
+ Settings settings = new Settings();
+ permissionFacade = new PermissionFacade(getMyBatis(), roleDao, userDao, permissionTemplateDao, settings);
}
@Test
@@ -163,7 +165,7 @@ public class PermissionFacadeTest extends AbstractDaoTestCase {
when(permissionTemplateDao.selectTemplateByKey("test_template")).thenReturn(permissionTemplateDto);
when(permissionTemplateDao.selectPermissionTemplate("Test template")).thenReturn(templateWithPermissions);
- permissionFacade = new PermissionFacade(null, null, null, permissionTemplateDao);
+ permissionFacade = new PermissionFacade(null, null, null, permissionTemplateDao, null);
PermissionTemplateDto permissionTemplate = permissionFacade.getPermissionTemplate("test_template");
@@ -176,7 +178,7 @@ public class PermissionFacadeTest extends AbstractDaoTestCase {
permissionTemplateDao = mock(PermissionTemplateDao.class);
- permissionFacade = new PermissionFacade(null, null, null, permissionTemplateDao);
+ permissionFacade = new PermissionFacade(null, null, null, permissionTemplateDao, null);
permissionFacade.getPermissionTemplate("unmatched");
}
diff --git a/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java b/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java
index 6edacf3d163..5f6b3af02b5 100644
--- a/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java
@@ -48,8 +48,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase {
public void initResourcePermissions() {
settings = new Settings();
PermissionFacade permissionFacade = new PermissionFacade(getMyBatis(),
- new RoleDao(getMyBatis()), new UserDao(getMyBatis()), new PermissionTemplateDao(getMyBatis()));
- permissions = new DefaultResourcePermissions(settings, getMyBatis(), permissionFacade);
+ new RoleDao(getMyBatis()), new UserDao(getMyBatis()), new PermissionTemplateDao(getMyBatis()), settings);
+ permissions = new DefaultResourcePermissions(getMyBatis(), permissionFacade);
}
@Test