From 451c00462f77778d5ca3972da4ca86d3ceee086f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 28 Sep 2017 14:54:02 +0200 Subject: [PATCH] Add method to select all administrators (for license notifications) --- .../sonar/db/permission/AuthorizationDao.java | 10 +++++++- .../db/permission/AuthorizationMapper.java | 8 +++---- .../db/permission/AuthorizationMapper.xml | 2 +- .../db/permission/AuthorizationDaoTest.java | 23 +++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java index 44afc088c09..d92a139a1b5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java @@ -28,6 +28,7 @@ import org.sonar.db.DbSession; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; /** @@ -166,7 +167,14 @@ public class AuthorizationDao implements Dao { } public List selectQualityProfileAdministratorLogins(DbSession dbSession) { - return mapper(dbSession).selectQualityProfileAdministratorLogins(ADMINISTER_QUALITY_PROFILES.getKey()); + return mapper(dbSession).selectLoginsWithGlobalPermission(ADMINISTER_QUALITY_PROFILES.getKey()); + } + + /** + * Used by license notifications + */ + public List selectGlobalAdministratorLogins(DbSession dbSession) { + return mapper(dbSession).selectLoginsWithGlobalPermission(ADMINISTER.getKey()); } private static AuthorizationMapper mapper(DbSession dbSession) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationMapper.java index 5c76c4ea5d4..da8ecbb5356 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationMapper.java @@ -40,15 +40,15 @@ public interface AuthorizationMapper { @Param("excludedUserId") int excludedUserId); int countUsersWithGlobalPermissionExcludingGroupMember(@Param("organizationUuid") String organizationUuid, - @Param("permission") String permission, @Param("groupId") int groupId, @Param("userId") int userId); + @Param("permission") String permission, @Param("groupId") int groupId, @Param("userId") int userId); int countUsersWithGlobalPermissionExcludingUserPermission(@Param("organizationUuid") String organizationUuid, - @Param("permission") String permission, @Param("userId") int userId); + @Param("permission") String permission, @Param("userId") int userId); Set selectOrganizationUuidsOfUserWithGlobalPermission(@Param("userId") int userId, @Param("permission") String permission); Set keepAuthorizedProjectIdsForAnonymous(@Param("role") String role, @Param("componentIds") Collection componentIds); - + Set keepAuthorizedProjectIdsForUser(@Param("userId") int userId, @Param("role") String role, @Param("componentIds") Collection componentIds); List keepAuthorizedUsersForRoleAndProject(@Param("role") String role, @Param("componentId") long componentId, @Param("userIds") List userIds); @@ -61,5 +61,5 @@ public interface AuthorizationMapper { Set selectProjectPermissionsOfAnonymous(@Param("projectUuid") String projectUuid); - List selectQualityProfileAdministratorLogins(@Param("permission") String permission); + List selectLoginsWithGlobalPermission(@Param("permission") String permission); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/AuthorizationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/AuthorizationMapper.xml index 52248d4b65f..072fefe4e3d 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/AuthorizationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/AuthorizationMapper.xml @@ -379,7 +379,7 @@ and gr.group_id is null - select u.login from user_roles ur inner join users u on u.id=ur.user_id diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/AuthorizationDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/AuthorizationDaoTest.java index c3a099c4f8a..36595bc5d72 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/AuthorizationDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/AuthorizationDaoTest.java @@ -1005,4 +1005,27 @@ public class AuthorizationDaoTest { assertThat(logins).isEmpty(); } + @Test + public void selectGlobalAdministratorLogins() { + OrganizationDto organization1 = db.organizations().insert(); + UserDto user1 = db.users().insertUser(); + db.users().insertPermissionOnUser(organization1, user1, ADMINISTER); + OrganizationDto organization2 = db.organizations().insert(); + UserDto user2 = db.users().insertUser(); + db.users().insertPermissionOnUser(organization2, user2, ADMINISTER); + + GroupDto administratorGroup2 = db.users().insertGroup(organization2); + db.users().insertPermissionOnGroup(administratorGroup2, ADMINISTER); + UserDto user3 = db.users().insertUser(); + db.users().insertMember(administratorGroup2, user3); + + UserDto user4 = db.users().insertUser(); + db.users().insertPermissionOnUser(organization1, user4, ADMINISTER_QUALITY_PROFILES); + db.users().insertUser(); + + List logins = underTest.selectGlobalAdministratorLogins(dbSession); + + assertThat(logins).containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin(), user3.getLogin()); + } + } -- 2.39.5