From: Julien Lancelot Date: Tue, 21 May 2013 15:34:56 +0000 (+0200) Subject: SONAR-4301 Create queries to select all authorized projects for a user and to select... X-Git-Tag: 3.6~321 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b9f5d71ddbf435f23f328a9134f9b27089c572b2;p=sonarqube.git SONAR-4301 Create queries to select all authorized projects for a user and to select issues id and project ids from issue query --- diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java index fe8ba6b950e..fc3d571fb48 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java @@ -22,6 +22,8 @@ package org.sonar.core.issue.db; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; +import org.apache.ibatis.session.ResultContext; +import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; @@ -43,6 +45,8 @@ import static com.google.common.collect.Maps.newHashMap; */ public class IssueDao implements BatchComponent, ServerComponent { + private final static Integer MAX_RESULT = 10000; + private final MyBatis mybatis; public IssueDao(MyBatis mybatis) { @@ -98,6 +102,34 @@ public class IssueDao implements BatchComponent, ServerComponent { return mapper.selectIssueAndComponentIds(query); } + @VisibleForTesting + List selectIssueAndProjectIds(IssueQuery query, Integer maxResults) { + SqlSession session = mybatis.openSession(); + try { + return selectIssueAndProjectIds(query, maxResults, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + /** + * The returned IssueDto list contains only the issue id and the project id + */ + public List selectIssueAndProjectIds(IssueQuery query, final Integer maxResults, SqlSession session) { + final List issues = newArrayList(); + ResultHandler resultHandler = new ResultHandler(){ + @Override + public void handleResult(ResultContext context) { + issues.add((IssueDto) context.getResultObject()); + if (issues.size() >= maxResults) { + context.stop(); + } + } + }; + session.select("selectIssueAndProjectIds", query, resultHandler); + return issues; + } + @VisibleForTesting Collection selectByIds(Collection ids, IssueQuery.Sort sort, Boolean asc) { SqlSession session = mybatis.openSession(); diff --git a/sonar-core/src/main/java/org/sonar/core/user/AuthorizationDao.java b/sonar-core/src/main/java/org/sonar/core/user/AuthorizationDao.java index ca29c79c7b2..df3addad3ed 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/AuthorizationDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/AuthorizationDao.java @@ -34,6 +34,7 @@ import java.util.Map; import java.util.Set; import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newHashMap; public class AuthorizationDao implements ServerComponent { @@ -74,4 +75,29 @@ public class AuthorizationDao implements ServerComponent { public boolean isAuthorizedComponentId(int componentId, @Nullable Integer userId, String role) { return keepAuthorizedComponentIds(Sets.newHashSet(componentId), userId, role).size() == 1; } + + public Set selectAuthorizedRootProjectsIds(@Nullable Integer userId, String role) { + SqlSession session = mybatis.openSession(); + try { + return selectAuthorizedRootProjectsIds(userId, role, session); + + } finally { + MyBatis.closeQuietly(session); + } + } + + public Set selectAuthorizedRootProjectsIds(@Nullable Integer userId, String role, SqlSession session) { + String sql; + Map params = newHashMap(); + if (userId == null) { + sql = "selectAuthorizedRootProjectsIdsForAnonymous"; + params.put("role", role); + } else { + sql = "selectAuthorizedRootProjectsIdsForUser"; + params.put("userId", userId); + params.put("role", role); + } + + return Sets.newHashSet(session.selectList(sql, params)); + } } diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 4edb2f7a599..3023ba9b728 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -157,6 +157,11 @@ + + + + + + diff --git a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java index fe83068bf2e..8ed5fdc2384 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java @@ -301,6 +301,18 @@ public class IssueDaoTest extends AbstractDaoTestCase { assertThat(results).hasSize(3); } + @Test + public void should_select_issue_and_project_ids() { + setupData("shared", "should_select_issue_and_project_ids"); + + IssueQuery query = IssueQuery.builder().build(); + List results = dao.selectIssueAndProjectIds(query, 5); + assertThat(results).hasSize(3); + + results = dao.selectIssueAndProjectIds(query, 2); + assertThat(results).hasSize(2); + } + @Test public void should_select_open_issues() { setupData("shared", "should_select_open_issues"); diff --git a/sonar-core/src/test/java/org/sonar/core/user/AuthorizationDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/AuthorizationDaoTest.java index ce9c8e2d224..6f9465d4b47 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/AuthorizationDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/AuthorizationDaoTest.java @@ -106,4 +106,47 @@ public class AuthorizationDaoTest extends AbstractDaoTestCase { null, "admin"); assertThat(componentIds).isEmpty(); } + + @Test + public void should_return_root_project_ids_for_user() { + setupData("should_return_root_project_ids_for_user"); + + AuthorizationDao authorization = new AuthorizationDao(getMyBatis()); + Set rootProjectIds = authorization.selectAuthorizedRootProjectsIds(USER, "user"); + + assertThat(rootProjectIds).containsOnly(PROJECT); + + // user does not have the role "admin" + rootProjectIds = authorization.selectAuthorizedRootProjectsIds(USER, "admin"); + assertThat(rootProjectIds).isEmpty(); + } + + @Test + public void should_return_root_project_ids_for_group() { + // but user is not in an authorized group + setupData("should_return_root_project_ids_for_group"); + + AuthorizationDao authorization = new AuthorizationDao(getMyBatis()); + Set rootProjectIds = authorization.selectAuthorizedRootProjectsIds(USER, "user"); + + assertThat(rootProjectIds).containsOnly(PROJECT); + + // user does not have the role "admin" + rootProjectIds = authorization.selectAuthorizedRootProjectsIds(USER, "admin"); + assertThat(rootProjectIds).isEmpty(); + } + + @Test + public void should_return_root_project_ids_for_anonymous() { + setupData("should_return_root_project_ids_for_anonymous"); + + AuthorizationDao authorization = new AuthorizationDao(getMyBatis()); + Set rootProjectIds = authorization.selectAuthorizedRootProjectsIds(null, "user"); + + assertThat(rootProjectIds).containsOnly(PROJECT); + + // group does not have the role "admin" + rootProjectIds = authorization.selectAuthorizedRootProjectsIds(null, "admin"); + assertThat(rootProjectIds).isEmpty(); + } } diff --git a/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueDaoTest/should_select_issue_and_project_ids.xml b/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueDaoTest/should_select_issue_and_project_ids.xml new file mode 100644 index 00000000000..3d7bd96af21 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/issue/db/IssueDaoTest/should_select_issue_and_project_ids.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_anonymous.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_anonymous.xml new file mode 100644 index 00000000000..a474a8ad168 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_anonymous.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_group.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_group.xml new file mode 100644 index 00000000000..73a034687d7 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_group.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_user.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_user.xml new file mode 100644 index 00000000000..036563f4f9d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorizationDaoTest/should_return_root_project_ids_for_user.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +