aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java39
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java18
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java41
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java34
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_beginning.xml (renamed from server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date.xml)0
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_given_date.xml (renamed from server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date_return_dtos_after_given_date.xml)0
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_with_project.xml31
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/issue/db/IssueAuthorizationMapper.xml9
8 files changed, 91 insertions, 81 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java
index a222720e1ce..80ddf10c32e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java
@@ -21,7 +21,6 @@
package org.sonar.server.issue.db;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ImmutableList;
import org.apache.ibatis.session.ResultContext;
import org.sonar.api.security.DefaultGroups;
import org.sonar.api.utils.System2;
@@ -37,11 +36,8 @@ import org.sonar.server.search.action.UpsertDto;
import java.util.Date;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import static com.google.common.collect.Maps.newHashMap;
-
public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, IssueAuthorizationDto, String> implements DaoComponent {
public static final String PROJECT_KEY = "project";
@@ -106,41 +102,6 @@ public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, Iss
return finalParams;
}
- @Override
- public List<IssueAuthorizationDto> findAfterDate(DbSession session, Date date) {
-
- Map<String, Object> params = newHashMap();
- params.put("date", date);
- params.put("permission", UserRole.USER);
- params.put("anyone", DefaultGroups.ANYONE);
-
- Map<String, IssueAuthorizationDto> authorizationDtoMap = newHashMap();
-
- List<Map<String, Object>> rows = session.selectList("org.sonar.core.issue.db.IssueAuthorizationMapper.selectAfterDate", params);
- for (Map<String, Object> row : rows) {
- String project = (String) row.get("project");
- String user = (String) row.get("permissionUser");
- String group = (String) row.get("permissionGroup");
- Date updatedAt = (Date) row.get("updatedAt");
- IssueAuthorizationDto issueAuthorizationDto = authorizationDtoMap.get(project);
- if (issueAuthorizationDto == null) {
- issueAuthorizationDto = new IssueAuthorizationDto()
- .setProject(project)
- .setPermission(UserRole.USER);
- issueAuthorizationDto.setUpdatedAt(updatedAt);
- }
- if (group != null) {
- issueAuthorizationDto.addGroup(group);
- }
- if (user != null) {
- issueAuthorizationDto.addUser(user);
- }
- authorizationDtoMap.put(project, issueAuthorizationDto);
- }
-
- return ImmutableList.<IssueAuthorizationDto>copyOf(authorizationDtoMap.values());
- }
-
protected void doDeleteByKey(DbSession session, String key) {
// Nothing to do on db side, only remove the index (done in BaseDao)
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
index 95d78d725f6..065769e2b83 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
@@ -20,6 +20,7 @@
package org.sonar.server.permission;
+import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
@@ -37,6 +38,7 @@ import org.sonar.core.user.UserDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.issue.db.IssueAuthorizationDao;
import org.sonar.server.issue.index.IssueAuthorizationIndex;
import org.sonar.server.search.IndexClient;
import org.sonar.server.user.UserSession;
@@ -120,7 +122,7 @@ public class InternalPermissionService implements ServerComponent {
} finally {
session.close();
}
- synchronizePermissions();
+ synchronizePermissions(componentKey);
}
public void applyPermissionTemplate(Map<String, Object> params) {
@@ -147,13 +149,12 @@ public class InternalPermissionService implements ServerComponent {
throw new IllegalStateException("Unable to find component with key " + componentKey);
}
permissionFacade.applyPermissionTemplate(session, query.getTemplateKey(), component.getId());
+ synchronizePermissions(componentKey);
}
-
session.commit();
} finally {
session.close();
}
- synchronizePermissions();
}
private void changePermission(String permissionChange, Map<String, Object> params) {
@@ -178,8 +179,9 @@ public class InternalPermissionService implements ServerComponent {
} finally {
session.close();
}
- if (changed) {
- synchronizePermissions();
+ String project = permissionChangeQuery.component();
+ if (changed && project != null) {
+ synchronizePermissions(project);
}
}
@@ -272,12 +274,14 @@ public class InternalPermissionService implements ServerComponent {
}
}
- private void synchronizePermissions() {
+ private void synchronizePermissions(String projectKey) {
// The synchronisation cannot use an existing session, otherwise it's failing with the error :
// org.apache.ibatis.executor.ExecutorException: Executor was closed
DbSession session = dbClient.openSession(false);
try {
- dbClient.issueAuthorizationDao().synchronizeAfter(session, index.get(IssueAuthorizationIndex.class).getLastSynchronization());
+ dbClient.issueAuthorizationDao().synchronizeAfter(session,
+ index.get(IssueAuthorizationIndex.class).getLastSynchronization(),
+ ImmutableMap.of(IssueAuthorizationDao.PROJECT_KEY, projectKey));
} finally {
session.close();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java
index 82d5c732450..0b8a8c988c4 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/db/IssueAuthorizationDaoTest.java
@@ -20,12 +20,12 @@
package org.sonar.server.issue.db;
+import com.google.common.collect.ImmutableMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
-import org.sonar.core.issue.db.IssueAuthorizationDto;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.DbSession;
@@ -50,32 +50,35 @@ public class IssueAuthorizationDaoTest extends AbstractDaoTestCase {
}
@Test(expected = IllegalStateException.class)
- public void get_nullable_by_key_is_not_implemented(){
+ public void get_nullable_by_key_is_not_implemented() {
assertThat(dao.getNullableByKey(session, "sonar"));
}
@Test
- public void find_after_date(){
- setupData("find_after_date");
-
- Iterable<IssueAuthorizationDto> results = dao.findAfterDate(session, new Date(0));
- assertThat(results).hasSize(1);
-
- IssueAuthorizationDto dto = results.iterator().next();
- assertThat(dto.getProject()).isEqualTo("org.struts:struts");
- assertThat(dto.getKey()).isEqualTo("org.struts:struts");
- assertThat(dto.getPermission()).isEqualTo("user");
- assertThat(dto.getGroups()).containsExactly("Anyone", "devs");
- assertThat(dto.getUsers()).containsExactly("user1");
- assertThat(dto.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-01-01"));
+ public void synchronize_after_since_beginning() throws Exception {
+ setupData("synchronize_after_since_beginning");
+
+ assertThat(session.getActionCount()).isEqualTo(0);
+
+ dao.synchronizeAfter(session, new Date(0));
+
+ assertThat(session.getActionCount()).isEqualTo(1);
}
@Test
- public void find_after_date_return_dtos_after_given_date(){
- setupData("find_after_date_return_dtos_after_given_date");
+ public void synchronize_after_since_given_date() {
+ setupData("synchronize_after_since_given_date");
- assertThat(dao.findAfterDate(session, new Date(0))).hasSize(2);
+ dao.synchronizeAfter(session, DateUtils.parseDate("2014-09-01"));
+ assertThat(session.getActionCount()).isEqualTo(1);
+ }
- assertThat(dao.findAfterDate(session, DateUtils.parseDate("2014-09-01"))).hasSize(1);
+ @Test
+ public void synchronize_after_with_project() {
+ setupData("synchronize_after_with_project");
+
+ dao.synchronizeAfter(session, DateUtils.parseDate("2014-01-01"), ImmutableMap.of(IssueAuthorizationDao.PROJECT_KEY, "org.sonar:sample"));
+ assertThat(session.getActionCount()).isEqualTo(1);
}
+
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
index d78d72cd0b0..03a3b769f8d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
@@ -164,7 +164,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertUserPermission(eq((Long) null), eq(2L), eq("shareDashboard"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -178,7 +178,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertUserPermission(eq(10L), eq(2L), eq("user"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -189,7 +189,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteUserPermission(eq((Long) null), eq(2L), eq("profileadmin"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -202,7 +202,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteUserPermission(eq(10L), eq(2L), eq("codeviewer"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -213,7 +213,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertGroupPermission(eq((Long) null), eq(2L), eq("shareDashboard"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -227,7 +227,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertGroupPermission(eq(10L), eq(2L), eq("user"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -237,7 +237,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertGroupPermission(eq((Long) null), eq((Long) null), eq("profileadmin"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -250,7 +250,7 @@ public class InternalPermissionServiceTest {
service.addPermission(params);
verify(permissionFacade).insertGroupPermission(eq(10L), eq((Long) null), eq("user"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -261,7 +261,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteGroupPermission(eq((Long) null), eq(2L), eq("profileadmin"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -275,7 +275,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteGroupPermission(eq(10L), eq(2L), eq("codeviewer"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -286,7 +286,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteGroupPermission(eq((Long) null), eq((Long) null), eq("profileadmin"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verifyZeroInteractions(issueAuthorizationDao);
}
@Test
@@ -300,7 +300,7 @@ public class InternalPermissionServiceTest {
service.removePermission(params);
verify(permissionFacade).deleteGroupPermission(eq(10L), eq((Long) null), eq("codeviewer"), eq(session));
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test
@@ -439,7 +439,9 @@ public class InternalPermissionServiceTest {
verify(permissionFacade).applyPermissionTemplate(session, "my_template_key", 2L);
verify(permissionFacade).applyPermissionTemplate(session, "my_template_key", 3L);
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample1")));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample2")));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample3")));
}
@Test(expected = ForbiddenException.class)
@@ -480,7 +482,7 @@ public class InternalPermissionServiceTest {
service.applyPermissionTemplate(params);
verify(permissionFacade).applyPermissionTemplate(session, "my_template_key", 1L);
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "org.sample.Sample")));
}
@Test(expected = ForbiddenException.class)
@@ -513,7 +515,7 @@ public class InternalPermissionServiceTest {
service.applyDefaultPermissionTemplate(componentKey);
verify(permissionFacade).grantDefaultRoles(session, componentId, qualifier);
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "component")));
}
@Test(expected = ForbiddenException.class)
@@ -548,7 +550,7 @@ public class InternalPermissionServiceTest {
when(resourceDao.selectProvisionedProject(session, componentKey)).thenReturn(mock(ResourceDto.class));
service.applyDefaultPermissionTemplate(componentKey);
- verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class));
+ verify(issueAuthorizationDao).synchronizeAfter(eq(session), any(Date.class), eq(ImmutableMap.of("project", "component")));
}
private Map<String, Object> buildPermissionChangeParams(String login, String group, String permission) {
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_beginning.xml
index 9236b6d6391..9236b6d6391 100644
--- a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date.xml
+++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_beginning.xml
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date_return_dtos_after_given_date.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_given_date.xml
index eff050279eb..eff050279eb 100644
--- a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/find_after_date_return_dtos_after_given_date.xml
+++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_since_given_date.xml
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_with_project.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_with_project.xml
new file mode 100644
index 00000000000..c28f48753c7
--- /dev/null
+++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/db/IssueAuthorizationDaoTest/synchronize_after_with_project.xml
@@ -0,0 +1,31 @@
+
+<dataset>
+
+ <groups id="100" name="devs"/>
+
+ <users id="10" login="user1" name="User 1" email="user1@company.net" active="[true]"/>
+
+
+ <!-- Project 1 -->
+
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]"
+ authorization_updated_at="2014-01-01"/>
+
+ <user_roles id="1" user_id="10" resource_id="1" role="user"/>
+ <user_roles id="2" user_id="10" resource_id="1" role="admin"/>
+ <user_roles id="3" user_id="10" resource_id="2" role="user"/>
+
+
+ <!-- Project 2 -->
+
+ <projects id="2" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.sonar:sample" name="Struts"
+ description="the description" long_name="SonarQube Sample"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]"
+ authorization_updated_at="2014-01-02"/>
+
+ <user_roles id="10" user_id="10" resource_id="2" role="user"/>
+ <user_roles id="11" user_id="10" resource_id="2" role="admin"/>
+
+</dataset>
diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueAuthorizationMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueAuthorizationMapper.xml
index fe8b5b3981b..5a1f44d6c62 100644
--- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueAuthorizationMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueAuthorizationMapper.xml
@@ -32,6 +32,9 @@
INNER JOIN users ON users.id = user_roles.user_id
WHERE
projects.authorization_updated_at &gt;= #{date}
+ <if test="project != null">
+ and projects.kee = #{project}
+ </if>
UNION
-- groups without Anyone
SELECT
@@ -45,6 +48,9 @@
INNER JOIN groups ON groups.id = group_roles.group_id
WHERE
projects.authorization_updated_at &gt;= #{date}
+ <if test="project != null">
+ and projects.kee = #{project}
+ </if>
AND group_id IS NOT NULL
UNION
-- Anyone groups
@@ -58,6 +64,9 @@
INNER JOIN group_roles ON group_roles.resource_id = projects.id AND group_roles.role = #{permission}
WHERE
projects.authorization_updated_at &gt;= #{date}
+ <if test="project != null">
+ and projects.kee = #{project}
+ </if>
AND group_roles.group_id IS NULL
) project_authorization
</select>