Skip to content

Commit

Permalink
Fix the issue of the system permission management page retrieving non…
Browse files Browse the repository at this point in the history
…-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 <[email protected]>
  • Loading branch information
klboke and nobodyiam authored Mar 17, 2023
1 parent c6dfe76 commit bb15bec
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -149,12 +154,15 @@ public Set<UserInfo> queryUsersWithRole(String roleName) {
}

List<UserRole> userRoles = userRoleRepository.findByRoleId(role.getId());
List<UserInfo> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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([email protected])
*/
Expand Down Expand Up @@ -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<UserInfo> users = rolePermissionService.queryUsersWithRole(someRoleName);
String roleName = "someRoleName";
Set<UserInfo> users = rolePermissionService.queryUsersWithRole(roleName);
Assertions.assertThat(users).isEmpty();

roleName = "apolloRoleName";
users = rolePermissionService.queryUsersWithRole(roleName);
Set<String> userIds = users.stream().map(UserInfo::getUserId).collect(Collectors.toSet());

assertTrue(userIds.containsAll(Sets.newHashSet("someUser", "anotherUser")));
assertTrue(userIds.containsAll(Sets.newHashSet("apollo")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit bb15bec

Please sign in to comment.