]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7746 Delete permission template characteristics when deleting a permission... 1041/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 15 Jun 2016 08:57:58 +0000 (10:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 16 Jun 2016 08:58:10 +0000 (10:58 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java
sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDaoTest.java
sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate-result.xml
sonar-db/src/test/resources/org/sonar/db/permission/PermissionTemplateDaoTest/deletePermissionTemplate.xml

index bbeecea43c50d4fb4644e873243bdb95bff9264a..7e5b5f56fde60fb7e0d6932ce02d5df0c5f676d3 100644 (file)
  */
 package org.sonar.server.permission.ws;
 
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
+import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+
 import java.util.Set;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -30,13 +37,6 @@ import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
 
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
-import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
-import static org.sonar.server.ws.WsUtils.checkRequest;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-
 public class DeleteTemplateAction implements PermissionsWsAction {
   private final DbClient dbClient;
   private final UserSession userSession;
index fb26767c883ca21848d37bc3b272bacf1e7b4203..e0fc453cf1d2b92cb9cd4d275678cf39af6086ec 100644 (file)
  */
 package org.sonar.server.permission.ws.template;
 
+import static com.google.common.primitives.Longs.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.internal.util.collections.Sets.newSet;
+import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+
 import java.util.Collections;
+import java.util.Date;
 import javax.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Rule;
@@ -34,6 +44,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.GroupTesting;
 import org.sonar.db.user.UserDto;
@@ -51,14 +62,6 @@ import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.mockito.internal.util.collections.Sets.newSet;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-
 public class DeleteTemplateActionTest {
 
   static final String TEMPLATE_UUID = "permission-template-uuid";
@@ -153,6 +156,22 @@ public class DeleteTemplateActionTest {
     newRequest(null);
   }
 
+  @Test
+  public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
+    dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
+      .setPermission(UserRole.USER)
+      .setTemplateId(permissionTemplate.getId())
+      .setWithProjectCreator(true)
+      .setCreatedAt(new Date().getTime())
+      .setUpdatedAt(new Date().getTime()));
+    dbSession.commit();
+    assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).hasSize(1);
+
+    newRequest(TEMPLATE_UUID);
+
+    assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).isEmpty();
+  }
+
   private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
     dbClient.permissionTemplateDao().insert(dbSession, template);
     UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true));
index c802286ee81b18480bc9c7eb8c0f994475c1cd10..61869c93115d26b4b498944d707ec4e515c8faee 100644 (file)
  */
 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;
@@ -36,10 +40,7 @@ import org.sonar.api.web.UserRole;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
-
-import static com.google.common.collect.Maps.newHashMap;
-import static java.lang.String.format;
-import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
 
 public class PermissionTemplateDao implements Dao {
 
@@ -202,10 +203,6 @@ public class PermissionTemplateDao implements Dao {
     return mapper(dbSession).countAll(upperCasedNameQuery);
   }
 
-  public int countAll(DbSession session) {
-    return mapper(session).countAll(null);
-  }
-
   private static String toUppercaseSqlQuery(String nameMatch) {
     String wildcard = "%";
     return format("%s%s%s", wildcard, nameMatch.toUpperCase(Locale.ENGLISH), wildcard);
@@ -254,6 +251,7 @@ public class PermissionTemplateDao implements Dao {
     PermissionTemplateMapper mapper = mapper(session);
     mapper.deleteUserPermissions(templateId);
     mapper.deleteGroupPermissions(templateId);
+    session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
     mapper.delete(templateId);
   }
 
index 05e9ff0360f1f7039e2ce369dd10f7e713b6803b..51204200c87df41806c6261327b6a2c376675daa 100644 (file)
 
 package org.sonar.db.permission.template;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.emptyList;
+import static java.util.Objects.requireNonNull;
+
 import java.util.List;
 import java.util.Optional;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Collections.emptyList;
-import static java.util.Objects.requireNonNull;
-
 public class PermissionTemplateCharacteristicDao implements Dao {
   public List<PermissionTemplateCharacteristicDto> selectByTemplateIds(DbSession dbSession, List<Long> templateIds) {
     return templateIds.isEmpty() ? emptyList() : mapper(dbSession).selectByTemplateIds(templateIds);
index 666a7d19fdb9b2d7af0e122501541c995a600011..4cd80559984f2f1dc75ef3dd29586c9284612b44 100644 (file)
@@ -33,4 +33,6 @@ public interface PermissionTemplateCharacteristicMapper {
   void insert(PermissionTemplateCharacteristicDto templatePermissionDto);
 
   void update(PermissionTemplateCharacteristicDto templatePermissionDto);
+
+  void deleteByTemplateId(long id);
 }
index ae90a6cd456c80d6fd8e1eb1a981c52feeb580da..59834cca7ad135c77d660a10ebf847854069c29d 100644 (file)
@@ -49,4 +49,9 @@
     updated_at=#{updatedAt, jdbcType=BIGINT}
     where id=#{id}
   </update>
+
+  <delete id="deleteByTemplateId" parameterType="long">
+    DELETE FROM perm_tpl_characteristics
+    WHERE template_id = #{permissionTemplateId}
+  </delete>
 </mapper>
index ca3dbee1f0a62c521ca5160008b97102e74a2713..b99319fa790ca26e9a9dfd430b3f270b5cf9769b 100644 (file)
  */
 package org.sonar.db.permission;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.web.UserRole.ADMIN;
+import static org.sonar.api.web.UserRole.CODEVIEWER;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.api.web.UserRole.USER;
+import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.user.GroupTesting.newGroupDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
+
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -38,17 +49,6 @@ import org.sonar.db.DbTester;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.api.web.UserRole.ADMIN;
-import static org.sonar.api.web.UserRole.CODEVIEWER;
-import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.db.user.GroupTesting.newGroupDto;
-import static org.sonar.db.user.UserTesting.newUserDto;
-
 
 public class PermissionTemplateDaoTest {
 
@@ -159,6 +159,7 @@ public class PermissionTemplateDaoTest {
     session.commit();
 
     checkTemplateTables("deletePermissionTemplate-result.xml");
+    db.assertDbUnitTable(getClass(), "deletePermissionTemplate-result.xml", "perm_tpl_characteristics");
   }
 
   @Test
index 9d5f97bcedd49cd42d1a01d66bbad4ebce356c2c..ba2f29918ffc664d6f2367aaeea7e61c7b439f39 100644 (file)
 
 package org.sonar.db.permission.template;
 
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.primitives.Longs.asList;
+import static java.util.Collections.emptyList;
+import static org.assertj.core.api.Assertions.assertThat;
+
 import java.util.List;
 import java.util.Optional;
 import org.junit.Rule;
@@ -30,10 +35,6 @@ import org.sonar.api.web.UserRole;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 
-import static com.google.common.collect.Lists.newArrayList;
-import static java.util.Collections.emptyList;
-import static org.assertj.core.api.Assertions.assertThat;
-
 public class PermissionTemplateCharacteristicDaoTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
@@ -179,4 +180,28 @@ public class PermissionTemplateCharacteristicDaoTest {
       .setCreatedAt(123_456_789L)
       .setUpdatedAt(2_000_000_000L));
   }
+
+  @Test
+  public void delete_by_permission_template_id() throws Exception {
+    underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
+      .setPermission(UserRole.USER)
+      .setTemplateId(1L)
+      .setWithProjectCreator(true)
+      .setCreatedAt(123_456_789L)
+      .setUpdatedAt(2_000_000_000L));
+    underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
+      .setPermission(UserRole.USER)
+      .setTemplateId(2L)
+      .setWithProjectCreator(true)
+      .setCreatedAt(123_456_789L)
+      .setUpdatedAt(2_000_000_000L));
+
+    assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(1);
+    assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(2);
+
+    dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(1L);
+
+    assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(0);
+    assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(1);
+  }
 }
index 115b732cb6472e347a4792b81d230db2c87700ee..29e39f9d91b85e782581cc924d01a86febdae845 100644 (file)
@@ -2,7 +2,9 @@
   <permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
                         created_at="[null]" updated_at="[null]"/>
 
-  <perm_templates_users/>
+  <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
 
-  <perm_templates_groups/>
+  <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+  <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
 </dataset>
index 0520355af3127750b16d1365236badf53a8d6d3f..ad3dea1aabf66de75e26daee3dd58462819aaa41 100644 (file)
@@ -7,8 +7,14 @@
   <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
   <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
   <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+  <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
 
   <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
   <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
   <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+  <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+  <perm_tpl_characteristics id="1" template_id="1" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+  <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+
 </dataset>