Skip to content

Commit

Permalink
fix search user duplication issue (#4371)
Browse files Browse the repository at this point in the history
* fix search user duplication issue
  • Loading branch information
nobodyiam authored May 25, 2022
1 parent 698cfba commit f929541
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release Notes.
Apollo 2.0.1

------------------
* [Upgrade spring boot to fix search user issue](https://github.com/apolloconfig/apollo/issues/4366)
* [Upgrade spring boot to fix search user issue](https://github.com/apolloconfig/apollo/pull/4366)
* [Fix search user duplication issue](https://github.com/apolloconfig/apollo/pull/4371)
------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/12?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down Expand Up @@ -98,18 +100,22 @@ private List<UserPO> findUsers(String keyword) {
if (StringUtils.isEmpty(keyword)) {
return userRepository.findFirst20ByEnabled(1);
}
List<UserPO> users = new ArrayList<>();
Map<Long, UserPO> users = new HashMap<>();
List<UserPO> byUsername = userRepository
.findByUsernameLikeAndEnabled("%" + keyword + "%", 1);
List<UserPO> byUserDisplayName = userRepository
.findByUserDisplayNameLikeAndEnabled("%" + keyword + "%", 1);
if (!CollectionUtils.isEmpty(byUsername)) {
users.addAll(byUsername);
for (UserPO user : byUsername) {
users.put(user.getId(), user);
}
}
if (!CollectionUtils.isEmpty(byUserDisplayName)) {
users.addAll(byUserDisplayName);
for (UserPO user : byUserDisplayName) {
users.put(user.getId(), user);
}
}
return users;
return new ArrayList<>(users.values());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.ctrip.framework.apollo.portal.repository.UserRepository;
import com.ctrip.framework.apollo.portal.spi.UserService;

import java.util.HashMap;
import java.util.Map;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -91,18 +93,22 @@ private List<UserPO> findUsers(String keyword) {
if (StringUtils.isEmpty(keyword)) {
return userRepository.findFirst20ByEnabled(1);
}
List<UserPO> users = new ArrayList<>();
Map<Long, UserPO> users = new HashMap<>();
List<UserPO> byUsername = userRepository
.findByUsernameLikeAndEnabled("%" + keyword + "%", 1);
List<UserPO> byUserDisplayName = userRepository
.findByUserDisplayNameLikeAndEnabled("%" + keyword + "%", 1);
if (!CollectionUtils.isEmpty(byUsername)) {
users.addAll(byUsername);
for (UserPO user : byUsername) {
users.put(user.getId(), user);
}
}
if (!CollectionUtils.isEmpty(byUserDisplayName)) {
users.addAll(byUserDisplayName);
for (UserPO user : byUserDisplayName) {
users.put(user.getId(), user);
}
}
return users;
return new ArrayList<>(users.values());
}

@Override
Expand Down

0 comments on commit f929541

Please sign in to comment.