This commit is contained in:
Chuck1sn
2025-05-15 21:48:09 +08:00
parent f33bcf3697
commit 082312db7c
56 changed files with 954 additions and 802 deletions

View File

@@ -32,9 +32,9 @@ public class DepartmentController {
}
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).READ_DEPARTMENT_PERMISSION)")
@GetMapping("/query")
List<Department> queryDepartments() {
return departmentRepository.findAll();
@GetMapping("/query-available")
List<Department> queryAvailableParentDepartmentsBy(@RequestParam(required = false) Long id) {
return departmentService.queryAvailableParentDepartmentsBy(id);
}
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).WRITE_DEPARTMENT_PERMISSION)")

View File

@@ -7,6 +7,7 @@ import com.zl.mjga.dto.permission.PermissionBindDto;
import com.zl.mjga.dto.position.PositionBindDto;
import com.zl.mjga.dto.role.RoleBindDto;
import com.zl.mjga.dto.urp.*;
import com.zl.mjga.repository.PermissionRepository;
import com.zl.mjga.repository.RoleRepository;
import com.zl.mjga.repository.UserRepository;
import com.zl.mjga.service.IdentityAccessService;
@@ -17,6 +18,7 @@ import lombok.RequiredArgsConstructor;
import org.jooq.generated.mjga.tables.pojos.User;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.DisabledException;
import org.springframework.web.bind.annotation.*;
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@@ -28,11 +30,15 @@ public class IdentityAccessController {
private final IdentityAccessService identityAccessService;
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final PermissionRepository permissionRepository;
@GetMapping("/me")
UserRolePermissionDto currentUser(Principal principal) {
String name = principal.getName();
User user = userRepository.fetchOneByUsername(name);
if (!user.getEnable()) {
throw new DisabledException(String.format("用户 %s 被禁用", name));
}
return identityAccessService.queryUniqueUserWithRolePermission(user.getId());
}
@@ -46,7 +52,7 @@ public class IdentityAccessController {
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).WRITE_USER_ROLE_PERMISSION)")
@PostMapping("/user")
void upsertUser(@RequestBody UserUpsertDto userUpsertDto) {
void upsertUser(@RequestBody @Valid UserUpsertDto userUpsertDto) {
identityAccessService.upsertUser(userUpsertDto);
}
@@ -56,7 +62,7 @@ public class IdentityAccessController {
return identityAccessService.queryUniqueUserWithRolePermission(userId);
}
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).WRITE_USER_ROLE_PERMISSION)")
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).DELETE_USER_ROLE_PERMISSION)")
@DeleteMapping("/user")
void deleteUser(@RequestParam Long userId) {
userRepository.deleteById(userId);
@@ -89,7 +95,7 @@ public class IdentityAccessController {
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).WRITE_USER_ROLE_PERMISSION)")
@DeleteMapping("/permission")
void deletePermission(@RequestParam Long permissionId) {
roleRepository.deleteById(permissionId);
permissionRepository.deleteById(permissionId);
}
@PreAuthorize("hasAuthority(T(com.zl.mjga.model.urp.EPermission).READ_USER_ROLE_PERMISSION)")

View File

@@ -0,0 +1,18 @@
package com.zl.mjga.dto.department;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@EqualsAndHashCode
public class DepartmentWithParentDto {
@NotNull private Long id;
@NotEmpty private String name;
@NotEmpty Long parentId;
@NotEmpty String parentName;
@NotEmpty String path;
}

View File

@@ -11,7 +11,8 @@ import lombok.NoArgsConstructor;
@Data
public class UserUpsertDto {
private Long id;
@NotEmpty private String username;
@NotEmpty
private String username;
private String password;
@NotNull private Boolean enable;
}

View File

@@ -1,6 +1,7 @@
package com.zl.mjga.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.*;
import org.springframework.lang.Nullable;
import org.springframework.security.access.AccessDeniedException;
@@ -72,13 +73,31 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
throw ex;
}
@ExceptionHandler(value = {DuplicateKeyException.class})
public ResponseEntity<Object> handleDuplicateException(
DuplicateKeyException ex, WebRequest request) {
log.error("DuplicateKeyException Handled ===> ", ex);
ErrorResponseException errorResponseException =
new ErrorResponseException(
HttpStatus.INTERNAL_SERVER_ERROR,
ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR, "您输入的内容已存在,请检查后重新提交"),
ex.getCause());
return handleExceptionInternal(
errorResponseException,
errorResponseException.getBody(),
errorResponseException.getHeaders(),
errorResponseException.getStatusCode(),
request);
}
@ExceptionHandler(value = {Throwable.class})
public ResponseEntity<Object> handleException(Throwable ex, WebRequest request) {
log.error("System Error Handled ===> ", ex);
ErrorResponseException errorResponseException =
new ErrorResponseException(
HttpStatus.INTERNAL_SERVER_ERROR,
ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "System Error"),
ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "发生系统异常,请联系管理员"),
ex.getCause());
return handleExceptionInternal(
errorResponseException,

View File

@@ -8,5 +8,6 @@ public enum EPermission {
READ_SCHEDULER_PERMISSION,
WRITE_SCHEDULER_PERMISSION,
WRITE_USER_ROLE_PERMISSION,
DELETE_USER_ROLE_PERMISSION,
READ_USER_ROLE_PERMISSION
}

View File

@@ -1,15 +1,16 @@
package com.zl.mjga.repository;
import static org.jooq.generated.mjga.Tables.*;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.noField;
import static org.jooq.impl.DSL.*;
import static org.jooq.impl.SQLDataType.VARCHAR;
import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.department.DepartmentQueryDto;
import com.zl.mjga.dto.department.DepartmentWithParentDto;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.generated.mjga.tables.Department;
import org.jooq.generated.mjga.tables.daos.DepartmentDao;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,9 +24,39 @@ public class DepartmentRepository extends DepartmentDao {
super(configuration);
}
public List<DepartmentWithParentDto> queryDepartmentAndSubsBy(Long id) {
CommonTableExpression<?> cte =
name("parent_department")
.fields("id", "name", "parent_name", "parent_id", "path")
.as(
select(
DEPARTMENT.ID,
DEPARTMENT.NAME,
DEPARTMENT.NAME,
DEPARTMENT.PARENT_ID,
DEPARTMENT.NAME.cast(VARCHAR))
.from(DEPARTMENT)
.where(DEPARTMENT.ID.eq(id))
.unionAll(
select(
DEPARTMENT.ID,
DEPARTMENT.NAME,
field(name("parent_department", "name"), VARCHAR),
DEPARTMENT.PARENT_ID,
field(name("parent_department", "path"), VARCHAR)
.concat("->")
.concat(DEPARTMENT.NAME))
.from(table(name("parent_department")))
.join(DEPARTMENT)
.on(
field(name("parent_department", "id"), Long.class)
.eq(DEPARTMENT.PARENT_ID))));
return ctx().withRecursive(cte).selectFrom(cte).fetch().into(DepartmentWithParentDto.class);
}
public Result<Record> pageFetchBy(
PageRequestDto pageRequestDto, DepartmentQueryDto departmentQueryDto) {
Department parent = DEPARTMENT.as("parent");
org.jooq.generated.mjga.tables.Department parent = DEPARTMENT.as("parent");
return ctx()
.select(
DEPARTMENT.asterisk(),
@@ -36,7 +67,7 @@ public class DepartmentRepository extends DepartmentDao {
true)
.otherwise(false)
.as("is_bound")
: noField(),
: noCondition(),
DSL.count().over().as("total_department").convertFrom(Long::valueOf))
.from(DEPARTMENT)
.leftJoin(parent)

View File

@@ -36,7 +36,7 @@ public class PermissionRepository extends PermissionDao {
true)
.otherwise(false)
.as("is_bound")
: noField(),
: noCondition(),
DSL.count().over().as("total_permission"))
.from(PERMISSION)
.where(

View File

@@ -31,7 +31,7 @@ public class PositionRepository extends PositionDao {
? DSL.when(POSITION.ID.in(selectUsersPosition(positionQueryDto.getUserId())), true)
.otherwise(false)
.as("is_bound")
: noField(),
: noCondition(),
DSL.count().over().as("total_position").convertFrom(Long::valueOf))
.from(POSITION)
.where(

View File

@@ -41,7 +41,7 @@ public class RoleRepository extends RoleDao {
? when(ROLE.ID.in(selectUsersRoleIds(roleQueryDto.getUserId())), true)
.otherwise(false)
.as("is_bound")
: noField(),
: noCondition(),
multiset(select(ROLE.permission().asterisk()).from(ROLE.permission()))
.convertFrom(r -> r.into(Permission.class))
.as("permissions"),

View File

@@ -6,12 +6,14 @@ import com.zl.mjga.dto.PageRequestDto;
import com.zl.mjga.dto.PageResponseDto;
import com.zl.mjga.dto.department.DepartmentQueryDto;
import com.zl.mjga.dto.department.DepartmentRespDto;
import com.zl.mjga.dto.department.DepartmentWithParentDto;
import com.zl.mjga.repository.DepartmentRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.generated.mjga.tables.pojos.Department;
import org.springframework.stereotype.Service;
@Service
@@ -21,6 +23,30 @@ public class DepartmentService {
private final DepartmentRepository departmentRepository;
public List<Department> queryAvailableParentDepartmentsBy(Long id) {
List<Department> allDepartments = departmentRepository.findAll();
if (id != null) {
List<DepartmentWithParentDto> departmentWithParentList = queryDepartmentAndSubsBy(id);
allDepartments.removeIf(
department -> {
return departmentWithParentList.stream()
.anyMatch(
(departmentWithParentDto -> {
return departmentWithParentDto.getId().equals(department.getId());
}));
});
}
return allDepartments;
}
public void upsertDepartment(Department department) {
departmentRepository.merge(department);
}
public List<DepartmentWithParentDto> queryDepartmentAndSubsBy(Long id) {
return departmentRepository.queryDepartmentAndSubsBy(id);
}
public PageResponseDto<List<DepartmentRespDto>> pageQueryDepartment(
PageRequestDto pageRequestDto, DepartmentQueryDto departmentQueryDto) {
Result<Record> records = departmentRepository.pageFetchBy(pageRequestDto, departmentQueryDto);

View File

@@ -27,10 +27,13 @@ public class SignService {
public Long signIn(SignInDto signInDto) {
User user = userRepository.fetchOneByUsername(signInDto.getUsername());
if (user == null) {
throw new BusinessException(String.format("%s user not found", signInDto.getUsername()));
throw new BusinessException("用户名不存在");
}
if (!passwordEncoder.matches(signInDto.getPassword(), user.getPassword())) {
throw new BusinessException("password invalid");
throw new BusinessException("密码错误");
}
if (!user.getEnable()) {
throw new BusinessException("用户被禁用");
}
return user.getId();
}
@@ -38,8 +41,7 @@ public class SignService {
@Transactional(rollbackFor = Throwable.class)
public void signUp(SignUpDto signUpDto) {
if (identityAccessService.isUsernameDuplicate(signUpDto.getUsername())) {
throw new BusinessException(
String.format("username %s already exist", signUpDto.getUsername()));
throw new BusinessException("用户名已存在");
}
User user = new User();
user.setUsername(signUpDto.getUsername());