From 7ea90bda7b5dd034cd5669bf68c1aec3b118a8ca Mon Sep 17 00:00:00 2001 From: Jacek Date: Mon, 4 May 2020 14:53:38 +0200 Subject: [PATCH] SONAR-13221 change USER_ROLES user_id FK to user_uuid --- .../db/permission/UserPermissionDao.java | 29 ++-- .../db/permission/UserPermissionDto.java | 12 +- .../db/permission/UserPermissionMapper.java | 14 +- .../db/organization/OrganizationMapper.xml | 3 +- .../db/permission/AuthorizationMapper.xml | 40 ++--- .../db/permission/UserPermissionMapper.xml | 32 ++-- .../org/sonar/db/user/RoleMapper.xml | 3 +- server/sonar-db-dao/src/schema/schema-sq.ddl | 6 +- .../db/permission/UserPermissionDaoTest.java | 110 ++++++------ .../java/org/sonar/db/user/UserDbTester.java | 12 +- .../db/migration/version/v83/DbVersion83.java | 29 +++- .../AddIndexOnUserUuidOfUserRolesTable.java | 60 +++++++ .../AddUserUuidColumnToUserRoles.java | 30 ++++ .../DropIndexOnUserIdOfUserRolesTable.java | 52 ++++++ .../DropUserIdColumnOfUserRolesTable.java | 36 ++++ .../userroles/PopulateUserRolesUserUuid.java | 52 ++++++ ...ddIndexOnUserUuidOfUserRolesTableTest.java | 49 ++++++ .../AddUserUuidColumnToUserRolesTest.java | 64 +++++++ ...DropIndexOnUserIdOfUserRolesTableTest.java | 52 ++++++ .../DropUserIdColumnOfUserRolesTableTest.java | 51 ++++++ .../PopulateUserRolesUserUuidTest.java | 145 ++++++++++++++++ .../schema.sql | 10 ++ .../schema.sql | 10 ++ .../schema.sql | 11 ++ .../schema.sql | 11 ++ .../PopulateUserRolesUserUuidTest/schema.sql | 42 +++++ .../es/newindex/NewAuthorizedIndex.java | 2 +- .../permission/index/AuthorizationDoc.java | 6 +- .../permission/index/IndexPermissions.java | 10 +- .../es/newindex/NewAuthorizedIndexTest.java | 21 +-- .../index/AuthorizationDocTest.java | 12 +- .../server/organization/MemberUpdater.java | 3 +- .../organization/MemberUpdaterTest.java | 4 +- .../index/PermissionIndexerDao.java | 12 +- .../index/WebAuthorizationTypeSupport.java | 6 +- .../index/ProjectMeasuresIndexTest.java | 2 +- .../index/PermissionIndexerDaoTest.java | 18 +- .../index/PermissionIndexerTest.java | 2 +- .../WebAuthorizationTypeSupportTest.java | 158 +++++++++--------- .../index/PermissionIndexerTester.java | 2 +- .../permission/PermissionTemplateService.java | 6 +- .../permission/UserPermissionChanger.java | 11 +- .../server/permission/ws/UsersAction.java | 6 +- .../project/ws/UpdateVisibilityAction.java | 8 +- .../server/user/ws/DeactivateAction.java | 3 +- .../PermissionTemplateServiceTest.java | 2 +- .../ws/UpdateVisibilityActionTest.java | 28 ++-- .../server/user/ws/DeactivateActionTest.java | 4 +- 48 files changed, 980 insertions(+), 311 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRoles.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuid.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest/schema.sql diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java index 7dab1f11eb3..274edbc24a5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java @@ -58,7 +58,6 @@ public class UserPermissionDao implements Dao { return paginate(mapper(dbSession).selectUserIdsByQueryAndScope(query), query); } - private static List paginate(List results, PermissionQuery query) { return results .stream() @@ -86,8 +85,8 @@ public class UserPermissionDao implements Dao { * * @return the global permissions. An empty list is returned if user or organization do not exist. */ - public List selectGlobalPermissionsOfUser(DbSession dbSession, int userId, String organizationUuid) { - return mapper(dbSession).selectGlobalPermissionsOfUser(userId, organizationUuid); + public List selectGlobalPermissionsOfUser(DbSession dbSession, String userUuid, String organizationUuid) { + return mapper(dbSession).selectGlobalPermissionsOfUser(userUuid, organizationUuid); } /** @@ -95,12 +94,12 @@ public class UserPermissionDao implements Dao { * * @return the project permissions. An empty list is returned if project or user do not exist. */ - public List selectProjectPermissionsOfUser(DbSession dbSession, int userId, String projectUuid) { - return mapper(dbSession).selectProjectPermissionsOfUser(userId, projectUuid); + public List selectProjectPermissionsOfUser(DbSession dbSession, String userUuid, String projectUuid) { + return mapper(dbSession).selectProjectPermissionsOfUser(userUuid, projectUuid); } - public Set selectUserIdsWithPermissionOnProjectBut(DbSession session, String projectUuid, String permission) { - return mapper(session).selectUserIdsWithPermissionOnProjectBut(projectUuid, permission); + public Set selectUserUuidsWithPermissionOnProjectBut(DbSession session, String projectUuid, String permission) { + return mapper(session).selectUserUuidsWithPermissionOnProjectBut(projectUuid, permission); } public void insert(DbSession dbSession, UserPermissionDto dto) { @@ -122,15 +121,15 @@ public class UserPermissionDao implements Dao { /** * Removes a single global permission from user */ - public void deleteGlobalPermission(DbSession dbSession, int userId, String permission, String organizationUuid) { - mapper(dbSession).deleteGlobalPermission(userId, permission, organizationUuid); + public void deleteGlobalPermission(DbSession dbSession, String userUuid, String permission, String organizationUuid) { + mapper(dbSession).deleteGlobalPermission(userUuid, permission, organizationUuid); } /** * Removes a single project permission from user */ - public void deleteProjectPermission(DbSession dbSession, int userId, String permission, String projectUuid) { - mapper(dbSession).deleteProjectPermission(userId, permission, projectUuid); + public void deleteProjectPermission(DbSession dbSession, String userUuid, String permission, String projectUuid) { + mapper(dbSession).deleteProjectPermission(userUuid, permission, projectUuid); } /** @@ -151,12 +150,12 @@ public class UserPermissionDao implements Dao { mapper(dbSession).deleteByOrganization(organizationUuid); } - public void deleteOrganizationMemberPermissions(DbSession dbSession, String organizationUuid, int userId) { - mapper(dbSession).deleteOrganizationMemberPermissions(organizationUuid, userId); + public void deleteOrganizationMemberPermissions(DbSession dbSession, String organizationUuid, String userUuid) { + mapper(dbSession).deleteOrganizationMemberPermissions(organizationUuid, userUuid); } - public void deleteByUserId(DbSession dbSession, int userId) { - mapper(dbSession).deleteByUserId(userId); + public void deleteByUserUuid(DbSession dbSession, String userUuid) { + mapper(dbSession).deleteByUserUuid(userUuid); } private static UserPermissionMapper mapper(DbSession dbSession) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDto.java index 158f7428ea3..232006f744e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDto.java @@ -26,18 +26,18 @@ public class UserPermissionDto { private String uuid; private String organizationUuid; private String permission; - private int userId; + private String userUuid; private String componentUuid; public UserPermissionDto() { // used by MyBatis } - public UserPermissionDto(String uuid, String organizationUuid, String permission, int userId, @Nullable String componentUuid) { + public UserPermissionDto(String uuid, String organizationUuid, String permission, String userUuid, @Nullable String componentUuid) { this.uuid = uuid; this.organizationUuid = organizationUuid; this.permission = permission; - this.userId = userId; + this.userUuid = userUuid; this.componentUuid = componentUuid; } @@ -49,8 +49,8 @@ public class UserPermissionDto { return permission; } - public int getUserId() { - return userId; + public String getUserUuid() { + return userUuid; } public String getOrganizationUuid() { @@ -69,7 +69,7 @@ public class UserPermissionDto { public String toString() { StringBuilder sb = new StringBuilder("UserPermissionDto{"); sb.append("permission='").append(permission).append('\''); - sb.append(", userId=").append(userId); + sb.append(", userUuid=").append(userUuid); sb.append(", organizationUuid=").append(organizationUuid); sb.append(", componentUuid=").append(componentUuid); sb.append('}'); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java index 4318a4d0403..648b6066ade 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java @@ -50,27 +50,27 @@ public interface UserPermissionMapper { /** * select id of users with at least one permission on the specified project but which do not have the specified permission. */ - Set selectUserIdsWithPermissionOnProjectBut(@Param("projectUuid") String projectUuid, @Param("permission") String permission); + Set selectUserUuidsWithPermissionOnProjectBut(@Param("projectUuid") String projectUuid, @Param("permission") String permission); void insert(UserPermissionDto dto); - void deleteGlobalPermission(@Param("userId") int userId, @Param("permission") String permission, + void deleteGlobalPermission(@Param("userUuid") String userUuid, @Param("permission") String permission, @Param("organizationUuid") String organizationUuid); - void deleteProjectPermission(@Param("userId") int userId, @Param("permission") String permission, + void deleteProjectPermission(@Param("userUuid") String userUuid, @Param("permission") String permission, @Param("projectUuid") String projectUuid); void deleteProjectPermissions(@Param("projectUuid") String projectUuid); int deleteProjectPermissionOfAnyUser(@Param("projectUuid") String projectUuid, @Param("permission") String permission); - List selectGlobalPermissionsOfUser(@Param("userId") int userId, @Param("organizationUuid") String organizationUuid); + List selectGlobalPermissionsOfUser(@Param("userUuid") String userUuid, @Param("organizationUuid") String organizationUuid); - List selectProjectPermissionsOfUser(@Param("userId") int userId, @Param("projectUuid") String projectUuid); + List selectProjectPermissionsOfUser(@Param("userUuid") String userUuid, @Param("projectUuid") String projectUuid); void deleteByOrganization(@Param("organizationUuid") String organizationUuid); - void deleteOrganizationMemberPermissions(@Param("organizationUuid") String organizationUuid, @Param("userId") int login); + void deleteOrganizationMemberPermissions(@Param("organizationUuid") String organizationUuid, @Param("userUuid") String userUuid); - void deleteByUserId(@Param("userId") int userId); + void deleteByUserUuid(@Param("userUuid") String userUuid); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index 8519e69c6ec..b7d9744bf94 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -150,8 +150,7 @@ from organizations org inner join user_roles ur on ur.organization_uuid = org.uuid - inner join users u on ur.user_id = u.id - and u.uuid = #{userUuid,jdbcType=VARCHAR} + and ur.user_uuid = #{userUuid,jdbcType=VARCHAR} and ur.component_uuid is null and ur.role = #{permission,jdbcType=VARCHAR} union 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 7cbb245c94a..01b030d294a 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 @@ -25,10 +25,9 @@ select ur.role from user_roles ur - inner join users u on ur.user_id=u.id where ur.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} and - u.uuid=#{userUuid, jdbcType=VARCHAR} + ur.user_uuid=#{userUuid, jdbcType=VARCHAR} and ur.component_uuid is null @@ -56,9 +55,8 @@ union - select u.uuid + select ur.user_uuid from user_roles ur - inner join users u on ur.user_id = u.id where ur.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ur.component_uuid is null and @@ -81,14 +79,13 @@ union - select u.uuid + select ur.user_uuid from user_roles ur - inner join users u on ur.user_id = u.id where ur.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ur.component_uuid is null and ur.role = #{permission, jdbcType=VARCHAR} and - u.uuid != #{excludedUserUuid, jdbcType=VARCHAR} + ur.user_uuid != #{excludedUserUuid, jdbcType=VARCHAR} ) remaining @@ -104,9 +101,8 @@ union - select u.uuid + select ur.user_uuid from user_roles ur - inner join users u on ur.user_id = u.id where ur.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ur.component_uuid is null and @@ -128,9 +124,8 @@ union - select u.uuid + select ur.user_uuid from user_roles ur - inner join users u on ur.user_id = u.id where ur.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ur.component_uuid is null and @@ -152,14 +147,13 @@ union - select u.uuid + select ur.user_uuid from user_roles ur - inner join users u on ur.user_id=u.id where ur.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and ur.component_uuid is null and ur.role = #{permission, jdbcType=VARCHAR} and - u.uuid != #{userUuid, jdbcType=VARCHAR} + ur.user_uuid != #{userUuid, jdbcType=VARCHAR} ) remaining @@ -177,11 +171,10 @@ select ur.organization_uuid from user_roles ur - inner join users u on ur.user_id=u.id where ur.component_uuid is null and ur.role = #{permission, jdbcType=VARCHAR} and - u.uuid = #{userUuid, jdbcType=VARCHAR} + ur.user_uuid = #{userUuid, jdbcType=VARCHAR} select - u.id as userId, + u.uuid as userUuid, ur.organization_uuid as organizationUuid, ur.component_uuid as componentUuid, ur.role as permission @@ -31,7 +31,7 @@ select u.id, lower(u.name) from users u - left join user_roles ur on ur.user_id = u.id and ur.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR} + left join user_roles ur on ur.user_uuid = u.uuid and ur.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR} and ur.component_uuid is null @@ -59,7 +59,7 @@ from users u - left join user_roles ur on ur.user_id = u.id + left join user_roles ur on ur.user_uuid = u.uuid left join components p on ur.component_uuid = p.uuid inner join organization_members om on u.uuid=om.user_uuid and om.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR} @@ -93,7 +93,7 @@ from user_roles ur where ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and - ur.user_id = #{userId,jdbcType=INTEGER} and + ur.user_uuid = #{userUuid,jdbcType=VARCHAR} and ur.component_uuid is null @@ -101,23 +101,23 @@ select ur.role from user_roles ur where - ur.user_id = #{userId,jdbcType=INTEGER} and + ur.user_uuid = #{userUuid,jdbcType=VARCHAR} and ur.component_uuid = #{projectUuid,jdbcType=VARCHAR} - select - distinct ur1.user_id + distinct ur1.user_uuid from user_roles ur1 where @@ -130,7 +130,7 @@ user_roles ur2 where ur2.component_uuid = ur1.component_uuid - and ur2.user_id = ur1.user_id + and ur2.user_uuid = ur1.user_uuid and role = #{permission,jdbcType=VARCHAR} ) @@ -139,13 +139,13 @@ insert into user_roles ( uuid, organization_uuid, - user_id, + user_uuid, component_uuid, role ) values ( #{uuid,jdbcType=VARCHAR}, #{organizationUuid,jdbcType=VARCHAR}, - #{userId,jdbcType=INTEGER}, + #{userUuid,jdbcType=VARCHAR}, #{componentUuid,jdbcType=VARCHAR}, #{permission,jdbcType=VARCHAR} ) @@ -155,7 +155,7 @@ delete from user_roles where role = #{permission,jdbcType=VARCHAR} and - user_id = #{userId,jdbcType=INTEGER} and + user_uuid = #{userUuid,jdbcType=VARCHAR} and organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and component_uuid is null @@ -164,7 +164,7 @@ delete from user_roles where role = #{permission,jdbcType=VARCHAR} and - user_id = #{userId,jdbcType=INTEGER} and + user_uuid = #{userUuid,jdbcType=VARCHAR} and component_uuid = #{projectUuid,jdbcType=VARCHAR} @@ -194,10 +194,10 @@ user_roles where organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and - user_id = #{userId,jdbcType=INTEGER} + user_uuid = #{userUuid,jdbcType=VARCHAR} - - DELETE FROM user_roles WHERE user_id=#{userId,jdbcType=INTEGER} + + DELETE FROM user_roles WHERE user_uuid=#{userUuid,jdbcType=VARCHAR} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/RoleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/RoleMapper.xml index 1341f617cb5..f40dd2ab25b 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/RoleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/RoleMapper.xml @@ -8,9 +8,8 @@ ur.component_uuid from user_roles ur - inner join users u on ur.user_id = u.id where - u.uuid = #{userUuid,jdbcType=VARCHAR} + ur.user_uuid = #{userUuid,jdbcType=VARCHAR} and ur.role = #{permission,jdbcType=VARCHAR} and ur.component_uuid is not null union diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 129fc3b2f18..3dd4eac0c93 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -915,14 +915,14 @@ CREATE UNIQUE INDEX "USER_PROPERTIES_USER_UUID_KEE" ON "USER_PROPERTIES"("USER_U CREATE TABLE "USER_ROLES"( "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, - "USER_ID" INTEGER, "ROLE" VARCHAR(64) NOT NULL, "COMPONENT_UUID" VARCHAR(40), - "UUID" VARCHAR(40) NOT NULL + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(40) ); ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); -CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_ID"); CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_UUID"); CREATE TABLE "USER_TOKENS"( "USER_UUID" VARCHAR(255) NOT NULL, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index 77cf00ff9c1..39f6cc386b8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -397,23 +397,23 @@ public class UserPermissionDaoTest { addProjectPermission(organization, "perm4", user2, project2); // user2 does not have global permissions -> do nothing - underTest.deleteGlobalPermission(dbSession, user2.getId(), "perm1", db.getDefaultOrganization().getUuid()); + underTest.deleteGlobalPermission(dbSession, user2.getUuid(), "perm1", db.getDefaultOrganization().getUuid()); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission is not granted -> do nothing - underTest.deleteGlobalPermission(dbSession, user1.getId(), "notGranted", db.getDefaultOrganization().getUuid()); + underTest.deleteGlobalPermission(dbSession, user1.getUuid(), "notGranted", db.getDefaultOrganization().getUuid()); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // permission is on project -> do nothing - underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm3", db.getDefaultOrganization().getUuid()); + underTest.deleteGlobalPermission(dbSession, user1.getUuid(), "perm3", db.getDefaultOrganization().getUuid()); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission on another organization-> do nothing - underTest.deleteGlobalPermission(dbSession, user1.getId(), "notGranted", "anotherOrg"); + underTest.deleteGlobalPermission(dbSession, user1.getUuid(), "notGranted", "anotherOrg"); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(5); // global permission exists -> delete it, but not the project permission with the same name ! - underTest.deleteGlobalPermission(dbSession, user1.getId(), "perm1", organization.getUuid()); + underTest.deleteGlobalPermission(dbSession, user1.getUuid(), "perm1", organization.getUuid()); assertThat(db.countSql(dbSession, "select count(uuid) from user_roles where role='perm1' and component_uuid is null")).isEqualTo(0); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); } @@ -431,10 +431,10 @@ public class UserPermissionDaoTest { addProjectPermission(organization, "perm", user2, project1); // no such provision -> ignore - underTest.deleteProjectPermission(dbSession, user1.getId(), "anotherPerm", project1.uuid()); + underTest.deleteProjectPermission(dbSession, user1.getUuid(), "anotherPerm", project1.uuid()); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(4); - underTest.deleteProjectPermission(dbSession, user1.getId(), "perm", project1.uuid()); + underTest.deleteProjectPermission(dbSession, user1.getUuid(), "perm", project1.uuid()); assertThatProjectPermissionDoesNotExist(user1, "perm", project1); assertThat(db.countRowsOfTable(dbSession, "user_roles")).isEqualTo(3); } @@ -470,10 +470,10 @@ public class UserPermissionDaoTest { addProjectPermission(organization, "perm4", user1, project); addProjectPermission(organization, "perm5", user1, project); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), org.getUuid())).containsOnly("perm3"); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), db.getDefaultOrganization().getUuid())).containsOnly("perm1"); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), "otherOrg")).isEmpty(); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user3.getId(), org.getUuid())).isEmpty(); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid(), org.getUuid())).containsOnly("perm3"); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid(), db.getDefaultOrganization().getUuid())).containsOnly("perm1"); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid(), "otherOrg")).isEmpty(); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user3.getUuid(), org.getUuid())).isEmpty(); } @Test @@ -490,9 +490,9 @@ public class UserPermissionDaoTest { addProjectPermission(organization, "perm4", user1, project2); addProjectPermission(organization, "perm5", user2, project1); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.uuid())).containsOnly("perm2", "perm3"); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.uuid())).containsOnly("perm4"); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project3.uuid())).isEmpty(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project1.uuid())).containsOnly("perm2", "perm3"); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project2.uuid())).containsOnly("perm4"); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project3.uuid())).isEmpty(); } @Test @@ -502,7 +502,7 @@ public class UserPermissionDaoTest { UserDto user = insertUser(organization); db.users().insertProjectPermissionOnUser(user, "foo", project); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, "1234", UserRole.USER)) + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, "1234", UserRole.USER)) .isEmpty(); } @@ -515,12 +515,12 @@ public class UserPermissionDaoTest { db.users().insertProjectPermissionOnUser(user1, "p1", project); db.users().insertProjectPermissionOnUser(user2, "p2", project); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.uuid(), "p2")) - .containsOnly(user1.getId()); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.uuid(), "p1")) - .containsOnly(user2.getId()); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.uuid(), "p3")) - .containsOnly(user1.getId(), user2.getId()); + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, project.uuid(), "p2")) + .containsOnly(user1.getUuid()); + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, project.uuid(), "p1")) + .containsOnly(user2.getUuid()); + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, project.uuid(), "p3")) + .containsOnly(user1.getUuid(), user2.getUuid()); } @Test @@ -532,10 +532,10 @@ public class UserPermissionDaoTest { db.users().insertProjectPermissionOnUser(user1, "p1", project); db.users().insertProjectPermissionOnUser(user2, "p2", project); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.uuid(), "p2")) - .containsOnly(user1.getId()); - assertThat(underTest.selectUserIdsWithPermissionOnProjectBut(dbSession, project.uuid(), "p1")) - .containsOnly(user2.getId()); + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, project.uuid(), "p2")) + .containsOnly(user1.getUuid()); + assertThat(underTest.selectUserUuidsWithPermissionOnProjectBut(dbSession, project.uuid(), "p1")) + .containsOnly(user2.getUuid()); } @Test @@ -599,7 +599,7 @@ public class UserPermissionDaoTest { db.users().insertPermissionOnUser(organization1, user2, SCAN); db.users().insertProjectPermissionOnUser(user2, UserRole.CODEVIEWER, project); - underTest.deleteOrganizationMemberPermissions(dbSession, organization1.getUuid(), user1.getId()); + underTest.deleteOrganizationMemberPermissions(dbSession, organization1.getUuid(), user1.getUuid()); dbSession.commit(); // user 1 permissions @@ -623,12 +623,12 @@ public class UserPermissionDaoTest { db.users().insertPermissionOnUser(user2, SCAN); db.users().insertProjectPermissionOnUser(user2, ADMINISTER_QUALITY_GATES.getKey(), project); - underTest.deleteByUserId(dbSession, user1.getId()); + underTest.deleteByUserUuid(dbSession, user1.getUuid()); dbSession.commit(); - assertThat(db.select("select user_id as \"userId\", component_uuid as \"projectUuid\", role as \"permission\" from user_roles")) - .extracting((row) -> row.get("userId"), (row) -> row.get("projectUuid"), (row) -> row.get("permission")) - .containsOnly(tuple(user2.getId().longValue(), null, SCAN.getKey()), tuple(user2.getId().longValue(), project.uuid(), ADMINISTER_QUALITY_GATES.getKey())); + assertThat(db.select("select user_uuid as \"userUuid\", component_uuid as \"projectUuid\", role as \"permission\" from user_roles")) + .extracting((row) -> row.get("userUuid"), (row) -> row.get("projectUuid"), (row) -> row.get("permission")) + .containsOnly(tuple(user2.getUuid(), null, SCAN.getKey()), tuple(user2.getUuid(), project.uuid(), ADMINISTER_QUALITY_GATES.getKey())); } @Test @@ -640,7 +640,7 @@ public class UserPermissionDaoTest { int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, "124", SCAN.getKey()); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); } @Test @@ -653,7 +653,7 @@ public class UserPermissionDaoTest { int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), SCAN.getKey()); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); } @Test @@ -667,8 +667,8 @@ public class UserPermissionDaoTest { int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project.uuid(), "p1"); assertThat(deletedCount).isEqualTo(0); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user.getId(), project.uuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user.getUuid(), project.uuid())).containsOnly(SCAN.getKey()); } @Test @@ -689,22 +689,22 @@ public class UserPermissionDaoTest { int deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project1.uuid(), SCAN.getKey()); assertThat(deletedCount).isEqualTo(2); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.uuid())).isEmpty(); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project1.uuid())).isEmpty(); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.uuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project2.uuid())).containsOnly(SCAN.getKey(), PROVISION_PROJECTS.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project1.uuid())).isEmpty(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project1.uuid())).isEmpty(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project2.uuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project2.uuid())).containsOnly(SCAN.getKey(), PROVISION_PROJECTS.getKey()); deletedCount = underTest.deleteProjectPermissionOfAnyUser(dbSession, project2.uuid(), SCAN.getKey()); assertThat(deletedCount).isEqualTo(2); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getId(), organization.getUuid())).containsOnly(SCAN.getKey()); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project1.uuid())).isEmpty(); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project1.uuid())).isEmpty(); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getId(), project2.uuid())).containsOnly(); - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getId(), project2.uuid())).containsOnly(PROVISION_PROJECTS.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user1.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user2.getUuid(), organization.getUuid())).containsOnly(SCAN.getKey()); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project1.uuid())).isEmpty(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project1.uuid())).isEmpty(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project2.uuid())).containsOnly(); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project2.uuid())).containsOnly(PROVISION_PROJECTS.getKey()); } private ComponentDto randomPublicOrPrivateProject(OrganizationDto organization) { @@ -746,35 +746,35 @@ public class UserPermissionDaoTest { List currentPermissions = underTest.selectUserPermissionsByQuery(dbSession, query, expectedUserIds); assertThat(currentPermissions).hasSize(expectedPermissions.length); Tuple[] expectedPermissionsAsTuple = Arrays.stream(expectedPermissions) - .map(expectedPermission -> tuple(expectedPermission.getUserId(), expectedPermission.getPermission(), expectedPermission.getComponentUuid(), + .map(expectedPermission -> tuple(expectedPermission.getUserUuid(), expectedPermission.getPermission(), expectedPermission.getComponentUuid(), expectedPermission.getOrganizationUuid())) .toArray(Tuple[]::new); assertThat(currentPermissions) - .extracting(UserPermissionDto::getUserId, UserPermissionDto::getPermission, UserPermissionDto::getComponentUuid, UserPermissionDto::getOrganizationUuid) + .extracting(UserPermissionDto::getUserUuid, UserPermissionDto::getPermission, UserPermissionDto::getComponentUuid, UserPermissionDto::getOrganizationUuid) .containsOnly(expectedPermissionsAsTuple); // test method "countUsers()" - long distinctUsers = stream(expectedPermissions).mapToLong(UserPermissionDto::getUserId).distinct().count(); + long distinctUsers = stream(expectedPermissions).map(UserPermissionDto::getUserUuid).distinct().count(); assertThat((long) underTest.countUsersByQuery(dbSession, query)).isEqualTo(distinctUsers); } private UserPermissionDto addGlobalPermission(OrganizationDto org, String permission, UserDto user) { - UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getId(), null); + UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getUuid(), null); underTest.insert(dbSession, dto); db.commit(); return dto; } private UserPermissionDto addProjectPermission(OrganizationDto org, String permission, UserDto user, ComponentDto project) { - UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getId(), project.uuid()); + UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getUuid(), project.uuid()); underTest.insert(dbSession, dto); db.commit(); return dto; } private void assertThatProjectPermissionDoesNotExist(UserDto user, String permission, ComponentDto project) { - assertThat(db.countSql(dbSession, "select count(uuid) from user_roles where role='" + permission + "' and user_id=" + user.getId() - + " and component_uuid='" + project.uuid() + "'")) + assertThat(db.countSql(dbSession, "select count(uuid) from user_roles where role='" + permission + "' and user_uuid='" + user.getUuid() + + "' and component_uuid='" + project.uuid() + "'")) .isEqualTo(0); } @@ -783,12 +783,12 @@ public class UserPermissionDaoTest { } private void assertOrgPermissionsOfUser(UserDto user, OrganizationDto organization, OrganizationPermission... permissions) { - assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getId(), organization.getUuid()).stream() + assertThat(underTest.selectGlobalPermissionsOfUser(dbSession, user.getUuid(), organization.getUuid()).stream() .map(OrganizationPermission::fromKey)) .containsOnly(permissions); } private void assertProjectPermissionsOfUser(UserDto user, ComponentDto project, String... permissions) { - assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user.getId(), project.uuid())).containsOnly(permissions); + assertThat(underTest.selectProjectPermissionsOfUser(dbSession, user.getUuid(), project.uuid())).containsOnly(permissions); } } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java index cf0ed18ece2..93bc6d09710 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java @@ -326,7 +326,7 @@ public class UserDbTester { */ @Deprecated public UserPermissionDto insertPermissionOnUser(OrganizationDto org, UserDto user, String permission) { - UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getId(), null); + UserPermissionDto dto = new UserPermissionDto(Uuids.create(), org.getUuid(), permission, user.getUuid(), null); db.getDbClient().userPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; @@ -340,12 +340,12 @@ public class UserDbTester { } public void deletePermissionFromUser(OrganizationDto org, UserDto user, OrganizationPermission permission) { - db.getDbClient().userPermissionDao().deleteGlobalPermission(db.getSession(), user.getId(), permission.getKey(), org.getUuid()); + db.getDbClient().userPermissionDao().deleteGlobalPermission(db.getSession(), user.getUuid(), permission.getKey(), org.getUuid()); db.commit(); } public void deletePermissionFromUser(ComponentDto project, UserDto user, String permission) { - db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user.getId(), permission, project.uuid()); + db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user.getUuid(), permission, project.uuid()); db.commit(); } @@ -356,7 +356,7 @@ public class UserDbTester { checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); checkArgument(project.getMainBranchProjectUuid() == null, "Permissions can't be granted on branches"); - UserPermissionDto dto = new UserPermissionDto(Uuids.create(), project.getOrganizationUuid(), permission, user.getId(), project.uuid()); + UserPermissionDto dto = new UserPermissionDto(Uuids.create(), project.getOrganizationUuid(), permission, user.getUuid(), project.uuid()); db.getDbClient().userPermissionDao().insert(db.getSession(), dto); db.commit(); return dto; @@ -364,11 +364,11 @@ public class UserDbTester { public List selectPermissionsOfUser(UserDto user, OrganizationDto organization) { return toListOfOrganizationPermissions(db.getDbClient().userPermissionDao() - .selectGlobalPermissionsOfUser(db.getSession(), user.getId(), organization.getUuid())); + .selectGlobalPermissionsOfUser(db.getSession(), user.getUuid(), organization.getUuid())); } public List selectProjectPermissionsOfUser(UserDto user, ComponentDto project) { - return db.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(db.getSession(), user.getId(), project.uuid()); + return db.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(db.getSession(), user.getUuid(), project.uuid()); } private static List toListOfOrganizationPermissions(List keys) { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java index c7fc42b7ff1..b45e0073379 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java @@ -288,6 +288,11 @@ import org.sonar.server.platform.db.migration.version.v83.users.fk.qprofileeditu import org.sonar.server.platform.db.migration.version.v83.users.fk.qprofileeditusers.DropUserIdColumnOfQProfileEditUsersTable; import org.sonar.server.platform.db.migration.version.v83.users.fk.qprofileeditusers.MakeQProfileEditUsersUserUuidColumnNotNullable; import org.sonar.server.platform.db.migration.version.v83.users.fk.qprofileeditusers.PopulateQProfileEditUsersUserUuid; +import org.sonar.server.platform.db.migration.version.v83.users.fk.userroles.AddIndexOnUserUuidOfUserRolesTable; +import org.sonar.server.platform.db.migration.version.v83.users.fk.userroles.AddUserUuidColumnToUserRoles; +import org.sonar.server.platform.db.migration.version.v83.users.fk.userroles.DropIndexOnUserIdOfUserRolesTable; +import org.sonar.server.platform.db.migration.version.v83.users.fk.userroles.DropUserIdColumnOfUserRolesTable; +import org.sonar.server.platform.db.migration.version.v83.users.fk.userroles.PopulateUserRolesUserUuid; import org.sonar.server.platform.db.migration.version.v83.usertokens.AddPrimaryKeyOnUuidColumnOfUserTokensTable; import org.sonar.server.platform.db.migration.version.v83.usertokens.AddUuidColumnToUserTokens; import org.sonar.server.platform.db.migration.version.v83.usertokens.DropIdColumnOfUserTokensTable; @@ -684,17 +689,23 @@ public class DbVersion83 implements DbVersion { .add(3654, "Drop column on 'user_id' column of 'PERM_TEMPLATES_USERS' table", DropUserIdColumnOfPermTemplatesUsersTable.class) // Migration of FK in PROPERTIES to USERS - .add(3616, "Add 'user_uuid' column on 'PROPERTIES' table", AddUserUuidColumnToPropertiesUsers.class) - .add(3617, "Populate 'user_uuid' for 'PROPERTIES'", PopulatePropertiesUserUuid.class) - .add(3618, "Drop column on 'user_id' column of 'PROPERTIES' table", DropUserIdColumnOfPropertiesTable.class) + .add(3655, "Add 'user_uuid' column on 'PROPERTIES' table", AddUserUuidColumnToPropertiesUsers.class) + .add(3656, "Populate 'user_uuid' for 'PROPERTIES'", PopulatePropertiesUserUuid.class) + .add(3657, "Drop column on 'user_id' column of 'PROPERTIES' table", DropUserIdColumnOfPropertiesTable.class) // Migration of FK in QPROFILE_EDIT_USERS to USERS - .add(3619, "Add 'user_uuid' column on 'QPROFILE_EDIT_USERS' table", AddUserUuidColumnToQProfileEditUsers.class) - .add(3620, "Populate 'user_uuid' for 'QPROFILE_EDIT_USERS'", PopulateQProfileEditUsersUserUuid.class) - .add(3621, "Make 'user_uuid' not-null for 'QPROFILE_EDIT_USERS'", MakeQProfileEditUsersUserUuidColumnNotNullable.class) - .add(3622, "Drop unique index on 'user_id','qprofile_uuid' columns of 'QPROFILE_EDIT_USERS' table", DropUniqueIndexOnUserIdAndQProfileUuidOfQProfileEditUsersTable.class) - .add(3623, "Add unique index on 'user_uuid','qprofile_uuid' columns of 'QPROFILE_EDIT_USERS' table", AddUniqueIndexOnUserUuidAndQProfileUuidOfQProfileEditUsersTable.class) - .add(3624, "Drop column on 'user_id' column of 'QPROFILE_EDIT_USERS' table", DropUserIdColumnOfQProfileEditUsersTable.class) + .add(3658, "Add 'user_uuid' column on 'QPROFILE_EDIT_USERS' table", AddUserUuidColumnToQProfileEditUsers.class) + .add(3659, "Populate 'user_uuid' for 'QPROFILE_EDIT_USERS'", PopulateQProfileEditUsersUserUuid.class) + .add(3660, "Make 'user_uuid' not-null for 'QPROFILE_EDIT_USERS'", MakeQProfileEditUsersUserUuidColumnNotNullable.class) + .add(3661, "Drop unique index on 'user_id','qprofile_uuid' columns of 'QPROFILE_EDIT_USERS' table", DropUniqueIndexOnUserIdAndQProfileUuidOfQProfileEditUsersTable.class) + .add(3662, "Add unique index on 'user_uuid','qprofile_uuid' columns of 'QPROFILE_EDIT_USERS' table", AddUniqueIndexOnUserUuidAndQProfileUuidOfQProfileEditUsersTable.class) + .add(3663, "Drop column on 'user_id' column of 'QPROFILE_EDIT_USERS' table", DropUserIdColumnOfQProfileEditUsersTable.class) + + .add(3664, "Add 'user_uuid' column on 'USER_ROLES' table", AddUserUuidColumnToUserRoles.class) + .add(3665, "Populate 'user_uuid' for 'USER_ROLES'", PopulateUserRolesUserUuid.class) + .add(3666, "Drop unique index on 'user_id' column of 'USER_ROLES' table", DropIndexOnUserIdOfUserRolesTable.class) + .add(3667, "Add unique index on 'user_uuid' columns of 'USER_ROLES' table", AddIndexOnUserUuidOfUserRolesTable.class) + .add(3668, "Drop column on 'user_id' column of 'USER_ROLES' table", DropUserIdColumnOfUserRolesTable.class) ; } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTable.java new file mode 100644 index 00000000000..b7e4b270ece --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTable.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class AddIndexOnUserUuidOfUserRolesTable extends DdlChange { + private static final String TABLE_NAME = "user_roles"; + private static final String INDEX_NAME = "user_roles_user"; + + public AddIndexOnUserUuidOfUserRolesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (!indexExists()) { + context.execute(new CreateIndexBuilder() + .setUnique(false) + .setTable(TABLE_NAME) + .setName(INDEX_NAME) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("user_uuid") + .setLimit(UUID_SIZE) + .build()) + .build()); + } + } + + private boolean indexExists() throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.indexExists(TABLE_NAME, INDEX_NAME, connection); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRoles.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRoles.java new file mode 100644 index 00000000000..5cf8280949d --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRoles.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.version.v83.users.fk.util.AddUserUuidColumnToTable; + +public class AddUserUuidColumnToUserRoles extends AddUserUuidColumnToTable { + + public AddUserUuidColumnToUserRoles(Database db) { + super(db, "user_roles"); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTable.java new file mode 100644 index 00000000000..a34f0b6a784 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTable.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.sql.DropIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropIndexOnUserIdOfUserRolesTable extends DdlChange { + private static final String TABLE_NAME = "user_roles"; + private static final String INDEX_NAME = "user_roles_user"; + + public DropIndexOnUserIdOfUserRolesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (indexExists()) { + context.execute(new DropIndexBuilder(getDialect()) + .setTable(TABLE_NAME) + .setName(INDEX_NAME) + .build()); + } + } + + private boolean indexExists() throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.indexExists(TABLE_NAME, INDEX_NAME, connection); + } + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTable.java new file mode 100644 index 00000000000..d1e43526b68 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTable.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropUserIdColumnOfUserRolesTable extends DdlChange { + public DropUserIdColumnOfUserRolesTable(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new DropColumnsBuilder(getDialect(), "user_roles", "user_id").build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuid.java new file mode 100644 index 00000000000..b645c44f3d9 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuid.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.DataChange; +import org.sonar.server.platform.db.migration.step.MassUpdate; + +public class PopulateUserRolesUserUuid extends DataChange { + + public PopulateUserRolesUserUuid(Database db) { + super(db); + } + + @Override + protected void execute(Context context) throws SQLException { + MassUpdate massUpdate = context.prepareMassUpdate(); + + massUpdate.select("select ur.uuid, u.uuid " + + "from user_roles ur " + + "join users u on ur.user_id = u.id where ur.user_uuid is null"); + + massUpdate.update("update user_roles set user_uuid = ? where uuid = ?"); + + massUpdate.execute((row, update, index) -> { + String userRolesUuid = row.getString(1); + String userUuid = row.getString(2); + + update.setString(1, userUuid); + update.setString(2, userRolesUuid); + return true; + }); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest.java new file mode 100644 index 00000000000..d64e1151b25 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class AddIndexOnUserUuidOfUserRolesTableTest { + + private static final String TABLE_NAME = "user_roles"; + private static final String INDEX_NAME = "user_roles_user"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(AddIndexOnUserUuidOfUserRolesTableTest.class, "schema.sql"); + + DdlChange underTest = new AddIndexOnUserUuidOfUserRolesTable(dbTester.database()); + + @Test + public void add_index() throws SQLException { + underTest.execute(); + dbTester.assertIndex(TABLE_NAME, INDEX_NAME, "user_uuid"); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + dbTester.assertIndex(TABLE_NAME, INDEX_NAME, "user_uuid"); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest.java new file mode 100644 index 00000000000..6103d41fe7d --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest.java @@ -0,0 +1,64 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.Uuids; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddUserUuidColumnToUserRolesTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(AddUserUuidColumnToUserRolesTest.class, "schema.sql"); + + private DdlChange underTest = new AddUserUuidColumnToUserRoles(db.database()); + + @Before + public void setup() { + insertUserRole(Uuids.createFast(), 1L); + insertUserRole(Uuids.createFast(), 2L); + insertUserRole(Uuids.createFast(), 3L); + } + + @Test + public void add_uuid_column() throws SQLException { + underTest.execute(); + + db.assertColumnDefinition("user_roles", "user_uuid", Types.VARCHAR, 40, true); + + assertThat(db.countSql("select count(*) from user_roles")) + .isEqualTo(3); + } + + private void insertUserRole(String uuid, Long userId) { + db.executeInsert("user_roles", + "uuid", uuid, + "organization_uuid", Uuids.createFast(), + "user_id", userId, + "role", Uuids.createFast()); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest.java new file mode 100644 index 00000000000..d80fff5cdaf --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest.java @@ -0,0 +1,52 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropIndexOnUserIdOfUserRolesTableTest { + + private static final String TABLE_NAME = "user_roles"; + private static final String INDEX_NAME = "user_roles_user"; + + @Rule + public CoreDbTester dbTester = CoreDbTester.createForSchema(DropIndexOnUserIdOfUserRolesTableTest.class, "schema.sql"); + + DdlChange underTest = new DropIndexOnUserIdOfUserRolesTable(dbTester.database()); + + @Test + public void add_index() throws SQLException { + underTest.execute(); + dbTester.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME); + } + + @Test + public void migration_is_reentrant() throws SQLException { + underTest.execute(); + // re-entrant + underTest.execute(); + dbTester.assertIndexDoesNotExist(TABLE_NAME, INDEX_NAME); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest.java new file mode 100644 index 00000000000..793662bb419 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class DropUserIdColumnOfUserRolesTableTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropUserIdColumnOfUserRolesTableTest.class, "schema.sql"); + + private DdlChange underTest = new DropUserIdColumnOfUserRolesTable(db.database()); + + @Test + public void execute() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist("user_roles", "user_id"); + } + + @Test + public void migration_is_not_re_entrant() throws SQLException { + underTest.execute(); + + assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest.java new file mode 100644 index 00000000000..6a771d21883 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest.java @@ -0,0 +1,145 @@ +/* + * SonarQube + * Copyright (C) 2009-2020 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.version.v83.users.fk.userroles; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.core.util.Uuids; +import org.sonar.db.CoreDbTester; +import org.sonar.server.platform.db.migration.step.DataChange; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PopulateUserRolesUserUuidTest { + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(PopulateUserRolesUserUuidTest.class, "schema.sql"); + + private DataChange underTest = new PopulateUserRolesUserUuid(db.database()); + + @Test + public void populate_uuids() throws SQLException { + long userId_1 = 1L; + String userUuid_1 = "uuid-1"; + insertUser(userId_1, userUuid_1); + + long userId_2 = 2L; + String userUuid_2 = "uuid-2"; + insertUser(userId_2, userUuid_2); + + long userId_3 = 3L; + String userUuid_3 = "uuid-3"; + insertUser(userId_3, userUuid_3); + + long userId_4 = 4L; + String userUuid_4 = "uuid-4"; + insertUser(userId_4, userUuid_4); + + String userRoleUuid_1 = Uuids.createFast(); + insertUserRole(userRoleUuid_1, userId_1); + String userRoleUuid_2 = Uuids.createFast(); + insertUserRole(userRoleUuid_2, userId_2); + String userRoleUuid_3 = Uuids.createFast(); + insertUserRole(userRoleUuid_3, userId_3); + String userRoleUuid_4 = Uuids.createFast(); + insertUserRole(userRoleUuid_4, userId_4); + String userRoleUuid_5 = Uuids.createFast(); + insertUserRole(userRoleUuid_5, userId_1); + + underTest.execute(); + + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_1, userUuid_1); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_2, userUuid_2); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_3, userUuid_3); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_4, userUuid_4); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_5, userUuid_1); + } + + @Test + public void migration_is_reentrant() throws SQLException { + long userId_1 = 1L; + String userUuid_1 = "uuid-1"; + insertUser(userId_1, userUuid_1); + + long userId_2 = 2L; + String userUuid_2 = "uuid-2"; + insertUser(userId_2, userUuid_2); + + long userId_3 = 3L; + String userUuid_3 = "uuid-3"; + insertUser(userId_3, userUuid_3); + + long userId_4 = 4L; + String userUuid_4 = "uuid-4"; + insertUser(userId_4, userUuid_4); + + String userRoleUuid_1 = Uuids.createFast(); + insertUserRole(userRoleUuid_1, userId_1); + String userRoleUuid_2 = Uuids.createFast(); + insertUserRole(userRoleUuid_2, userId_2); + String userRoleUuid_3 = Uuids.createFast(); + insertUserRole(userRoleUuid_3, userId_3); + + underTest.execute(); + + String userRoleUuid_4 = Uuids.createFast(); + insertUserRole(userRoleUuid_4, userId_4); + String userRoleUuid_5 = Uuids.createFast(); + insertUserRole(userRoleUuid_5, userId_1); + + // re-entrant + underTest.execute(); + + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_1, userUuid_1); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_2, userUuid_2); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_3, userUuid_3); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_4, userUuid_4); + assertThatUserRoleUserUuidIsEqualTo(userRoleUuid_5, userUuid_1); + } + + private void assertThatUserRoleUserUuidIsEqualTo(String userRoleUuid, String expectedUuid) { + assertThat(db.select(String.format("select user_uuid from user_roles where uuid = '%s'", userRoleUuid)) + .stream() + .map(row -> row.get("USER_UUID")) + .findFirst()) + .hasValue(expectedUuid); + } + + private void insertUserRole(String uuid, Long userId) { + db.executeInsert("user_roles", + "uuid", uuid, + "organization_uuid", Uuids.createFast(), + "user_id", userId, + "role", Uuids.createFast()); + } + + private void insertUser(Long id, String uuid) { + db.executeInsert("users", + "id", id, + "uuid", uuid, + "login", "login" + id, + "external_login", "ex-login" + id, + "external_identity_provider", "ex-provider" + id, + "external_id", id + 1, + "is_root", false, + "onboarded", false); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest/schema.sql new file mode 100644 index 00000000000..fee4bf2e2c9 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddIndexOnUserUuidOfUserRolesTableTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "USER_ROLES"( + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "USER_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(40) +); +ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); +CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest/schema.sql new file mode 100644 index 00000000000..b1619df129c --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/AddUserUuidColumnToUserRolesTest/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE "USER_ROLES"( + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "USER_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL +); +ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_ID"); +CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest/schema.sql new file mode 100644 index 00000000000..6080af78618 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropIndexOnUserIdOfUserRolesTableTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "USER_ROLES"( + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "USER_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(40) +); +ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_ID"); +CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest/schema.sql new file mode 100644 index 00000000000..6080af78618 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/DropUserIdColumnOfUserRolesTableTest/schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE "USER_ROLES"( + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "USER_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(40) +); +ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_ID"); +CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest/schema.sql new file mode 100644 index 00000000000..a1ad51f0b38 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/users/fk/userroles/PopulateUserRolesUserUuidTest/schema.sql @@ -0,0 +1,42 @@ +CREATE TABLE "USERS"( + "ID" INTEGER NOT NULL AUTO_INCREMENT (1,1), + "UUID" VARCHAR(255) NOT NULL, + "LOGIN" VARCHAR(255) NOT NULL, + "ORGANIZATION_UUID" VARCHAR(40), + "NAME" VARCHAR(200), + "EMAIL" VARCHAR(100), + "CRYPTED_PASSWORD" VARCHAR(100), + "SALT" VARCHAR(40), + "HASH_METHOD" VARCHAR(10), + "ACTIVE" BOOLEAN DEFAULT TRUE, + "SCM_ACCOUNTS" VARCHAR(4000), + "EXTERNAL_LOGIN" VARCHAR(255) NOT NULL, + "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100) NOT NULL, + "EXTERNAL_ID" VARCHAR(255) NOT NULL, + "IS_ROOT" BOOLEAN NOT NULL, + "USER_LOCAL" BOOLEAN, + "ONBOARDED" BOOLEAN NOT NULL, + "HOMEPAGE_TYPE" VARCHAR(40), + "HOMEPAGE_PARAMETER" VARCHAR(40), + "LAST_CONNECTION_DATE" BIGINT, + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); +ALTER TABLE "USERS" ADD CONSTRAINT "PK_USERS" PRIMARY KEY("ID"); +CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS"("LOGIN"); +CREATE INDEX "USERS_UPDATED_AT" ON "USERS"("UPDATED_AT"); +CREATE UNIQUE INDEX "USERS_UUID" ON "USERS"("UUID"); +CREATE UNIQUE INDEX "UNIQ_EXTERNAL_ID" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER", "EXTERNAL_ID"); +CREATE UNIQUE INDEX "UNIQ_EXTERNAL_LOGIN" ON "USERS"("EXTERNAL_IDENTITY_PROVIDER", "EXTERNAL_LOGIN"); + +CREATE TABLE "USER_ROLES"( + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "USER_ID" INTEGER, + "ROLE" VARCHAR(64) NOT NULL, + "COMPONENT_UUID" VARCHAR(40), + "UUID" VARCHAR(40) NOT NULL, + "USER_UUID" VARCHAR(40) +); +ALTER TABLE "USER_ROLES" ADD CONSTRAINT "PK_USER_ROLES" PRIMARY KEY("UUID"); +CREATE INDEX "USER_ROLES_USER" ON "USER_ROLES"("USER_ID"); +CREATE INDEX "USER_ROLES_COMPONENT_UUID" ON "USER_ROLES"("COMPONENT_UUID"); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/newindex/NewAuthorizedIndex.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/newindex/NewAuthorizedIndex.java index 289111f4af0..63bd13e7f4c 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/newindex/NewAuthorizedIndex.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/newindex/NewAuthorizedIndex.java @@ -40,7 +40,7 @@ public class NewAuthorizedIndex extends NewIndex { this.mainType = IndexType.main(index, TYPE_AUTHORIZATION); super.createTypeMapping(mainType) .keywordFieldBuilder(FIELD_GROUP_IDS).build() - .createLongField(FIELD_USER_IDS) + .keywordFieldBuilder(FIELD_USER_IDS).build() .createBooleanField(FIELD_ALLOW_ANYONE); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/AuthorizationDoc.java b/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/AuthorizationDoc.java index 64fbb797dce..0061d2a2450 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/AuthorizationDoc.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/AuthorizationDoc.java @@ -43,7 +43,7 @@ public class AuthorizationDoc extends BaseDoc { if (dto.isAllowAnyone()) { return res.setAllowAnyone(); } - return res.setRestricted(dto.getGroupUuids(), dto.getUserIds()); + return res.setRestricted(dto.getGroupUuids(), dto.getUserUuids()); } @Override @@ -73,10 +73,10 @@ public class AuthorizationDoc extends BaseDoc { return this; } - private AuthorizationDoc setRestricted(List groupUuids, List userIds) { + private AuthorizationDoc setRestricted(List groupUuids, List userUuids) { setField(FIELD_ALLOW_ANYONE, false); setField(FIELD_GROUP_IDS, groupUuids); - setField(FIELD_USER_IDS, userIds); + setField(FIELD_USER_IDS, userUuids); return this; } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/IndexPermissions.java b/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/IndexPermissions.java index a19e992f576..c4bab331349 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/IndexPermissions.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/permission/index/IndexPermissions.java @@ -25,7 +25,7 @@ import java.util.List; public final class IndexPermissions { private final String projectUuid; private final String qualifier; - private final List userIds = new ArrayList<>(); + private final List userUuids = new ArrayList<>(); private final List groupUuids = new ArrayList<>(); private boolean allowAnyone = false; @@ -42,12 +42,12 @@ public final class IndexPermissions { return qualifier; } - public List getUserIds() { - return userIds; + public List getUserUuids() { + return userUuids; } - public IndexPermissions addUserId(int l) { - userIds.add(l); + public IndexPermissions addUserUuid(String l) { + userUuids.add(l); return this; } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/newindex/NewAuthorizedIndexTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/newindex/NewAuthorizedIndexTest.java index 25d162ca4ca..5c082bf9416 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/newindex/NewAuthorizedIndexTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/newindex/NewAuthorizedIndexTest.java @@ -22,21 +22,18 @@ package org.sonar.server.es.newindex; import com.google.common.collect.ImmutableMap; import java.util.Locale; import java.util.Map; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonar.api.config.internal.MapSettings; import org.sonar.server.es.Index; import org.sonar.server.es.IndexType; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; public class NewAuthorizedIndexTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private String someIndexName = randomAlphabetic(10).toLowerCase(Locale.ENGLISH); private Index someIndex = Index.withRelations(someIndexName); @@ -47,10 +44,9 @@ public class NewAuthorizedIndexTest { public void constructor_fails_with_IAE_if_index_does_not_support_relations() { Index simpleIndex = Index.simple(someIndexName); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Index must accept relations"); - - new NewAuthorizedIndex(simpleIndex, defaultSettingsConfiguration); + assertThatThrownBy(() -> new NewAuthorizedIndex(simpleIndex, defaultSettingsConfiguration)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Index must accept relations"); } @Test @@ -64,10 +60,9 @@ public class NewAuthorizedIndexTest { public void build_fails_if_no_relation_mapping_has_been_created() { NewAuthorizedIndex underTest = new NewAuthorizedIndex(someIndex, defaultSettingsConfiguration); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("At least one relation mapping must be defined"); - - underTest.build(); + assertThatThrownBy(() -> underTest.build()) + .isInstanceOf(IllegalStateException.class) + .hasMessage("At least one relation mapping must be defined"); } @Test @@ -108,7 +103,7 @@ public class NewAuthorizedIndexTest { assertThat(getFieldAsMap(properties, "auth_groupIds")) .contains(entry("type", "keyword")); assertThat(getFieldAsMap(properties, "auth_userIds")) - .containsOnly(entry("type", "long")); + .contains(entry("type", "keyword")); assertThat(getFieldAsMap(properties, "auth_allowAnyone")) .containsOnly(entry("type", "boolean")); } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/permission/index/AuthorizationDocTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/permission/index/AuthorizationDocTest.java index d44447752e5..4fa0145ee5c 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/permission/index/AuthorizationDocTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/permission/index/AuthorizationDocTest.java @@ -123,15 +123,15 @@ public class AuthorizationDocTest { @Test public void fromDto_defines_userIds_and_groupIds_if_allowAnyone_is_false() { IndexPermissions underTest = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); - IntStream.range(0, 1 + new Random().nextInt(5)).forEach(underTest::addUserId); + IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(String::valueOf).forEach(underTest::addUserUuid); IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(Integer::toString).forEach(underTest::addGroupUuid); AuthorizationDoc doc = AuthorizationDoc.fromDto(IndexType.main(Index.simple("foo"), "bar"), underTest); boolean auth_allowAnyone = doc.getField("auth_allowAnyone"); assertThat(auth_allowAnyone).isFalse(); - List userIds = doc.getField("auth_userIds"); - assertThat(userIds).isEqualTo(underTest.getUserIds()); + List userUuids = doc.getField("auth_userIds"); + assertThat(userUuids).isEqualTo(underTest.getUserUuids()); List groupUuids = doc.getField("auth_groupIds"); assertThat(groupUuids).isEqualTo(underTest.getGroupUuids()); } @@ -139,7 +139,7 @@ public class AuthorizationDocTest { @Test public void fromDto_ignores_userIds_and_groupUuids_if_allowAnyone_is_true() { IndexPermissions underTest = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); - IntStream.range(0, 1 + new Random().nextInt(5)).forEach(underTest::addUserId); + IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(String::valueOf).forEach(underTest::addUserUuid); IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(Integer::toString).forEach(underTest::addGroupUuid); underTest.allowAnyone(); @@ -166,11 +166,11 @@ public class AuthorizationDocTest { IndexPermissions allowAnyone = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); allowAnyone.allowAnyone(); IndexPermissions someUserIds = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); - IntStream.range(0, 1 + new Random().nextInt(5)).forEach(someUserIds::addUserId); + IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(String::valueOf).forEach(someUserIds::addUserUuid); IndexPermissions someGroupUuids = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(Integer::toString).forEach(someGroupUuids::addGroupUuid); IndexPermissions someGroupUuidAndUserIs = new IndexPermissions(randomAlphabetic(3), randomAlphabetic(4)); - IntStream.range(0, 1 + new Random().nextInt(5)).forEach(someGroupUuidAndUserIs::addUserId); + IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(String::valueOf).forEach(someGroupUuidAndUserIs::addUserUuid); IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(Integer::toString).forEach(someGroupUuidAndUserIs::addGroupUuid); return new Object[][] { {allowAnyone}, diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java index 58da4ebdee1..35532d1aafd 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java @@ -133,10 +133,9 @@ public class MemberUpdater { } private void removeMemberInDb(DbSession dbSession, OrganizationDto organization, UserDto user) { - int userId = user.getId(); String userUuid = user.getUuid(); String organizationUuid = organization.getUuid(); - dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationUuid, userId); + dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationUuid, userUuid); dbClient.permissionTemplateDao().deleteUserPermissionsByOrganization(dbSession, organizationUuid, userUuid); dbClient.qProfileEditUsersDao().deleteByOrganizationAndUser(dbSession, organization, user); dbClient.userGroupDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userUuid); diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/organization/MemberUpdaterTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/organization/MemberUpdaterTest.java index f8d11d8bd49..44c288ff061 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/organization/MemberUpdaterTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/organization/MemberUpdaterTest.java @@ -498,13 +498,13 @@ public class MemberUpdaterTest { } private void assertOrgPermissionsOfUser(UserDto user, OrganizationDto organization, OrganizationPermission... permissions) { - assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(db.getSession(), user.getId(), organization.getUuid()).stream() + assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(db.getSession(), user.getUuid(), organization.getUuid()).stream() .map(OrganizationPermission::fromKey)) .containsOnly(permissions); } private void assertProjectPermissionsOfUser(UserDto user, ComponentDto project, String... permissions) { - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(db.getSession(), user.getId(), project.uuid())).containsOnly(permissions); + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(db.getSession(), user.getUuid(), project.uuid())).containsOnly(permissions); } private void insertProperty(String key, @Nullable String value, @Nullable String componentUuid, @Nullable String userUuid) { diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java index 2ca7818d3ff..9f02edeeaae 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexerDao.java @@ -47,7 +47,7 @@ public class PermissionIndexerDao { private static final String SQL_TEMPLATE = "SELECT " + " project_authorization.kind as kind, " + " project_authorization.project as project, " + - " project_authorization.user_id as user_id, " + + " project_authorization.user_uuid as user_uuid, " + " project_authorization.group_uuid as group_uuid, " + " project_authorization.qualifier as qualifier " + "FROM ( " + @@ -57,7 +57,7 @@ public class PermissionIndexerDao { " SELECT '" + RowKind.USER + "' as kind," + " c.uuid AS project, " + " c.qualifier AS qualifier, " + - " user_roles.user_id AS user_id, " + + " user_roles.user_uuid AS user_uuid, " + " NULL AS group_uuid " + " FROM components c " + " INNER JOIN user_roles ON user_roles.component_uuid = c.uuid AND user_roles.role = 'user' " + @@ -74,7 +74,7 @@ public class PermissionIndexerDao { " SELECT '" + RowKind.GROUP + "' as kind," + " c.uuid AS project, " + " c.qualifier AS qualifier, " + - " NULL AS user_id, " + + " NULL AS user_uuid, " + " groups.uuid AS group_uuid " + " FROM components c " + " INNER JOIN group_roles ON group_roles.component_uuid = c.uuid AND group_roles.role = 'user' " + @@ -93,7 +93,7 @@ public class PermissionIndexerDao { " SELECT '" + RowKind.ANYONE + "' as kind," + " c.uuid AS project, " + " c.qualifier AS qualifier, " + - " NULL AS user_id, " + + " NULL AS user_uuid, " + " NULL AS group_uuid " + " FROM components c " + " WHERE " + @@ -109,7 +109,7 @@ public class PermissionIndexerDao { " SELECT '" + RowKind.NONE + "' as kind," + " c.uuid AS project, " + " c.qualifier AS qualifier, " + - " NULL AS user_id, " + + " NULL AS user_uuid, " + " NULL AS group_uuid " + " FROM components c " + " WHERE " + @@ -197,7 +197,7 @@ public class PermissionIndexerDao { case NONE: break; case USER: - dto.addUserId(rs.getInt(3)); + dto.addUserUuid(rs.getString(3)); break; case GROUP: dto.addGroupUuid(rs.getString(4)); diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/WebAuthorizationTypeSupport.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/WebAuthorizationTypeSupport.java index c7c9adc43a9..efd3df0a1c5 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/WebAuthorizationTypeSupport.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/WebAuthorizationTypeSupport.java @@ -53,16 +53,14 @@ public class WebAuthorizationTypeSupport { return QueryBuilders.matchAllQuery(); } - Integer userId = userSession.getUserId(); BoolQueryBuilder filter = boolQuery(); // anyone filter.should(QueryBuilders.termQuery(FIELD_ALLOW_ANYONE, true)); // users - Optional.ofNullable(userId) - .map(Integer::longValue) - .ifPresent(id -> filter.should(termQuery(FIELD_USER_IDS, id))); + Optional.ofNullable(userSession.getUuid()) + .ifPresent(uuid -> filter.should(termQuery(FIELD_USER_IDS, uuid))); // groups userSession.getGroups() diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java index e14865bfd14..b4b54c18c93 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java @@ -1672,7 +1672,7 @@ public class ProjectMeasuresIndexTest { private void indexForUser(UserDto user, ProjectMeasuresDoc... docs) { es.putDocuments(TYPE_PROJECT_MEASURES, docs); - authorizationIndexer.allow(stream(docs).map(doc -> new IndexPermissions(doc.getId(), PROJECT).addUserId(user.getId())).collect(toList())); + authorizationIndexer.allow(stream(docs).map(doc -> new IndexPermissions(doc.getId(), PROJECT).addUserUuid(user.getUuid())).collect(toList())); } private void indexForGroup(GroupDto group, ProjectMeasuresDoc... docs) { diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java index fb9eb32b389..a032bf30f5d 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java @@ -109,13 +109,13 @@ public class PermissionIndexerDaoTest { IndexPermissions privateProject1Authorization = getByProjectUuid(privateProject1.uuid(), dtos); assertThat(privateProject1Authorization.getGroupUuids()).containsOnly(group.getUuid()); assertThat(privateProject1Authorization.isAllowAnyone()).isFalse(); - assertThat(privateProject1Authorization.getUserIds()).containsOnly(user1.getId(), user2.getId()); + assertThat(privateProject1Authorization.getUserUuids()).containsOnly(user1.getUuid(), user2.getUuid()); assertThat(privateProject1Authorization.getQualifier()).isEqualTo(PROJECT); IndexPermissions privateProject2Authorization = getByProjectUuid(privateProject2.uuid(), dtos); assertThat(privateProject2Authorization.getGroupUuids()).isEmpty(); assertThat(privateProject2Authorization.isAllowAnyone()).isFalse(); - assertThat(privateProject2Authorization.getUserIds()).containsOnly(user1.getId()); + assertThat(privateProject2Authorization.getUserUuids()).containsOnly(user1.getUuid()); assertThat(privateProject2Authorization.getQualifier()).isEqualTo(PROJECT); IndexPermissions view2Authorization = getByProjectUuid(view2.uuid(), dtos); @@ -144,13 +144,13 @@ public class PermissionIndexerDaoTest { IndexPermissions privateProject1Authorization = dtos.get(privateProject1.uuid()); assertThat(privateProject1Authorization.getGroupUuids()).containsOnly(group.getUuid()); assertThat(privateProject1Authorization.isAllowAnyone()).isFalse(); - assertThat(privateProject1Authorization.getUserIds()).containsOnly(user1.getId(), user2.getId()); + assertThat(privateProject1Authorization.getUserUuids()).containsOnly(user1.getUuid(), user2.getUuid()); assertThat(privateProject1Authorization.getQualifier()).isEqualTo(PROJECT); IndexPermissions privateProject2Authorization = dtos.get(privateProject2.uuid()); assertThat(privateProject2Authorization.getGroupUuids()).isEmpty(); assertThat(privateProject2Authorization.isAllowAnyone()).isFalse(); - assertThat(privateProject2Authorization.getUserIds()).containsOnly(user1.getId()); + assertThat(privateProject2Authorization.getUserUuids()).containsOnly(user1.getUuid()); assertThat(privateProject2Authorization.getQualifier()).isEqualTo(PROJECT); IndexPermissions view2Authorization = dtos.get(view2.uuid()); @@ -196,7 +196,7 @@ public class PermissionIndexerDaoTest { Assertions.assertThat(dtos).hasSize(1); IndexPermissions dto = dtos.get(0); assertThat(dto.getGroupUuids()).isEmpty(); - assertThat(dto.getUserIds()).isEmpty(); + assertThat(dto.getUserUuids()).isEmpty(); assertThat(dto.isAllowAnyone()).isFalse(); assertThat(dto.getProjectUuid()).isEqualTo(privateProject1.uuid()); assertThat(dto.getQualifier()).isEqualTo(privateProject1.qualifier()); @@ -209,7 +209,7 @@ public class PermissionIndexerDaoTest { Assertions.assertThat(dtos).hasSize(1); IndexPermissions dto = dtos.get(0); assertThat(dto.getGroupUuids()).isEmpty(); - assertThat(dto.getUserIds()).isEmpty(); + assertThat(dto.getUserUuids()).isEmpty(); assertThat(dto.isAllowAnyone()).isTrue(); assertThat(dto.getProjectUuid()).isEqualTo(publicProject.uuid()); assertThat(dto.getQualifier()).isEqualTo(publicProject.qualifier()); @@ -223,7 +223,7 @@ public class PermissionIndexerDaoTest { Assertions.assertThat(dtos).hasSize(1); IndexPermissions dto = dtos.get(0); assertThat(dto.getGroupUuids()).isEmpty(); - assertThat(dto.getUserIds()).containsOnly(user1.getId()); + assertThat(dto.getUserUuids()).containsOnly(user1.getUuid()); assertThat(dto.isAllowAnyone()).isFalse(); assertThat(dto.getProjectUuid()).isEqualTo(privateProject1.uuid()); assertThat(dto.getQualifier()).isEqualTo(privateProject1.qualifier()); @@ -238,7 +238,7 @@ public class PermissionIndexerDaoTest { Assertions.assertThat(dtos).hasSize(1); IndexPermissions dto = dtos.get(0); assertThat(dto.getGroupUuids()).containsOnly(group.getUuid()); - assertThat(dto.getUserIds()).isEmpty(); + assertThat(dto.getUserUuids()).isEmpty(); assertThat(dto.isAllowAnyone()).isFalse(); assertThat(dto.getProjectUuid()).isEqualTo(privateProject1.uuid()); assertThat(dto.getQualifier()).isEqualTo(privateProject1.qualifier()); @@ -247,7 +247,7 @@ public class PermissionIndexerDaoTest { private void isPublic(IndexPermissions view1Authorization, String qualifier) { assertThat(view1Authorization.getGroupUuids()).isEmpty(); assertThat(view1Authorization.isAllowAnyone()).isTrue(); - assertThat(view1Authorization.getUserIds()).isEmpty(); + assertThat(view1Authorization.getUserUuids()).isEmpty(); assertThat(view1Authorization.getQualifier()).isEqualTo(qualifier); } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java index 5a10ece3e7c..326a75ac709 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java @@ -379,7 +379,7 @@ public class PermissionIndexerTest { } private UserSessionRule logIn(UserDto u) { - userSession.logIn(u.getLogin()).setUserId(u.getId()); + userSession.logIn(u); return userSession; } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/WebAuthorizationTypeSupportTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/WebAuthorizationTypeSupportTest.java index 6814a3e0e79..79f9c9f54fc 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/WebAuthorizationTypeSupportTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/WebAuthorizationTypeSupportTest.java @@ -26,6 +26,8 @@ import org.junit.Rule; import org.junit.Test; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupTesting; +import org.sonar.db.user.UserDto; +import org.sonar.db.user.UserTesting; import org.sonar.server.tester.UserSessionRule; import static org.assertj.core.api.Assertions.assertThat; @@ -54,100 +56,102 @@ public class WebAuthorizationTypeSupportTest { HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter(); assertJson(filter.toString()).isSimilarTo("{" + - " \"has_parent\" : {" + - " \"query\" : {" + - " \"bool\" : {" + - " \"filter\" : [{" + - " \"bool\" : {" + - " \"should\" : [{" + - " \"term\" : {" + - " \"auth_allowAnyone\" : {\"value\": true}" + - " }" + - " }]" + - " }" + - " }]" + - " }" + - " }," + - " \"parent_type\" : \"auth\"" + - " }" + - "}"); + " \"has_parent\" : {" + + " \"query\" : {" + + " \"bool\" : {" + + " \"filter\" : [{" + + " \"bool\" : {" + + " \"should\" : [{" + + " \"term\" : {" + + " \"auth_allowAnyone\" : {\"value\": true}" + + " }" + + " }]" + + " }" + + " }]" + + " }" + + " }," + + " \"parent_type\" : \"auth\"" + + " }" + + "}"); } @Test public void createQueryFilter_sets_filter_on_anyone_and_user_id_if_user_is_logged_in_but_has_no_groups() { - userSession.logIn().setUserId(1234); + UserDto userDto = UserTesting.newUserDto(); + userSession.logIn(userDto); HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter(); assertJson(filter.toString()).isSimilarTo("{" + - " \"has_parent\": {" + - " \"query\": {" + - " \"bool\": {" + - " \"filter\": [{" + - " \"bool\": {" + - " \"should\": [" + - " {" + - " \"term\": {" + - " \"auth_allowAnyone\": {\"value\": true}" + - " }" + - " }," + - " {" + - " \"term\": {" + - " \"auth_userIds\": {\"value\": 1234}" + - " }" + - " }" + - " ]" + - " }" + - " }]" + - " }" + - " }," + - " \"parent_type\": \"auth\"" + - " }" + - "}"); + " \"has_parent\": {" + + " \"query\": {" + + " \"bool\": {" + + " \"filter\": [{" + + " \"bool\": {" + + " \"should\": [" + + " {" + + " \"term\": {" + + " \"auth_allowAnyone\": {\"value\": true}" + + " }" + + " }," + + " {" + + " \"term\": {" + + " \"auth_userIds\": {\"value\": \"" + userDto.getUuid() + "\"}" + + " }" + + " }" + + " ]" + + " }" + + " }]" + + " }" + + " }," + + " \"parent_type\": \"auth\"" + + " }" + + "}"); } @Test public void createQueryFilter_sets_filter_on_anyone_and_user_id_and_group_ids_if_user_is_logged_in_and_has_groups() { GroupDto group1 = GroupTesting.newGroupDto().setUuid("10"); GroupDto group2 = GroupTesting.newGroupDto().setUuid("11"); - userSession.logIn().setUserId(1234).setGroups(group1, group2); + UserDto userDto = UserTesting.newUserDto(); + userSession.logIn(userDto).setGroups(group1, group2); HasParentQueryBuilder filter = (HasParentQueryBuilder) underTest.createQueryFilter(); assertJson(filter.toString()).isSimilarTo("{" + - " \"has_parent\": {" + - " \"query\": {" + - " \"bool\": {" + - " \"filter\": [{" + - " \"bool\": {" + - " \"should\": [" + - " {" + - " \"term\": {" + - " \"auth_allowAnyone\": {\"value\": true}" + - " }" + - " }," + - " {" + - " \"term\": {" + - " \"auth_userIds\": {\"value\": 1234}" + - " }" + - " }," + - " {" + - " \"term\": {" + - " \"auth_groupIds\": {\"value\": \"10\"}" + - " }" + - " }," + - " {" + - " \"term\": {" + - " \"auth_groupIds\": {\"value\": \"11\"}" + - " }" + - " }" + - " ]" + - " }" + - " }]" + - " }" + - " }," + - " \"parent_type\": \"auth\"" + - " }" + - "}"); + " \"has_parent\": {" + + " \"query\": {" + + " \"bool\": {" + + " \"filter\": [{" + + " \"bool\": {" + + " \"should\": [" + + " {" + + " \"term\": {" + + " \"auth_allowAnyone\": {\"value\": true}" + + " }" + + " }," + + " {" + + " \"term\": {" + + " \"auth_userIds\": {\"value\": \"" + userDto.getUuid() + "\"}" + + " }" + + " }," + + " {" + + " \"term\": {" + + " \"auth_groupIds\": {\"value\": \"10\"}" + + " }" + + " }," + + " {" + + " \"term\": {" + + " \"auth_groupIds\": {\"value\": \"11\"}" + + " }" + + " }" + + " ]" + + " }" + + " }]" + + " }" + + " }," + + " \"parent_type\": \"auth\"" + + " }" + + "}"); } } diff --git a/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java b/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java index e6d14ec137a..facf7284c86 100644 --- a/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java +++ b/server/sonar-webserver-es/src/testFixtures/java/org/sonar/server/permission/index/PermissionIndexerTester.java @@ -44,7 +44,7 @@ public class PermissionIndexerTester { public PermissionIndexerTester allowOnlyUser(ComponentDto project, UserDto user) { IndexPermissions dto = new IndexPermissions(project.uuid(), project.qualifier()) - .addUserId(user.getId()); + .addUserUuid(user.getUuid()); return allow(dto); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java index f0f965d49ca..23c240a47b9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java @@ -132,8 +132,8 @@ public class PermissionTemplateService { List usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid()); String organizationUuid = template.getOrganizationUuid(); - Map userDtoMap = dbClient.userDao().selectByUuids(dbSession, usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet())) - .stream().collect(Collectors.toMap(UserDto::getUuid, UserDto::getId)); + Map userDtoMap = dbClient.userDao().selectByUuids(dbSession, usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet())) + .stream().collect(Collectors.toMap(UserDto::getUuid, UserDto::getUuid)); usersPermissions .stream() .filter(up -> permissionValidForProject(project, up.getPermission())) @@ -170,7 +170,7 @@ public class PermissionTemplateService { .filter(up -> permissionValidForProject(project, up.getPermission())) .filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())) .forEach(c -> { - UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), organizationUuid, c.getPermission(), userDto.getId(), project.uuid()); + UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), organizationUuid, c.getPermission(), userDto.getUuid(), project.uuid()); dbClient.userPermissionDao().insert(dbSession, dto); }); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/UserPermissionChanger.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/UserPermissionChanger.java index 11cda2fac2b..3142711d953 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/UserPermissionChanger.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/UserPermissionChanger.java @@ -93,7 +93,8 @@ public class UserPermissionChanger { if (loadExistingPermissions(dbSession, change).contains(change.getPermission())) { return false; } - UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), change.getOrganizationUuid(), change.getPermission(), change.getUserId().getId(), change.getProjectUuid()); + UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), change.getOrganizationUuid(), change.getPermission(), change.getUserId().getUuid(), + change.getProjectUuid()); dbClient.userPermissionDao().insert(dbSession, dto); return true; } @@ -105,9 +106,9 @@ public class UserPermissionChanger { checkOtherAdminsExist(dbSession, change); String projectUuid = change.getProjectUuid(); if (projectUuid != null) { - dbClient.userPermissionDao().deleteProjectPermission(dbSession, change.getUserId().getId(), change.getPermission(), projectUuid); + dbClient.userPermissionDao().deleteProjectPermission(dbSession, change.getUserId().getUuid(), change.getPermission(), projectUuid); } else { - dbClient.userPermissionDao().deleteGlobalPermission(dbSession, change.getUserId().getId(), change.getPermission(), change.getOrganizationUuid()); + dbClient.userPermissionDao().deleteGlobalPermission(dbSession, change.getUserId().getUuid(), change.getPermission(), change.getOrganizationUuid()); } return true; } @@ -115,10 +116,10 @@ public class UserPermissionChanger { private List loadExistingPermissions(DbSession dbSession, UserPermissionChange change) { String projectUuid = change.getProjectUuid(); if (projectUuid != null) { - return dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, change.getUserId().getId(), projectUuid); + return dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, change.getUserId().getUuid(), projectUuid); } return dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, - change.getUserId().getId(), + change.getUserId().getUuid(), change.getOrganizationUuid()); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/UsersAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/UsersAction.java index 76450b7c3dc..cb385a302f7 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/UsersAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/UsersAction.java @@ -145,14 +145,14 @@ public class UsersAction implements PermissionsWsAction { } private UsersWsResponse buildResponse(List users, List userPermissions, Paging paging) { - Multimap permissionsByUserId = TreeMultimap.create(); - userPermissions.forEach(userPermission -> permissionsByUserId.put(userPermission.getUserId(), userPermission.getPermission())); + Multimap permissionsByUserUuid = TreeMultimap.create(); + userPermissions.forEach(userPermission -> permissionsByUserUuid.put(userPermission.getUserUuid(), userPermission.getPermission())); UsersWsResponse.Builder response = UsersWsResponse.newBuilder(); users.forEach(user -> { Permissions.User.Builder userResponse = response.addUsersBuilder() .setLogin(user.getLogin()) - .addAllPermissions(permissionsByUserId.get(user.getId())); + .addAllPermissions(permissionsByUserUuid.get(user.getUuid())); ofNullable(user.getEmail()).ifPresent(userResponse::setEmail); ofNullable(emptyToNull(user.getEmail())).ifPresent(u -> userResponse.setAvatar(avatarResolver.create(user))); ofNullable(user.getName()).ifPresent(userResponse::setName); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java index 5c6130790fe..29ac7584956 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java @@ -146,13 +146,13 @@ public class UpdateVisibilityAction implements ProjectsWsAction { PUBLIC_PERMISSIONS.forEach(permission -> { dbClient.groupPermissionDao().selectGroupUuidsWithPermissionOnProjectBut(dbSession, component.uuid(), permission) .forEach(group -> insertProjectPermissionOnGroup(dbSession, component, permission, group)); - dbClient.userPermissionDao().selectUserIdsWithPermissionOnProjectBut(dbSession, component.uuid(), permission) - .forEach(userId -> insertProjectPermissionOnUser(dbSession, component, permission, userId)); + dbClient.userPermissionDao().selectUserUuidsWithPermissionOnProjectBut(dbSession, component.uuid(), permission) + .forEach(userUuid -> insertProjectPermissionOnUser(dbSession, component, permission, userUuid)); }); } - private void insertProjectPermissionOnUser(DbSession dbSession, ComponentDto component, String permission, Integer userId) { - dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(Uuids.create(), component.getOrganizationUuid(), permission, userId, component.uuid())); + private void insertProjectPermissionOnUser(DbSession dbSession, ComponentDto component, String permission, String userUuid) { + dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(Uuids.create(), component.getOrganizationUuid(), permission, userUuid, component.uuid())); } private void insertProjectPermissionOnGroup(DbSession dbSession, ComponentDto component, String permission, String groupUuid) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/DeactivateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/DeactivateAction.java index eb4a2ce0566..a5da21e08a4 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/DeactivateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/DeactivateAction.java @@ -97,13 +97,12 @@ public class DeactivateAction implements UsersWsAction { ensureNotLastAdministrator(dbSession, user); - Integer userId = user.getId(); String userUuid = user.getUuid(); dbClient.userTokenDao().deleteByUser(dbSession, user); dbClient.propertiesDao().deleteByKeyAndValue(dbSession, DEFAULT_ISSUE_ASSIGNEE, user.getLogin()); dbClient.propertiesDao().deleteByQuery(dbSession, PropertyQuery.builder().setUserUuid(userUuid).build()); dbClient.userGroupDao().deleteByUserUuid(dbSession, userUuid); - dbClient.userPermissionDao().deleteByUserId(dbSession, userId); + dbClient.userPermissionDao().deleteByUserUuid(dbSession, userUuid); dbClient.permissionTemplateDao().deleteUserPermissionsByUserUuid(dbSession, userUuid); dbClient.qProfileEditUsersDao().deleteByUser(dbSession, user); dbClient.organizationMemberDao().deleteByUserUuid(dbSession, userUuid); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java index f1ecbd8cd0c..8fd3bcfee1c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java @@ -442,7 +442,7 @@ public class PermissionTemplateServiceTest { private List selectProjectPermissionsOfUser(UserDto userDto, ComponentDto project) { return dbTester.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(session, - userDto.getId(), project.uuid()); + userDto.getUuid(), project.uuid()); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java index bbbe522c1c5..6f274c69d64 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java @@ -471,11 +471,11 @@ public class UpdateVisibilityActionTest { .setParam(PARAM_VISIBILITY, PRIVATE) .execute(); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user1.getId(), project.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user1.getUuid(), project.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, "p1", "p2"); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user2.getId(), project.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user2.getUuid(), project.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, "p2"); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user3.getId(), project.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user3.getUuid(), project.uuid())) .isEmpty(); } @@ -520,7 +520,7 @@ public class UpdateVisibilityActionTest { assertThat(dbClient.componentDao().selectByUuid(dbSession, portfolio.uuid()).get().isPrivate()).isTrue(); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getUuid(), portfolio.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, UserRole.ISSUE_ADMIN); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), portfolio.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), portfolio.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, UserRole.ADMIN); } @@ -545,7 +545,7 @@ public class UpdateVisibilityActionTest { assertThat(dbClient.componentDao().selectByUuid(dbSession, portfolio.uuid()).get().isPrivate()).isFalse(); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getUuid(), portfolio.uuid())) .containsOnly(UserRole.ISSUE_ADMIN); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), portfolio.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), portfolio.uuid())) .containsOnly(UserRole.ADMIN); } @@ -566,7 +566,7 @@ public class UpdateVisibilityActionTest { assertThat(dbClient.componentDao().selectByUuid(dbSession, application.uuid()).get().isPrivate()).isTrue(); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getUuid(), application.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, UserRole.ISSUE_ADMIN); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), application.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), application.uuid())) .containsOnly(UserRole.USER, UserRole.CODEVIEWER, UserRole.ADMIN); } @@ -591,7 +591,7 @@ public class UpdateVisibilityActionTest { assertThat(dbClient.componentDao().selectByUuid(dbSession, portfolio.uuid()).get().isPrivate()).isFalse(); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, organization.getUuid(), group.getUuid(), portfolio.uuid())) .containsOnly(UserRole.ISSUE_ADMIN); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), portfolio.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), portfolio.uuid())) .containsOnly(UserRole.ADMIN); } @@ -678,7 +678,7 @@ public class UpdateVisibilityActionTest { } private void unsafeInsertProjectPermissionOnUser(ComponentDto component, UserDto user, String permission) { - UserPermissionDto dto = new UserPermissionDto(Uuids.create(), component.getOrganizationUuid(), permission, user.getId(), component.uuid()); + UserPermissionDto dto = new UserPermissionDto(Uuids.create(), component.getOrganizationUuid(), permission, user.getUuid(), component.uuid()); dbTester.getDbClient().userPermissionDao().insert(dbTester.getSession(), dto); dbTester.commit(); } @@ -688,13 +688,13 @@ public class UpdateVisibilityActionTest { .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectGlobalPermissionsOfGroup(dbSession, component.getOrganizationUuid(), group.getUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); - assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getId(), component.getOrganizationUuid())) + assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getUuid(), component.getOrganizationUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, component.getOrganizationUuid(), null, component.uuid())) .isEmpty(); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, component.getOrganizationUuid(), group.getUuid(), component.uuid())) .containsAll(permissionService.getAllProjectPermissions()); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), component.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), component.uuid())) .containsAll(permissionService.getAllProjectPermissions()); } @@ -703,7 +703,7 @@ public class UpdateVisibilityActionTest { .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectGlobalPermissionsOfGroup(dbSession, component.getOrganizationUuid(), group.getUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); - assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getId(), component.getOrganizationUuid())) + assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getUuid(), component.getOrganizationUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, component.getOrganizationUuid(), null, component.uuid())) .doesNotContain(UserRole.USER) @@ -713,7 +713,7 @@ public class UpdateVisibilityActionTest { .doesNotContain(UserRole.USER) .doesNotContain(UserRole.CODEVIEWER) .containsAll(PROJECT_PERMISSIONS_BUT_USER_AND_CODEVIEWER); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), component.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), component.uuid())) .doesNotContain(UserRole.USER) .doesNotContain(UserRole.CODEVIEWER) .containsAll(PROJECT_PERMISSIONS_BUT_USER_AND_CODEVIEWER); @@ -724,13 +724,13 @@ public class UpdateVisibilityActionTest { .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectGlobalPermissionsOfGroup(dbSession, component.getOrganizationUuid(), group.getUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); - assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getId(), component.getOrganizationUuid())) + assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getUuid(), component.getOrganizationUuid())) .containsAll(ORGANIZATION_PERMISSIONS_NAME_SET); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, component.getOrganizationUuid(), null, component.uuid())) .containsAll(permissionService.getAllProjectPermissions()); assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, component.getOrganizationUuid(), group.getUuid(), component.uuid())) .containsAll(permissionService.getAllProjectPermissions()); - assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), component.uuid())) + assertThat(dbClient.userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), component.uuid())) .containsAll(permissionService.getAllProjectPermissions()); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java index 732cea32753..010afb4a438 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java @@ -163,8 +163,8 @@ public class DeactivateActionTest { deactivate(user.getLogin()); - assertThat(db.getDbClient().userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getId(), db.getDefaultOrganization().getUuid())).isEmpty(); - assertThat(db.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getId(), project.uuid())).isEmpty(); + assertThat(db.getDbClient().userPermissionDao().selectGlobalPermissionsOfUser(dbSession, user.getUuid(), db.getDefaultOrganization().getUuid())).isEmpty(); + assertThat(db.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(dbSession, user.getUuid(), project.uuid())).isEmpty(); } @Test -- 2.39.5