fix: 修复用户导入功能
1. 修改角色/岗位处理逻辑 2. 添加必填字段校验
This commit is contained in:
@ -1,77 +0,0 @@
|
|||||||
name: 构建和部署Spring Boot应用程序
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: ["main"]
|
|
||||||
pull_request:
|
|
||||||
branches: ["main"]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: 检出代码库
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: 设置JDK 17
|
|
||||||
uses: actions/setup-java@v3
|
|
||||||
with:
|
|
||||||
java-version: "17"
|
|
||||||
distribution: "temurin"
|
|
||||||
- name: 缓存Maven包
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/.m2/repository
|
|
||||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-maven-
|
|
||||||
|
|
||||||
- name: 安装依赖
|
|
||||||
run: ./mvnw install -DskipTests
|
|
||||||
|
|
||||||
- name: 运行测试
|
|
||||||
run: ./mvnw test
|
|
||||||
|
|
||||||
- name: 打包应用程序
|
|
||||||
run: ./mvnw package -DskipTests
|
|
||||||
|
|
||||||
- name: 上传构件
|
|
||||||
uses: actions/upload-artifact@v3
|
|
||||||
with:
|
|
||||||
name: spring-boot-app
|
|
||||||
path: |
|
|
||||||
target/jinduguanli-0.0.1-SNAPSHOT.jar
|
|
||||||
src/main/resources/application.yml
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
needs: build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: 下载构件
|
|
||||||
uses: actions/download-artifact@v3
|
|
||||||
with:
|
|
||||||
name: spring-boot-app
|
|
||||||
path: target/
|
|
||||||
|
|
||||||
- name: 通过scp复制文件
|
|
||||||
uses: appleboy/scp-action@v0.1.1
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.REMOTE_HOST }}
|
|
||||||
username: ${{ secrets.REMOTE_USER }}
|
|
||||||
key: ${{ secrets.REMOTE_SSH_KEY }}
|
|
||||||
source: |
|
|
||||||
target/jinduguanli-0.0.1-SNAPSHOT.jar
|
|
||||||
src/main/resources/application.yml
|
|
||||||
target: "/huertian/"
|
|
||||||
|
|
||||||
- name: 通过ssh执行远程命令
|
|
||||||
uses: appleboy/ssh-action@v0.1.8
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.REMOTE_HOST }}
|
|
||||||
username: ${{ secrets.REMOTE_USER }}
|
|
||||||
key: ${{ secrets.REMOTE_SSH_KEY }}
|
|
||||||
script: |
|
|
||||||
cd /huertian/
|
|
||||||
java -jar jinduguanli-0.0.1-SNAPSHOT.jar --spring.config.location=application.yml
|
|
@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@ -35,7 +36,9 @@ public class ImportController {
|
|||||||
private DepartmentService departmentService;
|
private DepartmentService departmentService;
|
||||||
|
|
||||||
@PostMapping("/users")
|
@PostMapping("/users")
|
||||||
public ApiResponse<String> importUsers(@RequestParam("file") MultipartFile file) {
|
public ApiResponse<String> importUsers(
|
||||||
|
@RequestParam("file") MultipartFile file,
|
||||||
|
@RequestHeader("Authorization") String authHeader) {
|
||||||
if (file.isEmpty()) {
|
if (file.isEmpty()) {
|
||||||
return new ApiResponse<>(ErrorCode.INVALID_PARAM, "文件不能为空", null);
|
return new ApiResponse<>(ErrorCode.INVALID_PARAM, "文件不能为空", null);
|
||||||
}
|
}
|
||||||
@ -57,7 +60,8 @@ public class ImportController {
|
|||||||
log.info("第{}行数据:{}", i + 1, userList.get(i));
|
log.info("第{}行数据:{}", i + 1, userList.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return userService.batchImportUsers(userList);
|
String token = authHeader.substring(7);
|
||||||
|
return userService.batchImportUsers(userList, token);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("导入用户数据失败", e);
|
log.error("导入用户数据失败", e);
|
||||||
return new ApiResponse<>(ErrorCode.SYSTEM_ERROR, "导入用户数据失败:" + e.getMessage(), null);
|
return new ApiResponse<>(ErrorCode.SYSTEM_ERROR, "导入用户数据失败:" + e.getMessage(), null);
|
||||||
|
@ -66,6 +66,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String jwt = authHeader.substring(7);
|
final String jwt = authHeader.substring(7);
|
||||||
|
logger.debug("Received JWT token: {}", jwt);
|
||||||
final String username = jwtService.extractUsername(jwt);
|
final String username = jwtService.extractUsername(jwt);
|
||||||
|
|
||||||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||||
|
@ -193,7 +193,7 @@ public class UserService implements UserDetailsService {
|
|||||||
return ApiResponse.success(user);
|
return ApiResponse.success(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApiResponse<String> batchImportUsers(List<UserImportDTO> userList) {
|
public ApiResponse<String> batchImportUsers(List<UserImportDTO> userList, String token) {
|
||||||
if (userList == null || userList.isEmpty()) {
|
if (userList == null || userList.isEmpty()) {
|
||||||
return new ApiResponse<>(ErrorCode.PARAM_ERROR, "导入数据不能为空", null);
|
return new ApiResponse<>(ErrorCode.PARAM_ERROR, "导入数据不能为空", null);
|
||||||
}
|
}
|
||||||
@ -234,8 +234,29 @@ public class UserService implements UserDetailsService {
|
|||||||
user.setEmail(dto.getEmail());
|
user.setEmail(dto.getEmail());
|
||||||
user.setPassword(passwordEncoder.encode(dto.getPassword()));
|
user.setPassword(passwordEncoder.encode(dto.getPassword()));
|
||||||
user.setDepartmentId(department.getId());
|
user.setDepartmentId(department.getId());
|
||||||
user.setRoles(0); // 默认角色
|
|
||||||
user.setJobs(0); // 默认工作
|
// 处理角色和岗位
|
||||||
|
String roleAndJob = dto.getRoleAndJob();
|
||||||
|
if (!StringUtils.isBlank(roleAndJob)) {
|
||||||
|
try {
|
||||||
|
user.setRoles(Integer.parseInt(roleAndJob));
|
||||||
|
user.setJobs(Integer.parseInt(roleAndJob));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
logger.warn("角色/岗位不是有效的数字: {}", roleAndJob);
|
||||||
|
errorMsg.append(String.format("第%d行角色/岗位不是有效的数字;", i + 2));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn("角色/岗位不能为空");
|
||||||
|
errorMsg.append(String.format("第%d行角色/岗位不能为空;", i + 2));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置创建者ID
|
||||||
|
String email = jwtService.extractUsername(token);
|
||||||
|
Optional<User> creator = userRepository.findByEmail(email);
|
||||||
|
user.setCreatorId(creator.map(User::getId).orElse(0L));
|
||||||
|
|
||||||
user.setStatus(1); // 设置状态为正常
|
user.setStatus(1); // 设置状态为正常
|
||||||
user.setCreatedAt(System.currentTimeMillis() / 1000);
|
user.setCreatedAt(System.currentTimeMillis() / 1000);
|
||||||
user.setUpdatedAt(System.currentTimeMillis() / 1000);
|
user.setUpdatedAt(System.currentTimeMillis() / 1000);
|
||||||
|
Reference in New Issue
Block a user