Compare commits
100
Commits
23204f3a48
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d002f96c3 | ||
|
|
684d7e19dc | ||
|
|
19544b1fd2 | ||
|
|
34d9b189d2 | ||
|
|
247535000b | ||
|
|
57422a64f2 | ||
|
|
164dc4aef1 | ||
|
|
765561aad2 | ||
|
|
9fa1e35044 | ||
|
|
85966705b3 | ||
|
|
bef66336bc | ||
|
|
ebb9f633a4 | ||
|
|
264d5495e6 | ||
|
|
b800069c82 | ||
|
|
79059e7818 | ||
|
|
4b943e887d | ||
|
|
359eac8985 | ||
|
|
1531c2498b | ||
|
|
60e80dda87 | ||
|
|
445da5f093 | ||
|
|
b62c7f2495 | ||
|
|
2b7c94e7c3 | ||
|
|
470b586085 | ||
|
|
3d0f9b9d1b | ||
|
|
634de93ffe | ||
|
|
996830bec7 | ||
|
|
cd88f94277 | ||
|
|
47970cd8ff | ||
|
|
ada3fbf9cb | ||
|
|
f6c99bd262 | ||
|
|
1241f19c16 | ||
|
|
d7a7a88232 | ||
|
|
7743b1bab2 | ||
|
|
982b1ee3e4 | ||
|
|
05e28c2c6d | ||
|
|
c18d0e8a9f | ||
|
|
1b221b13d1 | ||
|
|
135996ce19 | ||
|
|
6b85cddede | ||
|
|
e486f850f2 | ||
|
|
76c2f3a5fd | ||
|
|
bb542e7510 | ||
|
|
eba4461381 | ||
|
|
f5ad238015 | ||
|
|
4bdb24e9ac | ||
|
|
425922fd04 | ||
|
|
d65424e5bf | ||
|
|
2a35793baf | ||
|
|
d22df698b6 | ||
|
|
1771bf9909 | ||
|
|
15d69f9361 | ||
|
|
2b4089ef59 | ||
|
|
20c3e1f422 | ||
|
|
3b19d9e930 | ||
|
|
5fd11e9caf | ||
|
|
0b98dcdfe9 | ||
|
|
b373cbbe46 | ||
|
|
acd97c7b9b | ||
|
|
118bdb1666 | ||
|
|
944de228f7 | ||
|
|
25c9cd4dfc | ||
|
|
882cd226ec | ||
|
|
feb4d7af66 | ||
|
|
d75ab1ed5f | ||
|
|
a501eb7443 | ||
|
|
19941d9480 | ||
|
|
cd3d522d4f | ||
|
|
181c5078e9 | ||
|
|
e97f46e6b4 | ||
|
|
c0f89deb83 | ||
|
|
ce695463d3 | ||
|
|
83c683d473 | ||
|
|
9555474b0b | ||
|
|
a150f9e017 | ||
|
|
ecd2fa1c33 | ||
|
|
e3236d262d | ||
|
|
c8e2e16ba5 | ||
|
|
99b44faf09 | ||
|
|
b3e3cbf84b | ||
|
|
0aee11d824 | ||
|
|
fcf358a768 | ||
|
|
619369104c | ||
|
|
4f1408b9d0 | ||
|
|
029eea70cc | ||
|
|
22b1e65385 | ||
|
|
2170c48763 | ||
|
|
c2d1c48aae | ||
|
|
e14958c643 | ||
|
|
da288338cd | ||
|
|
ea430f2468 | ||
|
|
f76f79744a | ||
|
|
19daaff907 | ||
|
|
db94d8f932 | ||
|
|
53618fe362 | ||
|
|
523edffc4e | ||
|
|
c7ddb1a8b0 | ||
|
|
45c9fc814a | ||
|
|
407dc13ac4 | ||
|
|
0c06bf16c2 | ||
|
|
1db39e77d3 |
@@ -539,9 +539,6 @@
|
||||
| POST | `/user/login/sms` | loginBySms | - | SmsCaptchaVerifyRequest_Public | LoginResponse_Public | user-controller |
|
||||
| GET/HEAD/POST/PUT/PATCH/DELETE/OPTIONS | `/user/login/token` | token | - | - | LoginResponse_Public | user-controller |
|
||||
| POST | `/user/logout` | logout | - | - | - | user-controller |
|
||||
| POST | `/user/permission/create` | create | - | Permission | - | permission-controller |
|
||||
| POST | `/user/permission/create/batch` | createBatch | - | BatchCreateReq | - | permission-controller |
|
||||
| POST | `/user/permission/delete` | delete | id* | - | - | permission-controller |
|
||||
| POST | `/user/permission/grantable/list` | listGrantable | moduleCode* | - | Array<Permission> | permission-controller |
|
||||
| POST | `/user/permission/list` | list | - | PagePermission | PageResultPermission | permission-controller |
|
||||
| POST | `/user/permission/update` | update | - | Permission | - | permission-controller |
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# Lombok 全局配置
|
||||
|
||||
config.stopBubbling = true
|
||||
|
||||
# 将字段上的 @Qualifier 复制到 @RequiredArgsConstructor 生成的构造器参数,
|
||||
# 避免多个同泛型 Redis Bean 注入歧义
|
||||
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
<groupId>com.imyeyu.timiserverapi</groupId>
|
||||
<artifactId>TimiServerAPI</artifactId>
|
||||
<version>1.0.81</version>
|
||||
<version>1.0.82</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>TimiServerAPI</name>
|
||||
<description>imyeyu.com API</description>
|
||||
|
||||
@@ -11,10 +11,8 @@ import lombok.AllArgsConstructor;
|
||||
@AllArgsConstructor
|
||||
public enum CorePermissionCode implements BuiltinPermissionCode {
|
||||
|
||||
PERMISSION_CREATE("PERMISSION:CREATE", "权限创建", false, true),
|
||||
PERMISSION_READ("PERMISSION:READ", "权限读取", false, true),
|
||||
PERMISSION_UPDATE("PERMISSION:UPDATE", "权限修改", false, true),
|
||||
PERMISSION_DELETE("PERMISSION:DELETE", "权限删除", false, true),
|
||||
|
||||
ROLE_CREATE("ROLE:CREATE", "角色创建", false, true),
|
||||
ROLE_READ("ROLE:READ", "角色读取", false, true),
|
||||
@@ -29,10 +27,8 @@ public enum CorePermissionCode implements BuiltinPermissionCode {
|
||||
ROLE_PERMISSION_INHERIT_READ("ROLE_PERMISSION_INHERIT:READ", "角色权限继承读取", false, true),
|
||||
ROLE_PERMISSION_INHERIT_DELETE("ROLE_PERMISSION_INHERIT:DELETE", "角色权限继承删除", false, true),
|
||||
|
||||
SETTING_CREATE("SETTING:CREATE", "设置创建", false, true),
|
||||
SETTING_READ("SETTING:READ", "设置读取", false, true),
|
||||
SETTING_UPDATE("SETTING:UPDATE", "设置修改", false, true),
|
||||
SETTING_DELETE("SETTING:DELETE", "设置删除", false, true),
|
||||
|
||||
MULTILINGUAL_CREATE("MULTILINGUAL:CREATE", "多语言创建", false, true),
|
||||
MULTILINGUAL_READ("MULTILINGUAL:READ", "多语言读取", false, true),
|
||||
|
||||
@@ -94,23 +94,6 @@ public class SettingController {
|
||||
return service.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建配置
|
||||
*
|
||||
* @param setting 配置
|
||||
* @return 配置
|
||||
*/
|
||||
@Operation(summary = "创建配置", description = "创建配置,需要 SETTING:CREATE 权限。")
|
||||
@AOPLog
|
||||
@JsonView(ResponseView.Admin.class)
|
||||
@RequireCorePermission(CorePermissionCode.SETTING_CREATE)
|
||||
@RequestRateLimit
|
||||
@PostMapping("/create")
|
||||
public Setting create(@RequestBody Setting setting) {
|
||||
service.create(setting);
|
||||
return setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新配置
|
||||
*
|
||||
@@ -124,20 +107,4 @@ public class SettingController {
|
||||
public void update(@RequestBody Setting setting) {
|
||||
service.update(setting);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除配置
|
||||
*
|
||||
* @param id 配置 ID
|
||||
*/
|
||||
@Operation(summary = "删除配置", description = "按配置 ID 删除配置,需要 SETTING:DELETE 权限。")
|
||||
@AOPLog
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.SETTING_DELETE)
|
||||
@PostMapping("/delete")
|
||||
public void delete(
|
||||
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "配置 ID", required = true)
|
||||
@RequestBody String id) {
|
||||
service.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -46,11 +46,11 @@ public class GaoAiOperationServiceImplement implements GaoAiOperationService {
|
||||
private final ObjectMapper jackson;
|
||||
private final List<GaoAiOperationHandler> handlerList;
|
||||
|
||||
// @Qualifier("redisLocker")
|
||||
private Redis<String, Integer> locker;
|
||||
@Qualifier("redisLocker")
|
||||
private final Redis<String, Integer> locker;
|
||||
|
||||
// @Qualifier("redisRateLimit")
|
||||
private Redis<String, Integer> rateLimitRedis;
|
||||
@Qualifier("redisRateLimit")
|
||||
private final Redis<String, Integer> rateLimitRedis;
|
||||
|
||||
private Map<GaoAiOperation, GaoAiOperationHandler> handlerMap;
|
||||
|
||||
|
||||
+6
-6
@@ -30,14 +30,14 @@ public class GaoAiPendingOperationServiceImplement implements GaoAiPendingOperat
|
||||
|
||||
private final GaoAiProperties properties;
|
||||
|
||||
// @Qualifier("redisGaoAiOperation")
|
||||
private Redis<String, GaoAiPendingOperation> redis;
|
||||
@Qualifier("redisGaoAiOperation")
|
||||
private final Redis<String, GaoAiPendingOperation> redis;
|
||||
|
||||
// @Qualifier("redisGaoAiOperationIndex")
|
||||
private Redis<String, String> indexRedis;
|
||||
@Qualifier("redisGaoAiOperationIndex")
|
||||
private final Redis<String, String> indexRedis;
|
||||
|
||||
// @Qualifier("redisLocker")
|
||||
private Redis<String, Integer> locker;
|
||||
@Qualifier("redisLocker")
|
||||
private final Redis<String, Integer> locker;
|
||||
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.imyeyu.api.annotation.RequireCorePermission;
|
||||
import com.imyeyu.api.bean.CorePermissionCode;
|
||||
import com.imyeyu.api.modules.system.service.FileContentService;
|
||||
import com.imyeyu.spring.annotation.IgnoreGlobalReturn;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimit;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -21,6 +20,9 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
/**
|
||||
* 文件内容接口
|
||||
*
|
||||
* <p>读取、下载与预览均输出二进制内容,浏览器会按 Range 对同一文件分多次请求,
|
||||
* 请求次数取决于播放和下载行为,因此不使用 {@code @RequestRateLimit} 按请求次数限流。
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2026-11-24
|
||||
*/
|
||||
@@ -33,7 +35,6 @@ public class FileContentController {
|
||||
private final FileContentService contentService;
|
||||
|
||||
@Operation(summary = "读取文件", description = "以内联方式输出文件内容,支持 Range 与条件请求。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@IgnoreGlobalReturn
|
||||
@GetMapping("/read/{*path}")
|
||||
@@ -45,7 +46,6 @@ public class FileContentController {
|
||||
}
|
||||
|
||||
@Operation(summary = "下载文件", description = "以附件方式输出文件内容,支持 Range 与 HEAD 请求。")
|
||||
@RequestRateLimit
|
||||
@RequireCorePermission(CorePermissionCode.FILE_READ)
|
||||
@IgnoreGlobalReturn
|
||||
@RequestMapping(value = "/download/{*path}", method = {RequestMethod.GET, RequestMethod.HEAD})
|
||||
|
||||
@@ -7,10 +7,12 @@ import com.imyeyu.api.modules.user.entity.Permission;
|
||||
import com.imyeyu.api.modules.user.service.AuthorizationScopeService;
|
||||
import com.imyeyu.api.modules.user.service.PermissionService;
|
||||
import com.imyeyu.api.modules.user.service.UserLoginService;
|
||||
import com.imyeyu.api.modules.user.vo.permission.BatchCreateReq;
|
||||
import com.imyeyu.spring.annotation.AOPLog;
|
||||
import com.imyeyu.spring.bean.Page;
|
||||
import com.imyeyu.spring.bean.PageResult;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -21,12 +23,13 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 权限 SDK 控制器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-11-02 09:54
|
||||
*/
|
||||
/// 权限接口
|
||||
///
|
||||
/// 权限由系统代码定义,不支持创建和删除,仅提供查询与展示信息维护。
|
||||
///
|
||||
/// @author 夜雨
|
||||
/// @since 2025-11-02 09:54
|
||||
@Tag(name = "用户-权限", description = "系统内置权限查询与展示信息维护接口")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -37,43 +40,27 @@ public class PermissionController {
|
||||
private final UserLoginService userLoginService;
|
||||
private final AuthorizationScopeService authorizationScopeService;
|
||||
|
||||
@Operation(summary = "分页查询权限", description = "分页查询系统内置权限。权限由代码定义,不支持创建和删除。")
|
||||
@RequireCorePermission(CorePermissionCode.PERMISSION_READ)
|
||||
@PostMapping("/list")
|
||||
public PageResult<Permission> list(@RequestBody Page<Permission> page) {
|
||||
return service.page(page);
|
||||
}
|
||||
|
||||
@Operation(summary = "查询可授权权限", description = "查询当前用户在指定模块下可授予角色的权限。")
|
||||
@RequireCorePermission(CorePermissionCode.ROLE_PERMISSION_READ)
|
||||
@PostMapping("/grantable/list")
|
||||
public List<Permission> listGrantable(@RequestParam ModuleCode moduleCode) {
|
||||
public List<Permission> listGrantable(
|
||||
@Parameter(description = "模块代码", required = true)
|
||||
@RequestParam ModuleCode moduleCode) {
|
||||
return authorizationScopeService.listGrantablePermission(userLoginService.getRequireLoginUserId(), moduleCode);
|
||||
}
|
||||
|
||||
@AOPLog
|
||||
@RequireCorePermission(CorePermissionCode.PERMISSION_READ)
|
||||
@PostMapping("/create")
|
||||
public void create(@RequestBody Permission permission) {
|
||||
service.create(permission);
|
||||
}
|
||||
|
||||
@AOPLog
|
||||
@RequireCorePermission(CorePermissionCode.PERMISSION_CREATE)
|
||||
@PostMapping("/create/batch")
|
||||
public void createBatch(@RequestBody BatchCreateReq req) {
|
||||
service.create(req);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改权限展示信息", description = "仅修改权限名称和说明;权限代码及内置属性由代码定义,不允许通过接口修改。")
|
||||
@AOPLog
|
||||
@RequireCorePermission(CorePermissionCode.PERMISSION_UPDATE)
|
||||
@PostMapping("/update")
|
||||
public void update(@RequestBody Permission permission) {
|
||||
service.update(permission);
|
||||
}
|
||||
|
||||
@AOPLog
|
||||
@RequireCorePermission(CorePermissionCode.PERMISSION_DELETE)
|
||||
@PostMapping("/delete")
|
||||
public void delete(@RequestParam("id") String id) {
|
||||
service.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.imyeyu.api.modules.user.entity.Permission;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -18,4 +19,10 @@ public interface PermissionMapper extends BaseMapper<Permission, String> {
|
||||
List<Permission> selectByIdList(@Param("idList") List<String> idList);
|
||||
|
||||
List<Permission> selectByModuleCode(@Param("moduleCode") ModuleCode moduleCode);
|
||||
|
||||
/// 查询所有未删除权限的模块与代码,用于启动漂移检查
|
||||
///
|
||||
/// @return 权限列表,仅填充 moduleCode、code
|
||||
@Select("SELECT `module_code`, `code` FROM `permission` WHERE `deleted_at` IS NULL")
|
||||
List<Permission> selectActiveCodeList();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.imyeyu.api.modules.user.service;
|
||||
|
||||
import com.imyeyu.api.modules.user.entity.Permission;
|
||||
import com.imyeyu.api.modules.user.vo.permission.BatchCreateReq;
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import com.imyeyu.spring.service.BaseService;
|
||||
|
||||
@@ -13,11 +12,6 @@ import java.util.List;
|
||||
/// @since 2026-05-13 14:46
|
||||
public interface PermissionService extends BaseService<Permission, String> {
|
||||
|
||||
/// 批量创建权限
|
||||
///
|
||||
/// @param req 批量创建请求
|
||||
void create(BatchCreateReq req);
|
||||
|
||||
/// 查询角色权限
|
||||
///
|
||||
/// @param roleId 角色 ID
|
||||
|
||||
+10
-31
@@ -2,27 +2,20 @@ package com.imyeyu.api.modules.user.service.implement;
|
||||
|
||||
import com.imyeyu.api.config.dbsource.TimiServerDBConfig;
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import com.imyeyu.api.modules.common.mapper.MultilingualMapper;
|
||||
import com.imyeyu.api.modules.common.service.MultilingualService;
|
||||
import com.imyeyu.api.modules.user.bean.AuthOwnerType;
|
||||
import com.imyeyu.api.modules.user.entity.Permission;
|
||||
import com.imyeyu.api.modules.user.mapper.PermissionMapper;
|
||||
import com.imyeyu.api.modules.user.mapper.RolePermissionMapper;
|
||||
import com.imyeyu.api.modules.user.service.PermissionService;
|
||||
import com.imyeyu.api.modules.user.vo.permission.BatchCreateReq;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.lang.message.MessageResolver;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.mapper.BaseMapper;
|
||||
import com.imyeyu.spring.service.AbstractEntityService;
|
||||
import com.imyeyu.utils.Time;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/// 权限服务实现
|
||||
///
|
||||
@@ -35,9 +28,7 @@ public class PermissionServiceImplement extends AbstractEntityService<Permission
|
||||
private final PermissionMapper mapper;
|
||||
private final RolePermissionMapper rolePermissionMapper;
|
||||
|
||||
private final MultilingualMapper multilingualMapper;
|
||||
private final MultilingualService multilingualService;
|
||||
private final MessageResolver messageResolver;
|
||||
|
||||
@Override
|
||||
protected BaseMapper<Permission, String> mapper() {
|
||||
@@ -52,31 +43,19 @@ public class PermissionServiceImplement extends AbstractEntityService<Permission
|
||||
super.create(permission);
|
||||
}
|
||||
|
||||
@Transactional(TimiServerDBConfig.ROLLBACKER)
|
||||
@Override
|
||||
public void create(BatchCreateReq req) {
|
||||
if (TimiJava.isEmpty(req.getTypes())) {
|
||||
return;
|
||||
}
|
||||
List<Permission> permissionList = new ArrayList<>();
|
||||
for (BatchCreateReq.Type type : req.getTypes()) {
|
||||
Permission permission = new Permission();
|
||||
permission.setId(UUID.randomUUID().toString());
|
||||
permission.setModuleCode(req.getModuleCode());
|
||||
permission.setCode(req.getPrefixCode() + "_" + type.name());
|
||||
permission.setName(req.getPrefixName() + messageResolver.resolve(type.name().toLowerCase(), null, TimiSpring.getLanguage()));
|
||||
fillPermissionDefault(permission);
|
||||
permission.setCreatedAt(Time.now());
|
||||
permissionList.add(permission);
|
||||
}
|
||||
for (Permission permission : permissionList) {
|
||||
super.create(permission);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(TimiServerDBConfig.ROLLBACKER)
|
||||
@Override
|
||||
public void update(Permission permission) {
|
||||
// 权限代码及内置属性由代码定义维护,接口只允许修改名称和说明
|
||||
permission.setModuleCode(null);
|
||||
permission.setCode(null);
|
||||
permission.setBuiltin(null);
|
||||
permission.setProtectedPermission(null);
|
||||
permission.setOwnerType(null);
|
||||
permission.setOwnerId(null);
|
||||
permission.setCreatedBy(null);
|
||||
permission.setCreatedAt(null);
|
||||
permission.setDeletedAt(null);
|
||||
permission.setNameLangId(multilingualService.createIfDifferent(permission.getNameLangId(), permission.getName()));
|
||||
mapper.updateSelective(permission);
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.imyeyu.api.modules.user.vo.permission;
|
||||
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 批量创建权限请求
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-11-18 16:49
|
||||
*/
|
||||
@Data
|
||||
public class BatchCreateReq {
|
||||
|
||||
private ModuleCode moduleCode;
|
||||
|
||||
/**
|
||||
* 权限类型枚举
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-11-18 16:50
|
||||
*/
|
||||
public enum Type {
|
||||
|
||||
CREATE,
|
||||
|
||||
READ,
|
||||
|
||||
UPDATE,
|
||||
|
||||
DELETE
|
||||
}
|
||||
|
||||
private String prefixCode;
|
||||
|
||||
private String prefixName;
|
||||
|
||||
private Type[] types;
|
||||
}
|
||||
@@ -34,12 +34,16 @@ import com.imyeyu.api.modules.user.service.UserService;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.utils.Text;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -48,10 +52,14 @@ import java.util.Map;
|
||||
* @author 夜雨
|
||||
* @since 2026-06-05 15:45
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class InitBuiltinUser implements ApplicationRunner {
|
||||
|
||||
@Value("${spring.profiles.active:prod}")
|
||||
private String activeProfile;
|
||||
|
||||
private final RoleService roleService;
|
||||
private final UserService userService;
|
||||
private final LoggerService loggerService;
|
||||
@@ -77,6 +85,7 @@ public class InitBuiltinUser implements ApplicationRunner {
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
// 先同步所有内建权限和角色,再补齐继承与默认授权关系
|
||||
Map<String, Permission> permissionMap = initBuiltinPermissionMap();
|
||||
checkBuiltinPermissionDrift(permissionMap);
|
||||
Map<String, Role> roleMap = initBuiltinRoleMap();
|
||||
bindBuiltinRolePermissionInherit(roleMap);
|
||||
bindBuiltinRoleParent(roleMap);
|
||||
@@ -139,6 +148,10 @@ public class InitBuiltinUser implements ApplicationRunner {
|
||||
fillBuiltinPermission(permission, item);
|
||||
permissionService.create(permission);
|
||||
permission = permissionMapper.selectByExample(example);
|
||||
if (permission == null) {
|
||||
String key = BuiltinAuthRegistry.toPermissionKey(item.getModuleCode(), item.getValue());
|
||||
throw new IllegalStateException("内建权限初始化失败:" + key);
|
||||
}
|
||||
} else {
|
||||
fillBuiltinPermission(permission, item);
|
||||
permissionMapper.updateSelective(permission);
|
||||
@@ -148,6 +161,35 @@ public class InitBuiltinUser implements ApplicationRunner {
|
||||
return permissionMap;
|
||||
}
|
||||
|
||||
/// 检查数据库中存在但未在注册中心定义的权限
|
||||
///
|
||||
/// 生产环境仅告警,其他环境阻止启动,避免开发阶段出现代码与数据库漂移
|
||||
///
|
||||
/// @param permissionMap 已同步的内建权限映射
|
||||
private void checkBuiltinPermissionDrift(Map<String, Permission> permissionMap) {
|
||||
List<String> staleKeys = permissionMapper.selectActiveCodeList().stream()
|
||||
.map(item -> BuiltinAuthRegistry.toPermissionKey(item.getModuleCode(), item.getCode()))
|
||||
.filter(key -> !permissionMap.containsKey(key))
|
||||
.sorted()
|
||||
.toList();
|
||||
if (staleKeys.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String message = "检测到未在 BuiltinAuthRegistry 中注册的权限:%s".formatted(String.join(", ", staleKeys));
|
||||
if (isProductionProfile()) {
|
||||
log.warn("{};已跳过自动删除,请确认是否漏写 Flyway 迁移", message);
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException(message + ";请补充注册定义或通过 Flyway 迁移移除");
|
||||
}
|
||||
|
||||
/// @return true 为生产环境
|
||||
private boolean isProductionProfile() {
|
||||
return Arrays.stream(activeProfile.split(","))
|
||||
.map(String::trim)
|
||||
.anyMatch(profile -> profile.startsWith("prod"));
|
||||
}
|
||||
|
||||
/// 按内建定义补齐角色表,不存在才创建
|
||||
///
|
||||
/// @return 模块加角色代码到角色实体的映射
|
||||
|
||||
@@ -341,6 +341,10 @@ public class MultilingualFieldBinder {
|
||||
}
|
||||
|
||||
private boolean isSimpleValueType(Class<?> clazz) {
|
||||
// 泛型字段在反射中表现为 Object,实际值仍需按运行时类型继续遍历
|
||||
if (Object.class == clazz) {
|
||||
return false;
|
||||
}
|
||||
if (clazz.isPrimitive() || clazz.isEnum() || clazz.isAnnotation()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.imyeyu.api.util;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
@@ -14,11 +16,15 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
/**
|
||||
* 统一处理响应中的多语言字段回填
|
||||
*
|
||||
* <p>必须在全局返回包装处理器之前执行,否则响应体已被包装为 TimiResponse,
|
||||
* 其 {@code data} 字段声明为泛型 {@code T}(反射类型为 Object),无法继续向下遍历。</p>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-07-17 00:00
|
||||
*/
|
||||
@Component
|
||||
@RestControllerAdvice
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
@RequiredArgsConstructor
|
||||
public class MultilingualResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
-- 权限由系统代码硬约束,移除已废弃的权限创建、删除权限及其授权关系
|
||||
|
||||
UPDATE `role_permission` role_permission
|
||||
JOIN `permission` permission ON permission.`id` = role_permission.`permission_id`
|
||||
SET role_permission.`deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE permission.`module_code` = 'CORE'
|
||||
AND permission.`code` IN ('PERMISSION:CREATE', 'PERMISSION:DELETE')
|
||||
AND role_permission.`deleted_at` IS NULL;
|
||||
|
||||
UPDATE `role_permission_delegation` delegation
|
||||
JOIN `permission` permission ON permission.`id` = delegation.`permission_id`
|
||||
SET delegation.`deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE permission.`module_code` = 'CORE'
|
||||
AND permission.`code` IN ('PERMISSION:CREATE', 'PERMISSION:DELETE')
|
||||
AND delegation.`deleted_at` IS NULL;
|
||||
|
||||
UPDATE `permission`
|
||||
SET `deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE `module_code` = 'CORE'
|
||||
AND `code` IN ('PERMISSION:CREATE', 'PERMISSION:DELETE')
|
||||
AND `deleted_at` IS NULL;
|
||||
@@ -0,0 +1,39 @@
|
||||
-- 清理已从 BuiltinAuthRegistry 移除的历史权限及其授权关系
|
||||
-- ROLE_RELATION:* 已由 ROLE_PERMISSION_INHERIT:* 取代
|
||||
-- STORE/CUSTOMER/EVENT_RECORD:EXPORT 已由 DATA_EXPORT 取代
|
||||
-- SETTING:CREATE/DELETE 功能已移除
|
||||
|
||||
UPDATE `role_permission` role_permission
|
||||
JOIN `permission` permission ON permission.`id` = role_permission.`permission_id`
|
||||
SET role_permission.`deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE role_permission.`deleted_at` IS NULL
|
||||
AND (
|
||||
(permission.`module_code` = 'CORE'
|
||||
AND permission.`code` IN ('ROLE_RELATION:CREATE', 'ROLE_RELATION:READ', 'ROLE_RELATION:DELETE',
|
||||
'SETTING:CREATE', 'SETTING:DELETE'))
|
||||
OR (permission.`module_code` = 'GAO'
|
||||
AND permission.`code` IN ('CUSTOMER:EXPORT', 'EVENT_RECORD:EXPORT', 'STORE:EXPORT'))
|
||||
);
|
||||
|
||||
UPDATE `role_permission_delegation` delegation
|
||||
JOIN `permission` permission ON permission.`id` = delegation.`permission_id`
|
||||
SET delegation.`deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE delegation.`deleted_at` IS NULL
|
||||
AND (
|
||||
(permission.`module_code` = 'CORE'
|
||||
AND permission.`code` IN ('ROLE_RELATION:CREATE', 'ROLE_RELATION:READ', 'ROLE_RELATION:DELETE',
|
||||
'SETTING:CREATE', 'SETTING:DELETE'))
|
||||
OR (permission.`module_code` = 'GAO'
|
||||
AND permission.`code` IN ('CUSTOMER:EXPORT', 'EVENT_RECORD:EXPORT', 'STORE:EXPORT'))
|
||||
);
|
||||
|
||||
UPDATE `permission`
|
||||
SET `deleted_at` = UNIX_TIMESTAMP() * 1000
|
||||
WHERE `deleted_at` IS NULL
|
||||
AND (
|
||||
(`module_code` = 'CORE'
|
||||
AND `code` IN ('ROLE_RELATION:CREATE', 'ROLE_RELATION:READ', 'ROLE_RELATION:DELETE',
|
||||
'SETTING:CREATE', 'SETTING:DELETE'))
|
||||
OR (`module_code` = 'GAO'
|
||||
AND `code` IN ('CUSTOMER:EXPORT', 'EVENT_RECORD:EXPORT', 'STORE:EXPORT'))
|
||||
);
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.imyeyu.api.util;
|
||||
|
||||
import com.imyeyu.api.modules.common.entity.Multilingual;
|
||||
import com.imyeyu.api.modules.common.entity.Tag;
|
||||
import com.imyeyu.api.modules.common.service.MultilingualService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/// 响应多语言字段回填测试
|
||||
///
|
||||
/// @author 夜雨
|
||||
/// @since 2026-11-26
|
||||
class MultilingualFieldBinderTest {
|
||||
|
||||
@AfterEach
|
||||
void resetRequestContext() {
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
}
|
||||
|
||||
/// 泛型包装对象的 data 字段在反射中表现为 Object,仍需继续向下回填
|
||||
@Test
|
||||
void bindsMultilingualFieldThroughObjectTypedWrapper() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addHeader("Accept-Language", "zh-CN");
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
||||
MultilingualService service = mock(MultilingualService.class);
|
||||
Multilingual multilingual = new Multilingual();
|
||||
multilingual.setId("lang-1");
|
||||
multilingual.setZhCN("标签");
|
||||
when(service.listByIdList(List.of("lang-1"))).thenReturn(List.of(multilingual));
|
||||
|
||||
Tag tag = new Tag();
|
||||
tag.setNameLangId("lang-1");
|
||||
|
||||
MultilingualFieldBinder binder = new MultilingualFieldBinder(service);
|
||||
binder.bind(new ObjectDataWrapper(tag));
|
||||
|
||||
assertEquals("标签", tag.getName());
|
||||
}
|
||||
|
||||
/// 模拟 TimiResponse 等泛型包装对象
|
||||
private record ObjectDataWrapper(Object data) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user