@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / ce27c5f
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 19:30 UTC

Archive C5 content-bound approval hardening migration

davoodmehraban89 committed Oct 1, 2026 at 00:09 UTC ce27c5f3ac60daf0616ab03f424727797db08928
1 file changed +148
supabase/migrations/20260930203620_office_slice_c5_internal_approval.sql new
+148
@@ -0,0 +1,148 @@
1 +-- C5 attestation hardening applied after the initial internal-approval foundation.
2 +-- Internal authenticated approval evidence only; NOT a qualified digital signature.
3 +
4 +drop function if exists public.office_approval_work_queue(uuid);
5 +drop function if exists private.office_approval_work_queue_impl(uuid);
6 +
7 +create or replace function private.office_correspondence_digest(p_data jsonb)
8 +returns text language sql immutable set search_path=''
9 +as $$ select encode(extensions.digest(convert_to(coalesce(p_data,'{}'::jsonb)::text,'UTF8'),'sha256'),'hex') $$;
10 +revoke all on function private.office_correspondence_digest(jsonb) from public,anon,authenticated;
11 +
12 +create or replace function private.office_request_approval_impl(
13 + p_organization_id uuid,p_correspondence_id text,p_approver_member_id uuid,p_note text,p_due_at timestamptz
14 +) returns text language plpgsql security definer set search_path=''
15 +as $$
16 +declare
17 + v_uid uuid:=(select auth.uid()); v_source public.records%rowtype; v_approver public.organization_members%rowtype;
18 + v_approval_id text; v_event_id text; v_digest text; v_aal text:=coalesce((select auth.jwt()->>'aal'),'aal1');
19 +begin
20 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
21 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true)
22 + or not (private.has_org_capability(p_organization_id,'office_automation','edit') or private.has_org_capability(p_organization_id,'office_automation','refer'))
23 + then raise exception 'approval request permission required' using errcode='42501'; end if;
24 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=p_correspondence_id;
25 + if not found or coalesce(v_source.data->>'status','draft')='draft'
26 + or not private.can_access_record(p_organization_id,'correspondence',v_source.owner_id,v_source.data,'read')
27 + then raise exception 'registered readable correspondence required' using errcode='42501'; end if;
28 + select * into v_approver from public.organization_members m where m.id=p_approver_member_id and m.organization_id=p_organization_id and m.status='active';
29 + if not found then raise exception 'approver member not found' using errcode='42501'; end if;
30 + if v_approver.user_id=v_uid then raise exception 'requester cannot approve own request' using errcode='22023'; end if;
31 + if not private.member_can_access_record(v_approver.id,'office_automation',v_source.owner_id,v_source.data,'approve')
32 + or not private.member_can_access_record(v_approver.id,'office_automation',v_source.owner_id,v_source.data,'read')
33 + then raise exception 'approver lacks approve/read access' using errcode='42501'; end if;
34 + perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_organization_id::text||':office-approval:'||p_correspondence_id,0));
35 + if exists(select 1 from public.records req where req.organization_id=p_organization_id and req.collection='correspondenceAudit'
36 + and req.data->>'event'='approval_requested' and req.data->>'correspondenceId'=p_correspondence_id
37 + and not exists(select 1 from public.records act where act.organization_id=p_organization_id and act.collection='correspondenceAudit'
38 + and act.data->>'approvalId'=req.data->>'approvalId' and act.data->>'event' in ('approval_approved','approval_rejected')))
39 + then raise exception 'active approval request already exists' using errcode='23505'; end if;
40 + v_approval_id:='AP_'||replace(gen_random_uuid()::text,'-',''); v_event_id:='APE_'||replace(gen_random_uuid()::text,'-','');
41 + v_digest:=private.office_correspondence_digest(v_source.data);
42 + insert into public.records(organization_id,owner_id,collection,id,data)
43 + values(p_organization_id,v_uid,'correspondenceAudit',v_event_id,
44 + jsonb_build_object('event','approval_requested','approvalId',v_approval_id,'correspondenceId',p_correspondence_id,
45 + 'requestedByUserId',v_uid,'approverMemberId',v_approver.id,'approverUserId',v_approver.user_id,
46 + 'contentDigest',v_digest,'note',coalesce(p_note,''),'dueAt',p_due_at,'aal',v_aal,'at',clock_timestamp(),
47 + 'attestationType','internal_approval','qualifiedDigitalSignature',false));
48 + return v_approval_id;
49 +end $$;
50 +revoke all on function private.office_request_approval_impl(uuid,text,uuid,text,timestamptz) from public,anon;
51 +grant execute on function private.office_request_approval_impl(uuid,text,uuid,text,timestamptz) to authenticated;
52 +
53 +create or replace function public.office_request_approval(
54 + p_organization_id uuid,p_correspondence_id text,p_approver_member_id uuid,p_note text default null,p_due_at timestamptz default null
55 +) returns text language sql security invoker set search_path=''
56 +as $$ select private.office_request_approval_impl(p_organization_id,p_correspondence_id,p_approver_member_id,p_note,p_due_at) $$;
57 +revoke all on function public.office_request_approval(uuid,text,uuid,text,timestamptz) from public,anon;
58 +grant execute on function public.office_request_approval(uuid,text,uuid,text,timestamptz) to authenticated;
59 +
60 +create or replace function private.office_act_on_approval_impl(
61 + p_organization_id uuid,p_approval_id text,p_action text,p_note text
62 +) returns text language plpgsql security definer set search_path=''
63 +as $$
64 +declare
65 + v_uid uuid:=(select auth.uid()); v_req public.records%rowtype; v_source public.records%rowtype; v_member_id uuid;
66 + v_event_id text; v_event text; v_current_digest text; v_aal text:=coalesce((select auth.jwt()->>'aal'),'aal1');
67 +begin
68 + if v_uid is null then raise exception 'authentication required' using errcode='42501'; end if;
69 + if p_action not in ('approved','rejected') then raise exception 'invalid approval action' using errcode='22023'; end if;
70 + if not private.organization_has_module_entitlement(p_organization_id,'office_automation',true) then raise exception 'office automation entitlement required' using errcode='42501'; end if;
71 + select * into v_req from public.records r where r.organization_id=p_organization_id and r.collection='correspondenceAudit'
72 + and r.data->>'event'='approval_requested' and r.data->>'approvalId'=p_approval_id
73 + order by nullif(r.data->>'at','')::timestamptz desc limit 1;
74 + if not found then raise exception 'approval request not found' using errcode='42501'; end if;
75 + if v_req.data->>'approverUserId' is distinct from v_uid::text then raise exception 'only assigned approver may decide' using errcode='42501'; end if;
76 + begin v_member_id:=(v_req.data->>'approverMemberId')::uuid; exception when others then raise exception 'invalid approver member' using errcode='42501'; end;
77 + if not exists(select 1 from public.organization_members m where m.id=v_member_id and m.organization_id=p_organization_id and m.user_id=v_uid and m.status='active')
78 + then raise exception 'approver membership inactive' using errcode='42501'; end if;
79 + select * into v_source from public.records r where r.organization_id=p_organization_id and r.collection='correspondence' and r.id=v_req.data->>'correspondenceId';
80 + if not found or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'approve')
81 + or not private.member_can_access_record(v_member_id,'office_automation',v_source.owner_id,v_source.data,'read')
82 + then raise exception 'approver no longer authorized' using errcode='42501'; end if;
83 + perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_organization_id::text||':office-approval:'||p_approval_id,0));
84 + if exists(select 1 from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit'
85 + and a.data->>'approvalId'=p_approval_id and a.data->>'event' in ('approval_approved','approval_rejected'))
86 + then raise exception 'approval already decided' using errcode='22023'; end if;
87 + v_current_digest:=private.office_correspondence_digest(v_source.data);
88 + if v_current_digest is distinct from v_req.data->>'contentDigest'
89 + then raise exception 'approval content changed; create a new approval request' using errcode='40001'; end if;
90 + v_event:=case when p_action='approved' then 'approval_approved' else 'approval_rejected' end;
91 + v_event_id:='APE_'||replace(gen_random_uuid()::text,'-','');
92 + insert into public.records(organization_id,owner_id,collection,id,data)
93 + values(p_organization_id,v_uid,'correspondenceAudit',v_event_id,
94 + jsonb_build_object('event',v_event,'approvalId',p_approval_id,'correspondenceId',v_req.data->>'correspondenceId',
95 + 'actorUserId',v_uid,'contentDigest',v_current_digest,'note',coalesce(p_note,''),'aal',v_aal,'at',clock_timestamp(),
96 + 'attestationType','internal_approval','qualifiedDigitalSignature',false));
97 + return v_event_id;
98 +end $$;
99 +revoke all on function private.office_act_on_approval_impl(uuid,text,text,text) from public,anon;
100 +grant execute on function private.office_act_on_approval_impl(uuid,text,text,text) to authenticated;
101 +
102 +create or replace function public.office_act_on_approval(
103 + p_organization_id uuid,p_approval_id text,p_action text,p_note text default null
104 +) returns text language sql security invoker set search_path=''
105 +as $$ select private.office_act_on_approval_impl(p_organization_id,p_approval_id,p_action,p_note) $$;
106 +revoke all on function public.office_act_on_approval(uuid,text,text,text) from public,anon;
107 +grant execute on function public.office_act_on_approval(uuid,text,text,text) to authenticated;
108 +
109 +create or replace function private.office_approval_work_queue_impl(p_organization_id uuid)
110 +returns table(approval_id text,correspondence_id text,register_number text,subject text,requested_by_user_id uuid,approver_user_id uuid,note text,due_at timestamptz,requested_at timestamptz,state text,overdue boolean,content_changed boolean,decision_note text,decision_at timestamptz,request_aal text,decision_aal text)
111 +language sql stable security definer set search_path=''
112 +as $$
113 + with caller as (
114 + select m.user_id,m.is_owner from public.organization_members m
115 + where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active'
116 + ), reqs as (
117 + select r.data request_data,c.data correspondence_data,c.owner_id correspondence_owner
118 + from public.records r join public.records c on c.organization_id=r.organization_id and c.collection='correspondence' and c.id=r.data->>'correspondenceId'
119 + where r.organization_id=p_organization_id and r.collection='correspondenceAudit' and r.data->>'event'='approval_requested'
120 + )
121 + select r.request_data->>'approvalId',r.request_data->>'correspondenceId',r.correspondence_data->>'registerNumber',r.correspondence_data->>'subject',
122 + nullif(r.request_data->>'requestedByUserId','')::uuid,nullif(r.request_data->>'approverUserId','')::uuid,coalesce(r.request_data->>'note',''),
123 + nullif(r.request_data->>'dueAt','')::timestamptz,nullif(r.request_data->>'at','')::timestamptz,
124 + case when d.event='approval_approved' then 'approved' when d.event='approval_rejected' then 'rejected' else 'pending' end,
125 + (d.event is null and nullif(r.request_data->>'dueAt','')::timestamptz<clock_timestamp()),
126 + (private.office_correspondence_digest(r.correspondence_data) is distinct from r.request_data->>'contentDigest'),
127 + coalesce(d.note,''),d.at,r.request_data->>'aal',d.aal
128 + from reqs r
129 + left join lateral (
130 + select a.data->>'event' event,a.data->>'note' note,nullif(a.data->>'at','')::timestamptz at,a.data->>'aal' aal
131 + from public.records a where a.organization_id=p_organization_id and a.collection='correspondenceAudit'
132 + and a.data->>'approvalId'=r.request_data->>'approvalId' and a.data->>'event' in ('approval_approved','approval_rejected')
133 + order by nullif(a.data->>'at','')::timestamptz desc limit 1
134 + ) d on true
135 + where exists(select 1 from caller) and private.organization_has_module_entitlement(p_organization_id,'office_automation',false)
136 + and ((select is_owner from caller limit 1) or r.request_data->>'requestedByUserId'=(select auth.uid())::text or r.request_data->>'approverUserId'=(select auth.uid())::text)
137 + and private.can_access_record(p_organization_id,'correspondence',r.correspondence_owner,r.correspondence_data,'read')
138 + order by (d.event is null and nullif(r.request_data->>'dueAt','')::timestamptz<clock_timestamp()) desc,nullif(r.request_data->>'at','')::timestamptz desc
139 +$$;
140 +revoke all on function private.office_approval_work_queue_impl(uuid) from public,anon;
141 +grant execute on function private.office_approval_work_queue_impl(uuid) to authenticated;
142 +
143 +create or replace function public.office_approval_work_queue(p_organization_id uuid)
144 +returns table(approval_id text,correspondence_id text,register_number text,subject text,requested_by_user_id uuid,approver_user_id uuid,note text,due_at timestamptz,requested_at timestamptz,state text,overdue boolean,content_changed boolean,decision_note text,decision_at timestamptz,request_aal text,decision_aal text)
145 +language sql stable security invoker set search_path=''
146 +as $$ select * from private.office_approval_work_queue_impl(p_organization_id) $$;
147 +revoke all on function public.office_approval_work_queue(uuid) from public,anon;
148 +grant execute on function public.office_approval_work_queue(uuid) to authenticated;