-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBusinessRuleController.java
More file actions
101 lines (91 loc) · 3.89 KB
/
Copy pathBusinessRuleController.java
File metadata and controls
101 lines (91 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.dbaagent.controller;
import com.dbaagent.model.brain.BrainRule;
import com.dbaagent.service.BusinessRuleMemoryService;
import com.dbaagent.service.security.AccessControlService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* API endpoints for connection-scoped learned SQL business rules.
*
* <p><b>Authorization:</b> every endpoint here takes a caller-supplied connection id, so
* each one asserts access itself ({@code assertCanReadConnectionContent} for reads,
* {@code assertCanManageConnectionContent} for writes). {@code SecurityConfig} only
* requires an authenticated principal — nothing upstream inspects a connection id. See
* {@code ConnectionScopedAuthorizationSafetyTest}.
*/
@RestController
@RequestMapping("/business-rules")
@RequiredArgsConstructor
public class BusinessRuleController {
private final BusinessRuleMemoryService businessRuleMemoryService;
private final AccessControlService accessControlService;
/**
* Returns all active rules for the connection plus the subset applicable to an optional question.
*/
@GetMapping("/connection/{connectionId}")
public ResponseEntity<Map<String, Object>> getRules(
@PathVariable String connectionId,
@RequestParam(required = false) String question) {
accessControlService.assertCanReadConnectionContent(connectionId);
List<BrainRule> activeRules = businessRuleMemoryService.getActiveRules(connectionId);
List<BusinessRuleMemoryService.SqlGuardrail> applicable = businessRuleMemoryService
.resolveApplicableGuardrails(connectionId, question, null);
return ResponseEntity.ok(Map.of(
"connectionId", connectionId,
"activeRuleCount", activeRules.size(),
"activeRules", activeRules,
"applicableGuardrailCount", applicable.size(),
"applicableGuardrails", applicable,
"guardrailContext", businessRuleMemoryService.buildGuardrailContext(applicable)
));
}
/**
* Manual rule ingestion endpoint for operational/debug use.
*
* The learned rules are stored as connection-scoped SQL_* brain rules.
*/
@PostMapping("/connection/{connectionId}/learn")
public ResponseEntity<Map<String, Object>> learn(
@PathVariable String connectionId,
@RequestBody LearnRuleRequest request) {
accessControlService.assertCanManageConnectionContent(connectionId);
int learned = businessRuleMemoryService.learnFromFeedback(
connectionId,
request.text(),
request.tableName(),
request.columnName(),
request.createdBy(),
null
);
return ResponseEntity.ok(Map.of(
"connectionId", connectionId,
"learnedCount", learned,
"activeRuleCount", businessRuleMemoryService.getActiveRules(connectionId).size()
));
}
/**
* Deactivate a single rule by ID.
*/
@DeleteMapping("/rule/{ruleId}")
public ResponseEntity<Map<String, Object>> deactivateRule(@PathVariable String ruleId) {
String connectionId = businessRuleMemoryService.findConnectionIdForRule(ruleId)
.orElseThrow(() -> new org.springframework.web.server.ResponseStatusException(
org.springframework.http.HttpStatus.NOT_FOUND, "Rule not found"));
accessControlService.assertCanManageConnectionContent(connectionId);
boolean deactivated = businessRuleMemoryService.deactivateRule(ruleId);
return ResponseEntity.ok(Map.of(
"ruleId", ruleId,
"deactivated", deactivated
));
}
public record LearnRuleRequest(
String text,
String tableName,
String columnName,
String createdBy
) {
}
}