سامانه صدور فاکتور و حسابداری
security: bind legacy transition replay to actor and action
davoodmehraban89 committed
Oct 2, 2026 at 01:14 UTC
3134b565cfe8125385755a92328f715a45ff916e
1 file changed
+12
-3
supabase/migrations/20261002010000_request_wave1_legacy_transition_guard.sql
+12
-3
@@ -15,6 +15,7 @@ declare
15
v_row public.request_instances%rowtype;
16
v_next text;
17
v_from text;
18
+ v_action text;
19
v_event public.request_instance_events%rowtype;
20
begin
21
if auth.uid() is null then raise exception 'authentication required' using errcode='28000'; end if;
@@ -33,17 +34,25 @@ begin
34
raise exception 'D3 workflow requires request_step_decide' using errcode='55000';
35
end if;
36
37
+ v_action:=lower(trim(coalesce(p_action,'')));
38
+ if v_action not in ('approve','reject','return','submit','cancel') then raise exception 'unsupported transition action'; end if;
39
+
40
select e.* into v_event
41
from public.request_instance_events e
38
- where e.request_instance_id=v_row.id and e.idempotency_key=p_idempotency_key;
42
+ where e.request_instance_id=v_row.id
43
+ and e.organization_id=v_row.organization_id
44
+ and e.idempotency_key=p_idempotency_key;
45
if found then
46
+ if v_event.actor_user_id<>auth.uid() or v_event.action<>v_action then
47
+ raise exception 'idempotency key already used by another actor or action' using errcode='42501';
48
+ end if;
49
return query select v_row.id,v_event.to_status,v_event.revision;
50
return;
51
end if;
52
53
if p_expected_revision is null or p_expected_revision<>v_row.revision then raise exception 'stale revision'; end if;
54
v_from:=v_row.status;
46
- v_next:=case lower(trim(p_action))
55
+ v_next:=case v_action
56
when 'approve' then 'approved'
57
when 'reject' then 'rejected'
58
when 'return' then 'returned'
@@ -61,7 +70,7 @@ begin
70
insert into public.request_instance_events(
71
organization_id,request_instance_id,actor_user_id,action,from_status,to_status,revision,note,idempotency_key
72
) values(
64
- v_row.organization_id,v_row.id,auth.uid(),lower(trim(p_action)),v_from,v_next,v_row.revision,p_note,p_idempotency_key
73
+ v_row.organization_id,v_row.id,auth.uid(),v_action,v_from,v_next,v_row.revision,p_note,p_idempotency_key
74
);
75
return query select v_row.id,v_next,v_row.revision;
76
end $$;