]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4412 revert check of last remaining admin user
authorSimon Brandhof <simon.brandhof@gmail.com>
Sat, 29 Jun 2013 17:07:40 +0000 (19:07 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sat, 29 Jun 2013 17:07:40 +0000 (19:07 +0200)
sonar-core/src/main/java/org/sonar/core/user/RoleDao.java
sonar-core/src/main/java/org/sonar/core/user/RoleMapper.java
sonar-core/src/main/resources/org/sonar/core/user/RoleMapper.xml
sonar-core/src/test/java/org/sonar/core/user/RoleDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/RoleDaoTest/systemAdminsCount.xml [deleted file]
sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java

index 74884a4179426c3658d0d305201b2bef760f92c8..0b275b9662c5de1e151e3be5920e36e60292c4a2 100644 (file)
@@ -145,14 +145,4 @@ public class RoleDao implements TaskExtension, ServerExtension {
       MyBatis.closeQuietly(session);
     }
   }
-
-  public int countSystemAdministrators(@Nullable String groupName) {
-    SqlSession session = mybatis.openSession();
-    try {
-      RoleMapper mapper = session.getMapper(RoleMapper.class);
-      return mapper.countSystemAdministrators(groupName);
-    } finally {
-      MyBatis.closeQuietly(session);
-    }
-  }
 }
index da7b6c422efcf55219b19fe763af486370898b20..0fcaac7973bfe20ef333baa57bedc7486492fa38 100644 (file)
@@ -49,5 +49,5 @@ public interface RoleMapper {
 
   int countUserRoles(Long resourceId);
 
-  int countSystemAdministrators(@Nullable @Param("groupName") String groupName);
+  List<Long> countSystemAdministrators(@Nullable @Param("groupName") String groupName);
 }
index 1a267371a9b30dc9a0d3bda3b800a687fef6f87a..3dba348d55a23be25e0b89ecff4e5430f0c8d026 100644 (file)
     SELECT count(id)
     FROM group_roles WHERE resource_id=#{id}
   </select>
-
-  <select id="countSystemAdministrators" parameterType="String" resultType="int">
-    SELECT COUNT(DISTINCT u.id)
-    FROM users AS u
-    LEFT JOIN user_roles AS ur ON ur.user_id = u.id
-    INNER JOIN groups_users AS gu ON gu.user_id = u.id
-    INNER JOIN group_roles AS gr ON gr.group_id = gu.group_id
-    INNER JOIN groups AS g ON g.id = gu.group_id
-    WHERE (ur.role = 'admin' AND ur.resource_id IS NULL) OR (gr.role = 'admin' AND gr.resource_id IS NULL)
-    AND u.active = ${_true}
-    <if test="groupName != null">
-      AND g.name != #{groupName}
-    </if>
-  </select>
 </mapper>
index 127ba6ab6547897b26f49ad1d326f2720cd2e470..33eae64cf56474da3824e011c6a7e674c1fd3551 100644 (file)
@@ -75,17 +75,4 @@ public class RoleDaoTest extends AbstractDaoTestCase {
     checkTable("groupPermissions", "group_roles", "group_id", "role");
   }
 
-  @Test
-  public void should_retrieve_system_admins_count() throws Exception {
-    setupData("systemAdminsCount");
-
-    RoleDao dao = new RoleDao(getMyBatis());
-    int overallAdminsCount = dao.countSystemAdministrators(null);
-    int adminsCountAfterWholeGroupRemoval = dao.countSystemAdministrators("sonar-administrators");
-    int adminsCountAfterNonAdminGroupRemoval = dao.countSystemAdministrators("sonar-users");
-
-    assertThat(overallAdminsCount).isEqualTo(3);
-    assertThat(adminsCountAfterWholeGroupRemoval).isEqualTo(1);
-    assertThat(adminsCountAfterNonAdminGroupRemoval).isEqualTo(3);
-  }
 }
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleDaoTest/systemAdminsCount.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleDaoTest/systemAdminsCount.xml
deleted file mode 100644 (file)
index f39cc93..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<dataset>
-
-  <users id="200" login="admin" name="admin" active="[true]"/>
-  <users id="201" login="user_admin" name="user_admin" active="[true]"/>
-  <users id="202" login="user_in_admin_group" name="user_in_admin_group" active="[true]"/>
-  <users id="203" login="non_admin" name="non_admin" active="[true]"/>
-
-  <user_roles id="1" user_id="201" role="admin"/>
-
-  <groups_users group_id="100" user_id="200"/>
-  <groups_users group_id="100" user_id="202"/>
-  <groups_users group_id="101" user_id="201"/>
-  <groups_users group_id="101" user_id="203"/>
-
-  <groups id="100" name="sonar-administrators"/>
-  <groups id="101" name="sonar-users"/>
-
-  <group_roles id="1" group_id="100" role="admin"/>
-  <group_roles id="2" group_id="100" role="profileadmin"/>
-  <group_roles id="3" group_id="100" role="sharedashboard"/>
-  <group_roles id="4" group_id="101" role="sharedashboard"/>
-
-</dataset>
\ No newline at end of file
index 78e9a12393c0c97c1d10f0c21ae6ee4281dcb0b1..e2973b61a4752202b9d39546420ab2cbb7aa3044 100644 (file)
@@ -61,9 +61,6 @@ public class InternalPermissionService implements ServerComponent {
     UserSession.get().checkPermission(Permissions.SYSTEM_ADMIN);
     PermissionChangeQuery permissionChangeQuery = PermissionChangeQuery.buildFromParams(params);
     if(permissionChangeQuery.isValid()) {
-      if(Permissions.SYSTEM_ADMIN.equals(permissionChangeQuery.getRole()) && REMOVE.equals(permissionChange)) {
-        checkThatAtLeastOneAdminRemains(permissionChangeQuery);
-      }
       applyPermissionChange(permissionChange, permissionChangeQuery);
     } else {
       String errorMsg = String.format("Request '%s permission %s' is invalid", permissionChange, permissionChangeQuery.getRole());
@@ -127,14 +124,4 @@ public class InternalPermissionService implements ServerComponent {
     return (ADD.equals(operation) && existingPermissions.contains(role)) ||
       (REMOVE.equals(operation) && !existingPermissions.contains(role));
   }
-
-  private void checkThatAtLeastOneAdminRemains(PermissionChangeQuery permissionChangeQuery) {
-    int remainingSystemAdmins = roleDao.countSystemAdministrators(permissionChangeQuery.getGroup());
-    if(remainingSystemAdmins == 0) {
-      String errorMsg = String.format("Cannot remove permission %s to %s - At least one system administrator should remain active",
-        permissionChangeQuery.getRole(), permissionChangeQuery.getUser() == null ? permissionChangeQuery.getGroup() : permissionChangeQuery.getUser());
-      LOG.error(errorMsg);
-      throw new BadRequestException(errorMsg);
-    }
-  }
 }
index d36d539e38c7d2738b8c3dde12e0d15d597c5215..34e3966bc31fd1ff73c6663cba7a6bb9b8ff4c48 100644 (file)
@@ -138,32 +138,6 @@ public class InternalPermissionServiceTest {
     service.addPermission(params);
   }
 
-  @Test
-  public void should_prevent_last_admin_removal() throws Exception {
-    throwable.expect(BadRequestException.class);
-    params = buildParams("admin", null, Permissions.SYSTEM_ADMIN);
-    when(roleDao.countSystemAdministrators(null)).thenReturn(0);
-
-    service.removePermission(params);
-  }
-
-  @Test
-  public void should_prevent_last_admin_group_removal() throws Exception {
-    throwable.expect(BadRequestException.class);
-    params = buildParams(null, "sonar-administrators", Permissions.SYSTEM_ADMIN);
-    GroupDto adminGroups = new GroupDto().setId(2L).setName("sonar-administrators");
-
-    roleDao = mock(RoleDao.class);
-    when(roleDao.selectGroupPermissions("sonar-administrators")).thenReturn(Lists.newArrayList(Permissions.SYSTEM_ADMIN));
-    when(roleDao.countSystemAdministrators("sonar-administrators")).thenReturn(0);
-
-    userDao = mock(UserDao.class);
-    when(userDao.selectGroupByName("sonar-administrators")).thenReturn(adminGroups);
-
-    service = new InternalPermissionService(roleDao, userDao);
-    service.removePermission(params);
-  }
-
   @Test
   public void should_fail_on_anonymous_access() throws Exception {
     throwable.expect(ForbiddenException.class);