AdoS8ConfigWatchRulesController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Security.Claims;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Service.S8;
  4. using Microsoft.Extensions.Logging;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S8;
  6. [ApiController]
  7. [Route("api/aidop/s8/config/watch-rules")]
  8. [NonUnify]
  9. public class AdoS8ConfigWatchRulesController : ControllerBase
  10. {
  11. private const string LegacyHeaderUseInstead = "PUT /api/aidop/s8/config/watch-rules/{id}/params";
  12. private readonly S8WatchRuleService _svc;
  13. private readonly ILogger<AdoS8ConfigWatchRulesController> _logger;
  14. public AdoS8ConfigWatchRulesController(
  15. S8WatchRuleService svc,
  16. ILogger<AdoS8ConfigWatchRulesController> logger)
  17. {
  18. _svc = svc;
  19. _logger = logger;
  20. }
  21. [HttpGet]
  22. public async Task<IActionResult> ListAsync([FromQuery] long tenantId = 1, [FromQuery] long factoryId = 1) =>
  23. Ok(await _svc.ListAsync(tenantId, factoryId));
  24. /// <summary>
  25. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  26. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  27. /// </summary>
  28. [Obsolete("Legacy full-create endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  29. [HttpPost]
  30. public async Task<IActionResult> CreateAsync([FromBody] AdoS8WatchRule body)
  31. {
  32. MarkLegacyDeprecated("POST /api/aidop/s8/config/watch-rules");
  33. try { return Ok(await _svc.CreateAsync(body)); }
  34. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  35. }
  36. /// <summary>
  37. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  38. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  39. /// </summary>
  40. [Obsolete("Legacy full-update endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  41. [HttpPut("{id:long}")]
  42. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS8WatchRule body)
  43. {
  44. MarkLegacyDeprecated($"PUT /api/aidop/s8/config/watch-rules/{id}", id);
  45. try { return Ok(await _svc.UpdateAsync(id, body)); }
  46. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  47. }
  48. /// <summary>
  49. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  50. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  51. /// </summary>
  52. [Obsolete("Legacy delete endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  53. [HttpDelete("{id:long}")]
  54. public async Task<IActionResult> DeleteAsync(long id)
  55. {
  56. MarkLegacyDeprecated($"DELETE /api/aidop/s8/config/watch-rules/{id}", id);
  57. await _svc.DeleteAsync(id);
  58. return Ok();
  59. }
  60. /// <summary>
  61. /// Legacy endpoint. Rule configuration UI must use PUT /{id}/params.
  62. /// 保留兼容历史脚本/集成调用,但响应 header 与日志会标记为 deprecated。
  63. /// </summary>
  64. [Obsolete("Legacy test endpoint. Use PUT /api/aidop/s8/config/watch-rules/{id}/params for safe params editing.")]
  65. [HttpPost("{id:long}/test")]
  66. public async Task<IActionResult> TestAsync(long id)
  67. {
  68. MarkLegacyDeprecated($"POST /api/aidop/s8/config/watch-rules/{id}/test", id);
  69. try { return Ok(await _svc.TestAsync(id)); }
  70. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  71. }
  72. /// <summary>
  73. /// R4 安全更新:仅修改 params_json 与 enabled,绝不接受 expression / rule_code / data_source_id /
  74. /// scene_code / watch_object_type / rule_type / source_object_type 等敏感字段。
  75. /// 服务端按 rule_type 用对应 evaluator 的 Params.Parse 进行 schema 校验。
  76. /// </summary>
  77. [HttpPut("{id:long}/params")]
  78. public async Task<IActionResult> UpdateParamsAsync(long id, [FromBody] S8WatchRuleParamsPayload body)
  79. {
  80. try { return Ok(await _svc.UpdateParamsAsync(id, body)); }
  81. catch (S8BizException ex) { return BadRequest(new { message = ex.Message }); }
  82. }
  83. /// <summary>
  84. /// 旧端点 deprecated 收口:写入响应 header 标记 + 结构化 warning 日志,便于运行期识别 legacy 调用。
  85. /// 不阻断调用,不改返回结构。
  86. /// </summary>
  87. private void MarkLegacyDeprecated(string endpoint, long? ruleId = null)
  88. {
  89. // 响应 header — Headers 在响应已开始发送后会抛 InvalidOperationException,此处守卫一下避免污染主调用。
  90. var headers = Response.Headers;
  91. if (!headers.ContainsKey("X-AiDOP-Deprecated"))
  92. headers["X-AiDOP-Deprecated"] = "true";
  93. if (!headers.ContainsKey("X-AiDOP-Use-Instead"))
  94. headers["X-AiDOP-Use-Instead"] = LegacyHeaderUseInstead;
  95. var userId = User?.FindFirst("UserId")?.Value
  96. ?? User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
  97. var tenantId = HttpContext?.Request?.Query["tenantId"].ToString();
  98. var factoryId = HttpContext?.Request?.Query["factoryId"].ToString();
  99. _logger.LogWarning(
  100. "legacy_watch_rule_endpoint endpoint={Endpoint} ruleId={RuleId} userId={UserId} tenantId={TenantId} factoryId={FactoryId} useInstead={UseInstead}",
  101. endpoint, ruleId, userId, tenantId, factoryId, LegacyHeaderUseInstead);
  102. }
  103. }