update
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Shared business-source contracts for routing and executable-plan validation.
|
||||
|
||||
Contracts describe semantics, not permissions. Java still authenticates every
|
||||
read. Older catalogs retain their reviewed human role until both services update.
|
||||
"""
|
||||
|
||||
RELATIONS = {'applicant', 'pending_approver', 'approval_history', 'creator', 'assignee', 'participant', 'manager', 'unspecified', 'mixed'}
|
||||
|
||||
|
||||
def actor_relation(definition):
|
||||
relation = definition.get('query_contract', {}).get('actor_relation')
|
||||
if relation in RELATIONS:
|
||||
return relation
|
||||
role = definition.get('business_role', '')
|
||||
if role.startswith(('本人申请', '申请台账')):
|
||||
return 'applicant'
|
||||
if role.startswith('待我审批'):
|
||||
return 'pending_approver'
|
||||
return 'unspecified'
|
||||
|
||||
|
||||
def relation_conflict(requested, definition):
|
||||
supported = definition.get('query_contract', {}).get('actor_relations', [])
|
||||
if requested not in {'unspecified', 'mixed', ''} and supported:
|
||||
return requested not in supported
|
||||
actual = actor_relation(definition)
|
||||
if requested in {'unspecified', 'mixed', ''} or actual == 'unspecified':
|
||||
return False
|
||||
return requested != actual
|
||||
|
||||
|
||||
def contract_errors(query, definition, requested_relation='unspecified'):
|
||||
"""Run again on the planner result; routing alone is not an execution guard."""
|
||||
errors = []
|
||||
if relation_conflict(requested_relation, definition):
|
||||
errors.append({'type': 'business_relation_conflict',
|
||||
'message': '当前来源的业务关系与用户要求不同。本人申请、待我审批、已审批历史不能互相替代;选择匹配的授权来源,缺少来源时说明具体缺口。'})
|
||||
if query.filters.get('summary') and query.operation == 'count':
|
||||
errors.append({'type': 'aggregate_operation_conflict',
|
||||
'message': 'summary统计必须使用list执行,count只统计原始记录数量,不能将总额当成条数。'})
|
||||
return errors
|
||||
|
||||
|
||||
def normalize_operation(query, definition):
|
||||
"""Canonicalize registered aggregate operations without another model call.
|
||||
|
||||
This preserves the chosen metric and every predicate. It never chooses an
|
||||
amount field or turns a count into an inferred sum.
|
||||
"""
|
||||
summary = query.filters.get('summary')
|
||||
values = definition.get('filters', {}).get('summary', {}).get('values', {})
|
||||
if query.operation != 'count' or summary not in values:
|
||||
return
|
||||
if summary == 'count' and not query.filters.get('group_by'):
|
||||
query.filters.pop('summary')
|
||||
elif 'list' in definition.get('operations', []):
|
||||
query.operation = 'list'
|
||||
|
||||
|
||||
def bind_relation(query, definition, requested):
|
||||
"""Bind reviewed role predicates; actor identity remains server-controlled."""
|
||||
for key, value in definition.get('query_contract', {}).get('relation_filters', {}).get(requested, {}).items():
|
||||
if key not in definition.get('filters', {}):
|
||||
raise ValueError('Invalid registered relationship filter')
|
||||
query.filters[key] = value
|
||||
|
||||
|
||||
def audit_catalog(resources):
|
||||
"""Cheap, offline checks whenever a reviewed resource is added or changed."""
|
||||
issues = []
|
||||
ids = [r.get('id') for r in resources]
|
||||
for r in resources:
|
||||
rid = r.get('id')
|
||||
def fail(code, detail): issues.append({'resource': rid, 'code': code, 'detail': detail})
|
||||
if not rid or ids.count(rid) != 1: fail('duplicate_resource', '资源ID缺失或重复')
|
||||
if not set(r.get('operations', [])) <= {'list', 'count', 'detail'}:
|
||||
fail('write_operation', '只读助手目录包含非只读操作')
|
||||
for field in r.get('context_filters', []):
|
||||
if field not in r.get('filters', {}): fail('unknown_context_filter', field)
|
||||
for label, pair in r.get('time_bindings', {}).items():
|
||||
if len(pair) != 2 or any(key not in r.get('filters', {}) for key in pair): fail('invalid_time_binding', label)
|
||||
for name, relation in r.get('relations', {}).items():
|
||||
if relation.get('target') not in ids: fail('unknown_relation_target', name)
|
||||
for key in ('subject_filter', 'subject_id_filter', 'self_filter', 'department_filter'):
|
||||
if r.get(key) and r[key] not in r.get('filters', {}): fail('unknown_subject_filter', key)
|
||||
if r.get('query_contract', {}).get('actor_relation', 'unspecified') not in RELATIONS:
|
||||
fail('unknown_actor_relation', str(r['query_contract']))
|
||||
if not set(r.get('query_contract', {}).get('actor_relations', [])) <= RELATIONS:
|
||||
fail('unknown_actor_relation', str(r['query_contract']))
|
||||
for relation, filters in r.get('query_contract', {}).get('relation_filters', {}).items():
|
||||
if relation not in RELATIONS or not set(filters) <= set(r.get('filters', {})):
|
||||
fail('invalid_relation_binding', relation)
|
||||
return issues
|
||||
Reference in New Issue
Block a user