From bb15beca157d3623f0fa9f40a1d77c37d7776649 Mon Sep 17 00:00:00 2001 From: kl Date: Fri, 17 Mar 2023 09:28:52 +0800 Subject: [PATCH] Fix the issue of the system permission management page retrieving non-existent users. (#4802) * add tech-support-qq-4.png * Update README.md * Enhance the user experience in the scenario of submitting duplicate keys * Modify the key-value conflict exception prompt, adjust the code style * fix(apollo-portal): Fix the issue of the system permission management page retrieving non-existent users. * doc(CHANGES.md): Update CHANGES.md * fix(apollo-portal): Fix the test case testQueryUsersWithRole --------- Co-authored-by: Jason Song --- CHANGES.md | 1 + .../DefaultRolePermissionService.java | 18 +++++++++---- .../RolePermissionServiceTest.java | 27 ++++++++++--------- .../sql/permission/insert-test-roles.sql | 1 + .../sql/permission/insert-test-userroles.sql | 2 ++ 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ae6d41774e8..62f10a6f6a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,7 @@ Apollo 2.2.0 * [[Multi-Database Support][pg] Where clause need escape, otherwise will request postgre use lowwer case](https://github.com/apolloconfig/apollo/pull/4780) * [Misc dependency updates](https://github.com/apolloconfig/apollo/pull/4784) * [Fix the problem that the deletion failure of the system rights management page does not prompt](https://github.com/apolloconfig/apollo/pull/4803) +* [Fix the issue of the system permission management page retrieving non-existent users](https://github.com/apolloconfig/apollo/pull/4802) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1) \ No newline at end of file diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java index fa94c8b24a1..e6a46039776 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java @@ -28,11 +28,14 @@ import com.ctrip.framework.apollo.portal.repository.RoleRepository; import com.ctrip.framework.apollo.portal.repository.UserRoleRepository; import com.ctrip.framework.apollo.portal.service.RolePermissionService; +import com.ctrip.framework.apollo.portal.spi.UserService; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; +import java.util.Comparator; +import java.util.LinkedHashSet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -61,6 +64,8 @@ public class DefaultRolePermissionService implements RolePermissionService { private PortalConfig portalConfig; @Autowired private ConsumerRoleRepository consumerRoleRepository; + @Autowired + private UserService userService; /** * Create role with permissions, note that role name should be unique @@ -149,12 +154,15 @@ public Set queryUsersWithRole(String roleName) { } List userRoles = userRoleRepository.findByRoleId(role.getId()); + List userInfos = userService.findByUserIds(userRoles.stream().map(UserRole::getUserId).collect(Collectors.toList())); + + if(userInfos == null){ + return Collections.emptySet(); + } - return userRoles.stream().map(userRole -> { - UserInfo userInfo = new UserInfo(); - userInfo.setUserId(userRole.getUserId()); - return userInfo; - }).collect(Collectors.toSet()); + return userInfos.stream() + .sorted(Comparator.comparing(UserInfo::getUserId)) + .collect(Collectors.toCollection(LinkedHashSet::new)); } /** diff --git a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java index 132848a28f1..d617a9055ab 100644 --- a/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java +++ b/apollo-portal/src/test/java/com/ctrip/framework/apollo/portal/spi/defaultImpl/RolePermissionServiceTest.java @@ -16,6 +16,10 @@ */ package com.ctrip.framework.apollo.portal.spi.defaultImpl; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import com.ctrip.framework.apollo.common.entity.BaseEntity; import com.ctrip.framework.apollo.portal.AbstractIntegrationTest; import com.ctrip.framework.apollo.portal.entity.bo.UserInfo; @@ -29,19 +33,15 @@ import com.ctrip.framework.apollo.portal.repository.UserRoleRepository; import com.ctrip.framework.apollo.portal.service.RolePermissionService; import com.google.common.collect.Sets; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.jdbc.Sql; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - /** * @author Jason Song(song_s@ctrip.com) */ @@ -285,13 +285,14 @@ public void testRemoveRoleFromUsersWithRoleNotExisted() throws Exception { @Sql(scripts = "/sql/permission/insert-test-userroles.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) public void testQueryUsersWithRole() throws Exception { - String someRoleName = "someRoleName"; - - Set users = rolePermissionService.queryUsersWithRole(someRoleName); + String roleName = "someRoleName"; + Set users = rolePermissionService.queryUsersWithRole(roleName); + Assertions.assertThat(users).isEmpty(); + roleName = "apolloRoleName"; + users = rolePermissionService.queryUsersWithRole(roleName); Set userIds = users.stream().map(UserInfo::getUserId).collect(Collectors.toSet()); - - assertTrue(userIds.containsAll(Sets.newHashSet("someUser", "anotherUser"))); + assertTrue(userIds.containsAll(Sets.newHashSet("apollo"))); } @Test diff --git a/apollo-portal/src/test/resources/sql/permission/insert-test-roles.sql b/apollo-portal/src/test/resources/sql/permission/insert-test-roles.sql index 7ba8f2bdeec..53c61eef8be 100644 --- a/apollo-portal/src/test/resources/sql/permission/insert-test-roles.sql +++ b/apollo-portal/src/test/resources/sql/permission/insert-test-roles.sql @@ -15,3 +15,4 @@ -- INSERT INTO "Role" (`Id`, `RoleName`) VALUES (990, 'someRoleName'); INSERT INTO "Role" (`Id`, `RoleName`) VALUES (991, 'anotherRoleName'); +INSERT INTO "Role" (`Id`, `RoleName`) VALUES (992, 'apolloRoleName'); diff --git a/apollo-portal/src/test/resources/sql/permission/insert-test-userroles.sql b/apollo-portal/src/test/resources/sql/permission/insert-test-userroles.sql index 0c3917f4798..04a66840b80 100644 --- a/apollo-portal/src/test/resources/sql/permission/insert-test-userroles.sql +++ b/apollo-portal/src/test/resources/sql/permission/insert-test-userroles.sql @@ -17,3 +17,5 @@ INSERT INTO "UserRole" (`Id`, `UserId`, `RoleId`, `DataChange_CreatedBy`, `DataC VALUES (990, 'someUser', 990, 'someOperator', 'someOperator'); INSERT INTO "UserRole" (`Id`, `UserId`, `RoleId`, `DataChange_CreatedBy`, `DataChange_LastModifiedBy`) VALUES (991, 'anotherUser', 990, 'someOperator', 'someOperator'); +INSERT INTO "UserRole" (`Id`, `UserId`, `RoleId`, `DataChange_CreatedBy`, `DataChange_LastModifiedBy`) +VALUES (992, 'apollo', 992, 'someOperator', 'someOperator');