|
@@ -3,6 +3,7 @@ package cn.reghao.autodop.dmaster.auth.service;
|
|
|
import cn.reghao.autodop.common.utils.security.Cryptor;
|
|
import cn.reghao.autodop.common.utils.security.Cryptor;
|
|
|
import cn.reghao.autodop.common.utils.security.Md5Cryptor;
|
|
import cn.reghao.autodop.common.utils.security.Md5Cryptor;
|
|
|
import cn.reghao.autodop.common.utils.security.Salt;
|
|
import cn.reghao.autodop.common.utils.security.Salt;
|
|
|
|
|
+import cn.reghao.autodop.dmaster.auth.db.RoleCrud;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.GrantedAuthorityImpl;
|
|
import cn.reghao.autodop.dmaster.auth.entity.GrantedAuthorityImpl;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.Role;
|
|
import cn.reghao.autodop.dmaster.auth.entity.Role;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.User;
|
|
import cn.reghao.autodop.dmaster.auth.entity.User;
|
|
@@ -12,6 +13,7 @@ import cn.reghao.autodop.dmaster.common.db.PageList;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
@@ -26,14 +28,14 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
*/
|
|
|
@Service
|
|
@Service
|
|
|
public class UserService {
|
|
public class UserService {
|
|
|
- private final RoleRepository roleRepository;
|
|
|
|
|
private final UserRepository userRepository;
|
|
private final UserRepository userRepository;
|
|
|
|
|
+ private final RoleCrud roleCrud;
|
|
|
private final Cryptor cryptor;
|
|
private final Cryptor cryptor;
|
|
|
|
|
|
|
|
- public UserService(RoleRepository roleRepository, UserRepository userRepository)
|
|
|
|
|
|
|
+ public UserService(UserRepository userRepository, RoleCrud roleCrud)
|
|
|
throws NoSuchAlgorithmException {
|
|
throws NoSuchAlgorithmException {
|
|
|
- this.roleRepository = roleRepository;
|
|
|
|
|
this.userRepository = userRepository;
|
|
this.userRepository = userRepository;
|
|
|
|
|
+ this.roleCrud = roleCrud;
|
|
|
this.cryptor = new Md5Cryptor();
|
|
this.cryptor = new Md5Cryptor();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -72,17 +74,28 @@ public class UserService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void modifyUserRoles(int userId, Set<Integer> roleIds) {
|
|
public void modifyUserRoles(int userId, Set<Integer> roleIds) {
|
|
|
- /*List<Integer> list = new ArrayList<>(roleIds);
|
|
|
|
|
|
|
+ List<Integer> list = new ArrayList<>(roleIds);
|
|
|
// 修改后用户的角色
|
|
// 修改后用户的角色
|
|
|
- List<Role> newRoles = roleRepository.findByRoleIds(list);
|
|
|
|
|
|
|
+ List<Role> newRoles = roleCrud.findByRoleIds(list);
|
|
|
|
|
+
|
|
|
// 用户当前的角色
|
|
// 用户当前的角色
|
|
|
|
|
+ User user = userRepository.findById(userId);
|
|
|
|
|
+ List<String> roles = user.getAuthorities().stream()
|
|
|
|
|
+ .map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
|
|
|
|
+ if (roles.isEmpty()) {
|
|
|
|
|
+ Set<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
|
|
+ .map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ user.setAuthorities(authorities);
|
|
|
|
|
+ userRepository.save(user);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
List<Role> currentRoles = getUserRoles(userId);
|
|
List<Role> currentRoles = getUserRoles(userId);
|
|
|
if (currentRoles.isEmpty()) {
|
|
if (currentRoles.isEmpty()) {
|
|
|
List<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
List<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
- .map(role -> new GrantedAuthorityImpl(userId, role.getTitle()))
|
|
|
|
|
|
|
+ .map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
- authorityMapper.batchInsert(authorities);
|
|
|
|
|
- return;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
List<Role> tmp = new ArrayList<>(currentRoles);
|
|
List<Role> tmp = new ArrayList<>(currentRoles);
|
|
@@ -93,20 +106,23 @@ public class UserService {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
if (!newRoles.isEmpty()) {
|
|
if (!newRoles.isEmpty()) {
|
|
|
- List<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
|
|
- .map(role -> new GrantedAuthorityImpl(userId, role.getTitle()))
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- authorityMapper.batchInsert(authorities);
|
|
|
|
|
|
|
+ Set<GrantedAuthorityImpl> authorities = newRoles.stream()
|
|
|
|
|
+ .map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ user.setAuthorities(authorities);
|
|
|
|
|
+ userRepository.save(user);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!currentRoles.isEmpty()) {
|
|
if (!currentRoles.isEmpty()) {
|
|
|
List<String> ids = currentRoles.stream().map(Role::getTitle).collect(Collectors.toList());
|
|
List<String> ids = currentRoles.stream().map(Role::getTitle).collect(Collectors.toList());
|
|
|
- //authorityMapper.batchDelete(ids);
|
|
|
|
|
- }*/
|
|
|
|
|
|
|
+ // TODO
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<Role> getUserRoles(int userId) {
|
|
public List<Role> getUserRoles(int userId) {
|
|
|
- //return userRepository.findUserRoles(userId);
|
|
|
|
|
- return new ArrayList<>();
|
|
|
|
|
|
|
+ User user = userRepository.findById(userId);
|
|
|
|
|
+ List<String> roles = user.getAuthorities().stream()
|
|
|
|
|
+ .map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
|
|
|
|
+ return roleCrud.findUserRoles(roles);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|