|
|
@@ -5,6 +5,7 @@ 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.app.vo.PageList;
|
|
|
+import cn.reghao.autodop.dmaster.auth.entity.GrantedAuthorityImpl;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.Permission;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.Role;
|
|
|
import cn.reghao.autodop.dmaster.auth.entity.UserAuth;
|
|
|
@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -40,7 +43,7 @@ public class UserService {
|
|
|
this.permissionRepository = permissionRepository;
|
|
|
}
|
|
|
|
|
|
- public void addOrUpdate(int type, String json) {
|
|
|
+ public void addOrUpdate(int type, String json) throws Exception {
|
|
|
switch (type) {
|
|
|
case 1:
|
|
|
Role role = (Role) JsonUtil.jsonToObject(json, Role.class);
|
|
|
@@ -76,7 +79,19 @@ public class UserService {
|
|
|
break;
|
|
|
case 3:
|
|
|
UserAuthVO vo = (UserAuthVO) JsonUtil.jsonToObject(json, UserAuthVO.class);
|
|
|
- UserAuth userAuth = vo.to();
|
|
|
+ UserAuth userAuth = UserAuthVO.to(vo);
|
|
|
+ Set<GrantedAuthorityImpl> authorities = userAuth.authorities();
|
|
|
+ Set<GrantedAuthorityImpl> authoritiesEntity = new HashSet<>();
|
|
|
+ for (GrantedAuthorityImpl authority : authorities) {
|
|
|
+ String roleName = authority.getAuthority();
|
|
|
+ Role entity = roleRepository.findRoleByName(roleName);
|
|
|
+ if (entity == null) {
|
|
|
+ throw new Exception(roleName + " 不存在...");
|
|
|
+ }
|
|
|
+ authoritiesEntity.add(new GrantedAuthorityImpl(entity.getName()));
|
|
|
+ }
|
|
|
+ userAuth.setAuthorities(authoritiesEntity);
|
|
|
+
|
|
|
UserAuth authEntity = userAuthRepository.findUserAuthByUsername(userAuth.getUsername());
|
|
|
if (authEntity == null) {
|
|
|
String password = userAuth.getPassword();
|
|
|
@@ -84,19 +99,21 @@ public class UserService {
|
|
|
String encryptedPwd = cryptor.encrypt(password + salt);
|
|
|
userAuth.setPassword(encryptedPwd);
|
|
|
userAuth.setSalt(salt);
|
|
|
- userAuthRepository.save(userAuth);
|
|
|
} else {
|
|
|
+ // 修改密码
|
|
|
if (!authEntity.getPassword().equals(userAuth.getPassword())) {
|
|
|
String password = userAuth.getPassword();
|
|
|
String salt = Salt.get(64);
|
|
|
String encryptedPwd = cryptor.encrypt(password + salt);
|
|
|
- authEntity.setPassword(encryptedPwd);
|
|
|
- authEntity.setSalt(salt);
|
|
|
+ userAuth.setPassword(encryptedPwd);
|
|
|
+ userAuth.setSalt(salt);
|
|
|
+
|
|
|
+ userAuth.setCreateTime(authEntity.getCreateTime());
|
|
|
+ userAuth.setUpdateTime(LocalDateTime.now());
|
|
|
}
|
|
|
- authEntity.setAuthorities(userAuth.authorities());
|
|
|
- authEntity.setUpdateTime(LocalDateTime.now());
|
|
|
- userAuthRepository.save(authEntity);
|
|
|
}
|
|
|
+ userAuth.setIsDelete(false);
|
|
|
+ userAuthRepository.save(userAuth);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
@@ -114,7 +131,7 @@ public class UserService {
|
|
|
rolePageList.setList(rolePage.getContent().stream()
|
|
|
.peek(role -> {
|
|
|
String name = role.getName().split(rolePrefix)[1];
|
|
|
- role.setName(name);
|
|
|
+ role.setName(name.toLowerCase());
|
|
|
})
|
|
|
.collect(Collectors.toList()));
|
|
|
|
|
|
@@ -128,7 +145,7 @@ public class UserService {
|
|
|
.peek(permission -> {
|
|
|
Role role = permission.getRole();
|
|
|
String name = role.getName().split(rolePrefix)[1];
|
|
|
- role.setName(name);
|
|
|
+ role.setName(name.toLowerCase());
|
|
|
}).collect(Collectors.toList()));
|
|
|
return permissionPageList;
|
|
|
case 3:
|
|
|
@@ -137,7 +154,7 @@ public class UserService {
|
|
|
userAuthVOPageList.setTotalSize(userAuthPage.getTotalElements());
|
|
|
userAuthVOPageList.setTotalPages(userAuthPage.getTotalPages());
|
|
|
userAuthVOPageList.setList(userAuthPage.getContent().stream()
|
|
|
- .map(UserAuthVO::new)
|
|
|
+ .map(UserAuthVO::from)
|
|
|
.collect(Collectors.toList()));
|
|
|
return userAuthVOPageList;
|
|
|
default:
|
|
|
@@ -160,4 +177,35 @@ public class UserService {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void checkOrSetAdmin() {
|
|
|
+ String roleName = rolePrefix + "ADMIN";
|
|
|
+ Role roleEntity = roleRepository.findRoleByName(roleName);
|
|
|
+ if (roleEntity == null) {
|
|
|
+ Role role = new Role(roleName);
|
|
|
+ role.setDescription("管理员");
|
|
|
+ role.setIsDelete(false);
|
|
|
+ roleEntity = roleRepository.save(role);
|
|
|
+ }
|
|
|
+
|
|
|
+ String adminName = "admin";
|
|
|
+ UserAuth authEntity = userAuthRepository.findUserAuthByUsername(adminName);
|
|
|
+ if (authEntity == null) {
|
|
|
+ UserAuth userAuth = new UserAuth();
|
|
|
+ userAuth.setUsername("admin");
|
|
|
+ userAuth.setPassword("admin123456");
|
|
|
+ Set<GrantedAuthorityImpl> authorities = new HashSet<>();
|
|
|
+ authorities.add(new GrantedAuthorityImpl(roleEntity.getName()));
|
|
|
+ userAuth.setAuthorities(authorities);
|
|
|
+
|
|
|
+ String password = userAuth.getPassword();
|
|
|
+ String salt = Salt.get(64);
|
|
|
+ String encryptedPwd = cryptor.encrypt(password + salt);
|
|
|
+ userAuth.setPassword(encryptedPwd);
|
|
|
+ userAuth.setSalt(salt);
|
|
|
+
|
|
|
+ userAuth.setIsDelete(false);
|
|
|
+ userAuthRepository.save(userAuth);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|