]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8134 remove RoleDao#deleteGroupRole()
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 12 Oct 2016 22:54:42 +0000 (00:54 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 13 Oct 2016 10:18:42 +0000 (12:18 +0200)
replaced by GroupPermissionDao#delete()

14 files changed:
server/sonar-server/src/main/java/org/sonar/server/permission/GroupPermissionChanger.java
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java
sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java
sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java
sonar-db/src/main/java/org/sonar/db/user/RoleDao.java
sonar-db/src/main/java/org/sonar/db/user/RoleMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
sonar-db/src/main/resources/org/sonar/db/user/RoleMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java
sonar-db/src/test/java/org/sonar/db/permission/PermissionRepositoryTest.java
sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java
sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java
sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission-result.xml [deleted file]
sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission.xml [deleted file]

index 32d34717fa57d1123fe295f2692bf19ff2ef6283..f2c382a5463a32b4ad31232de9a8eab56b5fc525 100644 (file)
@@ -58,12 +58,11 @@ public class GroupPermissionChanger {
         break;
       case REMOVE:
         checkAdminUsersExistOutsideTheRemovedGroup(dbSession, change);
-        GroupPermissionDto deletedDto = new GroupPermissionDto()
-          .setRole(change.getPermission())
-          .setOrganizationUuid(change.getOrganizationUuid())
-          .setGroupId(change.getGroupIdOrAnyone().getId())
-          .setResourceId(change.getNullableProjectId());
-        dbClient.roleDao().deleteGroupRole(deletedDto, dbSession);
+        dbClient.groupPermissionDao().delete(dbSession,
+          change.getPermission(),
+          change.getOrganizationUuid(),
+          change.getGroupIdOrAnyone().getId(),
+          change.getNullableProjectId());
         break;
       default:
         throw new UnsupportedOperationException("Unsupported permission change: " + change.getOperation());
index 37e375b4b46fc1d3007628b3af36ee2dd0f5298e..c1685e3977654ca1475e1da21cd9bb27aa557757 100644 (file)
@@ -120,6 +120,24 @@ public class GroupPermissionDao implements Dao {
     mapper(dbSession).deleteByRootComponentId(rootComponentId);
   }
 
+  /**
+   * Delete a single permission. It can be:
+   * <ul>
+   *   <li>a global permission granted to a group</li>
+   *   <li>a global permission granted to anyone</li>
+   *   <li>a permission granted to a group for a project</li>
+   *   <li>a permission granted to anyone for a project</li>
+   * </ul>
+   * @param dbSession
+   * @param permission the kind of permission
+   * @param organizationUuid UUID of organization, even if parameter {@code groupId} is not null
+   * @param groupId if null, then anyone, else id of group
+   * @param rootComponentId if null, then global permission, else id of root component (project)
+   */
+  public void delete(DbSession dbSession, String permission, String organizationUuid, @Nullable Long groupId, @Nullable Long rootComponentId) {
+    mapper(dbSession).delete(permission, organizationUuid, groupId, rootComponentId);
+  }
+
   private static GroupPermissionMapper mapper(DbSession session) {
     return session.getMapper(GroupPermissionMapper.class);
   }
index ca005bcc6177c22136ef1a7b03a7b534f4e03866..5a8138776c9efa9dca5f09043d3ec0f91aa3b29b 100644 (file)
@@ -61,4 +61,7 @@ public interface GroupPermissionMapper {
   void insert(GroupPermissionDto dto);
 
   void deleteByRootComponentId(@Param("rootComponentId") long componentId);
+
+  void delete(@Param("permission") String permission, @Param("organizationUuid") String organizationUuid,
+    @Nullable @Param("groupId") Long groupId, @Nullable @Param("rootComponentId") Long rootComponentId);
 }
index 53b335642965dc0e3fc3224bbe19ff3250b28559..d909d0ff0fe16699bb1a9d9b4af5601e6f61ee47 100644 (file)
@@ -56,15 +56,6 @@ public class PermissionRepository {
     this.settings = settings;
   }
 
-  public void deleteGroupPermission(@Nullable Long resourceId, @Nullable Long groupId, String permission, DbSession session) {
-    GroupPermissionDto groupRole = new GroupPermissionDto()
-      .setRole(permission)
-      .setGroupId(groupId)
-      .setResourceId(resourceId);
-    updateProjectAuthorizationDate(session, resourceId);
-    dbClient.roleDao().deleteGroupRole(groupRole, session);
-  }
-
   /**
    * For each modification of permission on a project, update the authorization_updated_at to help ES reindex only relevant changes
    */
index 04d8e82bde2dbabd0cd364e435c21f7ef85a0de0..2eb62c0efc6b1ea9b2cef937349d30abdc3ac621 100644 (file)
@@ -24,7 +24,6 @@ import javax.annotation.Nullable;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
-import org.sonar.db.permission.GroupPermissionDto;
 
 public class RoleDao implements Dao {
 
@@ -45,14 +44,6 @@ public class RoleDao implements Dao {
     return session.getMapper(RoleMapper.class).selectGroupPermissions(groupName, resourceId, DefaultGroups.isAnyone(groupName));
   }
 
-  /**
-   * @deprecated does not support organizations on anyone groups
-   */
-  @Deprecated
-  public void deleteGroupRole(GroupPermissionDto groupRole, DbSession session) {
-    mapper(session).deleteGroupRole(groupRole);
-  }
-
   private static int countResourceGroupRoles(DbSession session, Long resourceId) {
     return mapper(session).countResourceGroupRoles(resourceId);
   }
index 783514c5fafa30ce588d57949e10f076c9389f58..23d154d5cbecae5b0d4e53a64f8451c224afbf10 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.db.user;
 import java.util.List;
 import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
-import org.sonar.db.permission.GroupPermissionDto;
 
 /**
  * @since 3.2
@@ -36,8 +35,6 @@ public interface RoleMapper {
    */
   List<String> selectGroupPermissions(@Param("groupName") String groupName, @Nullable @Param("resourceId") Long resourceId, @Param("isAnyOneGroup") Boolean isAnyOneGroup);
 
-  void deleteGroupRole(GroupPermissionDto dto);
-
   int countResourceGroupRoles(Long resourceId);
 
   int countResourceUserRoles(long resourceId);
index 065111f7bfed1ddd5db055d7652fb6a5e43e6e08..dff26a62a71992a84832db19e071a04baf0289cf 100644 (file)
     delete from group_roles
     where resource_id=#{rootComponentId,jdbcType=BIGINT}
   </delete>
+
+  <delete id="delete" parameterType="map">
+    delete from group_roles
+    where
+    role=#{permission,jdbcType=VARCHAR} and
+    organization_uuid=#{organizationUuid,jdbcType=VARCHAR} and
+    <choose>
+      <when test="rootComponentId != null">
+        resource_id=#{rootComponentId,jdbcType=BIGINT}
+      </when>
+      <otherwise>
+        resource_id is null
+      </otherwise>
+    </choose>
+    and
+    <choose>
+      <when test="groupId != null">
+        group_id=#{groupId,jdbcType=BIGINT}
+      </when>
+      <otherwise>
+        group_id is null
+      </otherwise>
+    </choose>
+  </delete>
+
 </mapper>
index 27a020c8dce1e9402b85c8c50c7cf3d03963bd0d..0ac0d4355123eee6db0255989c3775f40f1a5147 100644 (file)
     </where>
   </select>
 
-  <delete id="deleteGroupRole" parameterType="map">
-    DELETE FROM group_roles
-    WHERE role=#{role}
-    AND
-    <choose>
-      <when test="resourceId != null">
-        resource_id=#{resourceId}
-      </when>
-      <otherwise>
-        resource_id IS NULL
-      </otherwise>
-    </choose>
-    AND
-    <choose>
-      <when test="groupId != null">
-        group_id=#{groupId}
-      </when>
-      <otherwise>
-        group_id IS NULL
-      </otherwise>
-    </choose>
-  </delete>
-
   <select id="countResourceUserRoles" parameterType="long" resultType="int">
     SELECT count(1)
     FROM user_roles WHERE resource_id=#{id}
index b1b4cc869f7fa38e7f82204c40d75bf2b1681766..014fafcbb69dec111f2fe08a8ef8c912407bc15e 100644 (file)
@@ -333,4 +333,72 @@ public class GroupPermissionDaoTest {
     assertThat(db.countSql("select count(id) from group_roles where resource_id=" + project1.getId())).isEqualTo(0);
     assertThat(db.countRowsOfTable("group_roles")).isEqualTo(2);
   }
+
+  @Test
+  public void delete_global_permission_from_group() {
+    GroupDto group1 = db.users().insertGroup();
+    ComponentDto project1 = db.components().insertProject();
+    db.users().insertPermissionOnAnyone("perm1");
+    db.users().insertPermissionOnGroup(group1, "perm2");
+    db.users().insertProjectPermissionOnGroup(group1, "perm3", project1);
+    db.users().insertProjectPermissionOnAnyone("perm4", project1);
+
+    underTest.delete(dbSession, "perm2", group1.getOrganizationUuid(), group1.getId(), null);
+    dbSession.commit();
+
+    assertThatNoPermission("perm2");
+    assertThat(db.countRowsOfTable("group_roles")).isEqualTo(3);
+  }
+
+  @Test
+  public void delete_global_permission_from_anyone() {
+    GroupDto group1 = db.users().insertGroup();
+    ComponentDto project1 = db.components().insertProject();
+    db.users().insertPermissionOnAnyone("perm1");
+    db.users().insertPermissionOnGroup(group1, "perm2");
+    db.users().insertProjectPermissionOnGroup(group1, "perm3", project1);
+    db.users().insertProjectPermissionOnAnyone("perm4", project1);
+
+    underTest.delete(dbSession, "perm1", group1.getOrganizationUuid(), null, null);
+    dbSession.commit();
+
+    assertThatNoPermission("perm1");
+    assertThat(db.countRowsOfTable("group_roles")).isEqualTo(3);
+  }
+
+  @Test
+  public void delete_project_permission_from_group() {
+    GroupDto group1 = db.users().insertGroup();
+    ComponentDto project1 = db.components().insertProject();
+    db.users().insertPermissionOnAnyone("perm1");
+    db.users().insertPermissionOnGroup(group1, "perm2");
+    db.users().insertProjectPermissionOnGroup(group1, "perm3", project1);
+    db.users().insertProjectPermissionOnAnyone("perm4", project1);
+
+    underTest.delete(dbSession, "perm3", group1.getOrganizationUuid(), group1.getId(), project1.getId());
+    dbSession.commit();
+
+    assertThatNoPermission("perm3");
+    assertThat(db.countRowsOfTable("group_roles")).isEqualTo(3);
+  }
+
+  @Test
+  public void delete_project_permission_from_anybody() {
+    GroupDto group1 = db.users().insertGroup();
+    ComponentDto project1 = db.components().insertProject();
+    db.users().insertPermissionOnAnyone("perm1");
+    db.users().insertPermissionOnGroup(group1, "perm2");
+    db.users().insertProjectPermissionOnGroup(group1, "perm3", project1);
+    db.users().insertProjectPermissionOnAnyone("perm4", project1);
+
+    underTest.delete(dbSession, "perm4", group1.getOrganizationUuid(), null, project1.getId());
+    dbSession.commit();
+
+    assertThatNoPermission("perm4");
+    assertThat(db.countRowsOfTable("group_roles")).isEqualTo(3);
+  }
+
+  private void assertThatNoPermission(String permission) {
+    assertThat(db.countSql("select count(id) from group_roles where role='" + permission + "'")).isEqualTo(0);
+  }
 }
index 5b53fef921f9072c6b97d8e08f9f355a800124a3..ecdf59dd0b9658e42db4f3e3e235c83030eb7f70 100644 (file)
@@ -110,18 +110,6 @@ public class PermissionRepositoryTest {
     dbTester.assertDbUnitTable(getClass(), "apply_default_permission_template-result.xml", "user_roles", "user_id", "resource_id", "role");
   }
 
-  @Test
-  public void should_delete_group_permission() {
-    dbTester.prepareDbUnit(getClass(), "should_delete_group_permission.xml");
-
-    underTest.deleteGroupPermission(PROJECT.getId(), 100L, UserRole.USER, session);
-    session.commit();
-
-    dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
-    dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "projects", "authorization_updated_at");
-    checkAuthorizationUpdatedAtIsUpdated();
-  }
-
   @Test
   public void would_user_have_permission_with_default_permission_template() {
     UserDto user = dbTester.users().insertUser();
index e092e46465a43e104de33b82a95b209865fcc7e8..843cd398aa9c22fea05989856cf4e1bcd58a8ed3 100644 (file)
@@ -31,7 +31,6 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.permission.GroupPermissionDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
@@ -117,30 +116,6 @@ public class RoleDaoTest {
     assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-users", 1L)).containsOnly(UserRole.CODEVIEWER);
   }
 
-  @Test
-  public void delete_global_group_permission() {
-    db.prepareDbUnit(getClass(), "globalGroupPermissions.xml");
-
-    GroupPermissionDto groupRoleToDelete = new GroupPermissionDto().setGroupId(100L).setRole(GlobalPermissions.QUALITY_PROFILE_ADMIN);
-
-    underTest.deleteGroupRole(groupRoleToDelete, db.getSession());
-    db.getSession().commit();
-
-    db.assertDbUnit(getClass(), "globalGroupPermissions-result.xml", "group_roles");
-  }
-
-  @Test
-  public void delete_resource_group_permission() {
-    db.prepareDbUnit(getClass(), "resourceGroupPermissions.xml");
-
-    GroupPermissionDto groupRoleToDelete = new GroupPermissionDto().setGroupId(100L).setRole(UserRole.CODEVIEWER).setResourceId(1L);
-
-    underTest.deleteGroupRole(groupRoleToDelete, db.getSession());
-    db.getSession().commit();
-
-    db.assertDbUnit(getClass(), "resourceGroupPermissions-result.xml", "group_roles");
-  }
-
   @Test
   public void delete_all_group_permissions_by_group_id() {
     db.prepareDbUnit(getClass(), "deleteGroupPermissionsByGroupId.xml");
index a6501d831cad4c7594e59151a94b96346a24b23c..a2a75166c453b2637b4181bc0ce8871f29ff926b 100644 (file)
@@ -71,6 +71,14 @@ public class UserDbTester {
     return insertGroup(group);
   }
 
+  /**
+   * Create group in default organization
+   */
+  public GroupDto insertGroup() {
+    GroupDto group = newGroupDto().setOrganizationUuid(db.getDefaultOrganization().getUuid());
+    return insertGroup(group);
+  }
+
   public GroupDto insertGroup(GroupDto dto) {
     db.getDbClient().groupDao().insert(db.getSession(), dto);
     db.commit();
diff --git a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission-result.xml b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission-result.xml
deleted file mode 100644 (file)
index efd14a5..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-<dataset>
-
-  <groups id="100"
-          name="devs"
-          organization_uuid="org1"/>
-
-  <group_roles id="1"
-               group_id="100"
-               resource_id="123"
-               role="admin"
-               organization_uuid="org1"/>
-
-  <projects uuid="A"
-            uuid_path="NOT_USED"
-            root_uuid="A"
-            scope="PRJ"
-            qualifier="TRK"
-            kee="org.struts:struts"
-            name="Struts"
-            description="the description"
-            long_name="Apache Struts"
-            enabled="[true]"
-            language="java"
-            copy_component_uuid="[null]"
-            developer_uuid="[null]"
-            path="[null]"
-            authorization_updated_at="123456789"
-            id="123"/>
-
-</dataset>
diff --git a/sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission.xml b/sonar-db/src/test/resources/org/sonar/db/permission/PermissionRepositoryTest/should_delete_group_permission.xml
deleted file mode 100644 (file)
index a66c2de..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-<dataset>
-
-  <groups id="100"
-          name="devs"
-          organization_uuid="org1"/>
-
-  <group_roles id="1"
-               group_id="100"
-               resource_id="123"
-               role="admin"
-               organization_uuid="org1"/>
-  <group_roles id="2"
-               group_id="100"
-               resource_id="123"
-               role="user"
-               organization_uuid="org1"/>
-
-  <projects uuid="A"
-            uuid_path="NOT_USED"
-            root_uuid="A"
-            scope="PRJ"
-            qualifier="TRK"
-            kee="org.struts:struts"
-            name="Struts"
-            description="the description"
-            long_name="Apache Struts"
-            enabled="[true]"
-            language="java"
-            copy_component_uuid="[null]"
-            developer_uuid="[null]"
-            path="[null]"
-            authorization_updated_at="123456789"
-            id="123"/>
-
-</dataset>