feat: 添加权限

This commit is contained in:
siujamo
2025-12-31 16:42:08 +08:00
parent 382a6c177f
commit 9b8d276e0b
5 changed files with 71 additions and 5 deletions
@@ -1,6 +1,7 @@
package com.onixbyte.helix.controller;
import com.onixbyte.helix.domain.entity.Authority;
import com.onixbyte.helix.domain.web.request.AddAuthorityRequest;
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
import com.onixbyte.helix.service.AuthorityService;
import org.springframework.data.domain.Page;
@@ -30,4 +31,9 @@ public class AuthorityController {
var pageRequest = PageRequest.of(pageNum - 1, pageSize);
return authorityService.getAuthorities(pageRequest, request);
}
@PostMapping
public Authority addAuthority(@Validated @RequestBody AddAuthorityRequest request) {
return authorityService.addAuthority(request);
}
}
@@ -0,0 +1,4 @@
package com.onixbyte.helix.domain.database.query.wrapper;
public class QueryAuthorityWrapper {
}
@@ -0,0 +1,15 @@
package com.onixbyte.helix.domain.web.request;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
public record AddAuthorityRequest(
@NotNull @NotBlank(message = "权限编码不能为空") String code,
@NotNull @NotBlank(message = "权限名称不能为空") String name,
String description,
@Pattern(
regexp = "^(ACTIVE|INACTIVE)?$",
message = "状态错误") String status
) {
}
@@ -1,11 +1,15 @@
package com.onixbyte.helix.manager;
import com.onixbyte.helix.repository.AuthorityRepository;
import com.onixbyte.helix.shared.CacheName;
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
import com.onixbyte.helix.domain.entity.Authority;
import com.onixbyte.helix.mapper.AuthorityMapper;
import com.onixbyte.helix.repository.AuthorityRepository;
import com.onixbyte.helix.shared.CacheName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
@@ -15,6 +19,7 @@ import java.util.List;
@Component
public class AuthorityManager {
private static final Logger log = LoggerFactory.getLogger(AuthorityManager.class);
private final AuthorityMapper authorityMapper;
private final AuthorityRepository authorityRepository;
@@ -29,7 +34,19 @@ public class AuthorityManager {
return authorityMapper.selectByUserId(userId);
}
public Page<Authority> selectAll(Pageable pageable) {
public Page<Authority> selectAll(Pageable pageable, QueryAuthorityWrapper wrapper) {
return authorityRepository.findAll(pageable);
}
public boolean existsByCode(Authority authority) {
return authorityRepository.exists(Example.of(Authority
.builder()
.code(authority.getCode())
.build()
));
}
public Authority save(Authority authority) {
return authorityRepository.save(authority);
}
}
@@ -1,13 +1,18 @@
package com.onixbyte.helix.service;
import com.onixbyte.helix.domain.database.query.wrapper.QueryAuthorityWrapper;
import com.onixbyte.helix.domain.entity.Authority;
import com.onixbyte.helix.domain.web.request.AddAuthorityRequest;
import com.onixbyte.helix.domain.web.request.QueryAuthorityRequest;
import com.onixbyte.helix.enumeration.Status;
import com.onixbyte.helix.exception.BizException;
import com.onixbyte.helix.manager.AuthorityManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class AuthorityService {
@@ -19,6 +24,25 @@ public class AuthorityService {
}
public Page<Authority> getAuthorities(Pageable pageable, QueryAuthorityRequest request) {
return authorityManager.selectAll(pageable);
var wrapper = new QueryAuthorityWrapper();
return authorityManager.selectAll(pageable, wrapper);
}
public Authority addAuthority(AddAuthorityRequest request) {
var authority = Authority.builder()
.code(request.code())
.name(request.name())
.description(request.description())
.status(Optional.ofNullable(request.status())
.map(Status::valueOf)
.orElse(Status.ACTIVE))
.build();
if (authorityManager.existsByCode(authority)) {
throw new BizException(HttpStatus.CONFLICT, "权限编码 `" + authority.getCode() + "` 已被使用");
}
return authorityManager.save(authority);
}
}