feat: add framework script editor actions
Some checks failed
ci / build-test-parity (push) Has been cancelled
Some checks failed
ci / build-test-parity (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||
import com.eip4.platform.metadata.security.IllegalIdentifierException;
|
||||
|
||||
/**
|
||||
* 通用字段源码编辑端点。
|
||||
*
|
||||
* <p>用于 framework 定制视图中的 AceEditor 迁移:RUNSCRIPT / CONVERTSCRIPT /
|
||||
* SAVESCRIPT / XML / SENDINFO 等字段由 amis input-editor 编辑。表名和列名先过
|
||||
* {@link IdentifierWhitelist},再叠加一层可编辑字段白名单,避免把它变成任意列更新接口。
|
||||
*/
|
||||
@RestController
|
||||
public class FieldEditorController {
|
||||
|
||||
private static final Pattern IDENTIFIER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*");
|
||||
|
||||
private static final Set<String> EDITABLE_FIELDS = Set.of(
|
||||
"RUNSCRIPT",
|
||||
"CONVERTSCRIPT",
|
||||
"SAVESCRIPT",
|
||||
"XML",
|
||||
"CONTENTXML",
|
||||
"SENDINFO");
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final IdentifierWhitelist whitelist;
|
||||
|
||||
public FieldEditorController(JdbcTemplate jdbcTemplate, IdentifierWhitelist whitelist) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.whitelist = whitelist;
|
||||
}
|
||||
|
||||
@PostMapping("/api/field-editor/load")
|
||||
public Map<String, Object> load(@RequestBody FieldEditRequest req) {
|
||||
Identifiers ids = validate(req);
|
||||
try {
|
||||
String value = jdbcTemplate.queryForObject(
|
||||
"SELECT " + ids.fieldName + " FROM " + ids.tableName + " WHERE "
|
||||
+ ids.idField + " = ?",
|
||||
String.class, req.id);
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("value", value == null ? "" : value);
|
||||
Map<String, Object> resp = ok("加载成功");
|
||||
resp.put("data", data);
|
||||
return resp;
|
||||
} catch (EmptyResultDataAccessException ex) {
|
||||
return fail("记录不存在或已被删除");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/api/field-editor/save")
|
||||
public Map<String, Object> save(@RequestBody FieldEditRequest req) {
|
||||
Identifiers ids = validate(req);
|
||||
int updated = jdbcTemplate.update(
|
||||
"UPDATE " + ids.tableName + " SET " + ids.fieldName + " = ? WHERE "
|
||||
+ ids.idField + " = ?",
|
||||
req.value == null ? "" : req.value, req.id);
|
||||
if (updated <= 0) {
|
||||
return fail("记录不存在或已被删除");
|
||||
}
|
||||
return ok("保存成功");
|
||||
}
|
||||
|
||||
private Identifiers validate(FieldEditRequest req) {
|
||||
if (req == null) {
|
||||
throw new IllegalIdentifierException("字段编辑参数为空");
|
||||
}
|
||||
String table = normalizeIdentifier(req.tableName, "实体表名");
|
||||
String idField = normalizeIdentifier(req.idField, "主键列");
|
||||
String field = normalizeIdentifier(req.fieldName, "编辑字段");
|
||||
if (req.id == null || req.id.isBlank()) {
|
||||
throw new IllegalIdentifierException("字段编辑缺少记录主键");
|
||||
}
|
||||
if (!EDITABLE_FIELDS.contains(field)) {
|
||||
throw new IllegalIdentifierException("字段编辑只允许脚本/XML/配置字段");
|
||||
}
|
||||
whitelist.requireLegalTable(table);
|
||||
whitelist.requireLegalColumn(table, idField, "主键列");
|
||||
whitelist.requireLegalColumn(table, field, "编辑字段");
|
||||
return new Identifiers(table, idField, field);
|
||||
}
|
||||
|
||||
private static String normalizeIdentifier(String raw, String name) {
|
||||
if (raw == null || raw.isBlank() || !IDENTIFIER.matcher(raw).matches()) {
|
||||
throw new IllegalIdentifierException(name + "含非法标识符,已拒绝");
|
||||
}
|
||||
return raw.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static Map<String, Object> ok(String msg) {
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", msg);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static Map<String, Object> fail(String msg) {
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 1);
|
||||
resp.put("msg", msg);
|
||||
return resp;
|
||||
}
|
||||
|
||||
public static class FieldEditRequest {
|
||||
public String tableName;
|
||||
public String idField;
|
||||
public String id;
|
||||
public String fieldName;
|
||||
public String value;
|
||||
}
|
||||
|
||||
private record Identifiers(String tableName, String idField, String fieldName) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user