|
|
@@ -3,14 +3,11 @@ package cn.reghao.autodop.dmaster.auth.service;
|
|
|
import cn.reghao.autodop.common.utils.security.Cryptor;
|
|
|
import cn.reghao.autodop.common.utils.security.Md5Cryptor;
|
|
|
import cn.reghao.autodop.common.utils.security.Salt;
|
|
|
-import cn.reghao.autodop.dmaster.auth.db.RoleCrud;
|
|
|
-import cn.reghao.autodop.dmaster.auth.db.UserCrud;
|
|
|
import cn.reghao.autodop.dmaster.auth.db.query.RoleQuery;
|
|
|
-import cn.reghao.autodop.dmaster.auth.db.query.UserQuery;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.GrantedAuthorityImpl;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.Role;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.User;
|
|
|
-import cn.reghao.autodop.dmaster.auth.repository.RoleRepository;
|
|
|
+import cn.reghao.autodop.dmaster.auth.entity.UserInfo;
|
|
|
import cn.reghao.autodop.dmaster.auth.repository.UserRepository;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
@@ -30,17 +27,12 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
private UserRepository userRepository;
|
|
|
- private RoleRepository roleRepository;
|
|
|
- private UserQuery userQuery;
|
|
|
private RoleQuery roleQuery;
|
|
|
private Cryptor cryptor;
|
|
|
|
|
|
- public UserServiceImpl(UserRepository userRepository, RoleRepository roleRepository,
|
|
|
- UserQuery userQuery, RoleQuery roleQuery)
|
|
|
+ public UserServiceImpl(UserRepository userRepository, RoleQuery roleQuery)
|
|
|
throws NoSuchAlgorithmException {
|
|
|
this.userRepository = userRepository;
|
|
|
- this.roleRepository = roleRepository;
|
|
|
- this.userQuery = userQuery;
|
|
|
this.roleQuery = roleQuery;
|
|
|
this.cryptor = new Md5Cryptor();
|
|
|
}
|
|
|
@@ -54,6 +46,10 @@ public class UserServiceImpl implements UserService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ Set<GrantedAuthorityImpl> authorities = user.getRoles().stream()
|
|
|
+ .map(role -> new GrantedAuthorityImpl(role.getTitle()))
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ user.setAuthorities(authorities);
|
|
|
setEncryptPassword(user);
|
|
|
user.setAvatarUrl("/imgs/avatar/default.png");
|
|
|
userRepository.save(user);
|
|
|
@@ -69,10 +65,8 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
@Override
|
|
|
public void modifyUserPassword(Integer userId, String newPassword) {
|
|
|
- Optional<User> userOptional = userRepository.findById(userId);
|
|
|
- if (userOptional.isPresent()) {
|
|
|
- User userEntity = userOptional.get();
|
|
|
- userEntity.setPassword(newPassword);
|
|
|
+ User userEntity = getUser(userId);
|
|
|
+ if (userEntity != null) {
|
|
|
setEncryptPassword(userEntity);
|
|
|
userRepository.save(userEntity);
|
|
|
} else {
|
|
|
@@ -81,23 +75,22 @@ public class UserServiceImpl implements UserService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void modifyUserInfo(User user) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void setUserStatus(Integer userId, Integer status) {
|
|
|
+ public void modifyUserInfo(UserInfo userInfo) {
|
|
|
+ User userEntity = getUser(userInfo.getUserId());
|
|
|
+ if (userEntity == null) {
|
|
|
+ log.error("用户不存在...");
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void setUserRoles(Integer userId, List<Role> roles) {
|
|
|
- Optional<User> userOptional = userRepository.findById(userId);
|
|
|
- if (userOptional.isEmpty()) {
|
|
|
+ public void setUserRoles(Integer userId, Set<Role> roles) {
|
|
|
+ User userEntity = getUser(userId);
|
|
|
+ if (userEntity == null) {
|
|
|
log.error("用户不存在...");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- User userEntity = userOptional.get();
|
|
|
List<String> roleTitles = userEntity.getAuthorities().stream()
|
|
|
.map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
|
|
if (roleTitles.isEmpty()) {
|
|
|
@@ -110,5 +103,35 @@ public class UserServiceImpl implements UserService {
|
|
|
}
|
|
|
|
|
|
List<Role> currentRoles = roleQuery.getUserRoles(roleTitles);
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setUserStatus(Integer userId, Boolean enable) {
|
|
|
+ User userEntity = getUser(userId);
|
|
|
+ if (userEntity == null) {
|
|
|
+ log.error("用户不存在...");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ userEntity.setIsEnabled(enable);
|
|
|
+ userRepository.save(userEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteUser(Integer userId) {
|
|
|
+ User userEntity = getUser(userId);
|
|
|
+ if (userEntity != null) {
|
|
|
+ userRepository.delete(userEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private User getUser(Integer userId) {
|
|
|
+ Optional<User> userOptional = userRepository.findById(userId);
|
|
|
+ if (userOptional.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return userOptional.get();
|
|
|
}
|
|
|
}
|