|
|
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -73,17 +74,12 @@ public class UserService {
|
|
|
userRepository.save(user);
|
|
|
}
|
|
|
|
|
|
- public void modifyUserRoles(int userId, Set<Integer> roleIds) {
|
|
|
- List<Integer> list = new ArrayList<>(roleIds);
|
|
|
- // 修改后用户的角色
|
|
|
- List<Role> newRoles = roleCrud.findByRoleIds(list);
|
|
|
-
|
|
|
- // 用户当前的角色
|
|
|
- User user = userRepository.findById(userId);
|
|
|
- List<String> roles = user.getAuthorities().stream()
|
|
|
+ public void modifyUserRoles(User user, Set<Role> roles) {
|
|
|
+ // 用户当前拥有的角色
|
|
|
+ List<String> roleList = user.getAuthorities().stream()
|
|
|
.map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
|
|
- if (roles.isEmpty()) {
|
|
|
- Set<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
+ if (roleList.isEmpty()) {
|
|
|
+ Set<GrantedAuthorityImpl> authorities = roles.stream()
|
|
|
.map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
.collect(Collectors.toSet());
|
|
|
user.setAuthorities(authorities);
|
|
|
@@ -91,22 +87,16 @@ public class UserService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- List<Role> currentRoles = getUserRoles(userId);
|
|
|
- if (currentRoles.isEmpty()) {
|
|
|
- List<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
- .map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
-
|
|
|
+ List<Role> currentRoles = getUserRoles(user.getId());
|
|
|
List<Role> tmp = new ArrayList<>(currentRoles);
|
|
|
tmp.forEach(role -> {
|
|
|
- if (newRoles.remove(role)) {
|
|
|
+ if (roles.remove(role)) {
|
|
|
currentRoles.remove(role);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- if (!newRoles.isEmpty()) {
|
|
|
- Set<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
+ if (!roles.isEmpty()) {
|
|
|
+ Set<GrantedAuthorityImpl> authorities = roles.stream()
|
|
|
.map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
.collect(Collectors.toSet());
|
|
|
user.setAuthorities(authorities);
|