سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 19:30 UTC
fix(security): authorize lifecycle RPCs and expose only authenticated execution
davoodmehraban89 committed
Nov 18, 2026 at 09:04 UTC
d646139ae5aaca724d9a4a3200335a88c12a60da
1 file changed
+112
supabase/migrations/20261118102000_document_lifecycle_security.sql
new
+112
@@ -0,0 +1,112 @@
1
+-- Security hardening for the lifecycle RPCs.
2
+-- The functions are callable by authenticated users only and delegate tenant/role
3
+-- authorization to the same Phase-1 authorization gate used by journal RPCs.
4
+CREATE OR REPLACE FUNCTION public.finora_document_identity(
5
+ p_organization_id uuid,p_collection text,p_id text,p_temporary_number text DEFAULT NULL)
6
+RETURNS jsonb
7
+LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
8
+AS $$
9
+DECLARE r public.records%rowtype; n bigint; t text;
10
+BEGIN
11
+ IF auth.uid() IS NULL THEN RAISE EXCEPTION 'FINORA_AUTH_REQUIRED' USING ERRCODE='42501'; END IF;
12
+ PERFORM private.finora_phase1a_authorize(p_organization_id,'create',
13
+ CASE WHEN p_collection='invoices' THEN 'sale' WHEN p_collection='purchases' THEN 'purchase' ELSE 'manual' END);
14
+ SELECT * INTO r FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id FOR UPDATE;
15
+ IF NOT FOUND THEN RAISE EXCEPTION 'FINORA_DOCUMENT_NOT_FOUND' USING ERRCODE='P0002'; END IF;
16
+ n:=NULLIF(r.data->>'atefNumber','')::bigint;
17
+ IF n IS NULL THEN n:=public.finora_next_atef(p_organization_id); END IF;
18
+ t:=nullif(trim(coalesce(p_temporary_number,r.data->>'temporaryNumber',r.data->>'number','')),'');
19
+ IF t IS NULL THEN t:='TMP-'||n::text; END IF;
20
+ UPDATE public.records SET data=(r.data||jsonb_build_object('atefNumber',n,'temporaryNumber',t,'documentStatus','draft')-'number'-'postedAt'-'postedBy')
21
+ WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id;
22
+ INSERT INTO public.finora_document_lifecycle_audit(organization_id,atef_number,document_collection,document_id,event_type,temporary_number,actor_id)
23
+ VALUES(p_organization_id,n,p_collection,p_id,'IDENTITY_ASSIGNED',t,auth.uid())
24
+ ON CONFLICT DO NOTHING;
25
+ RETURN (SELECT data FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id);
26
+END $$;
27
+
28
+CREATE OR REPLACE FUNCTION public.finora_assign_permanent_document_number(
29
+ p_organization_id uuid,p_collection text,p_id text,p_company_id text,p_fiscal_year_id text,p_document_type text)
30
+RETURNS jsonb
31
+LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
32
+AS $$
33
+DECLARE r public.records%rowtype; n bigint; status text;
34
+BEGIN
35
+ IF auth.uid() IS NULL THEN RAISE EXCEPTION 'FINORA_AUTH_REQUIRED' USING ERRCODE='42501'; END IF;
36
+ PERFORM private.finora_phase1a_authorize(p_organization_id,'create',
37
+ CASE WHEN p_collection='invoices' THEN 'sale' WHEN p_collection='purchases' THEN 'purchase' ELSE 'manual' END);
38
+ SELECT * INTO r FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id FOR UPDATE;
39
+ IF NOT FOUND THEN RAISE EXCEPTION 'FINORA_DOCUMENT_NOT_FOUND' USING ERRCODE='P0002'; END IF;
40
+ status:=coalesce(r.data->>'status',r.data->>'documentStatus',r.data->>'accountingStatus','draft');
41
+ IF status<>'posted' THEN RAISE EXCEPTION 'FINORA_DOCUMENT_NOT_POSTED' USING ERRCODE='23514'; END IF;
42
+ IF coalesce(r.data->>'number','')<>'' THEN
43
+ RETURN r.data;
44
+ END IF;
45
+ n:=public.finora_next_permanent_number(p_organization_id,p_company_id,p_fiscal_year_id,p_document_type);
46
+ UPDATE public.records SET data=(r.data||jsonb_build_object('number',n::text,'documentStatus','posted','accountingStatus',CASE WHEN r.data ? 'accountingStatus' THEN 'posted' ELSE r.data->>'accountingStatus' END))
47
+ WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id;
48
+ INSERT INTO public.finora_document_lifecycle_audit(organization_id,atef_number,document_collection,document_id,event_type,temporary_number,permanent_number,actor_id,metadata)
49
+ VALUES(p_organization_id,(r.data->>'atefNumber')::bigint,p_collection,p_id,'POSTED',r.data->>'temporaryNumber',n::text,auth.uid(),jsonb_build_object('document_type',p_document_type))
50
+ ON CONFLICT DO NOTHING;
51
+ RETURN (SELECT data FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_id);
52
+END $$;
53
+
54
+CREATE OR REPLACE FUNCTION public.finora_delete_draft_source(
55
+ p_organization_id uuid,p_collection text,p_source_id text)
56
+RETURNS jsonb
57
+LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
58
+AS $$
59
+DECLARE s public.records%rowtype; v public.records%rowtype; a bigint;
60
+BEGIN
61
+ IF auth.uid() IS NULL THEN RAISE EXCEPTION 'FINORA_AUTH_REQUIRED' USING ERRCODE='42501'; END IF;
62
+ PERFORM private.finora_phase1a_authorize(p_organization_id,'delete',
63
+ CASE WHEN p_collection='invoices' THEN 'sale' WHEN p_collection='purchases' THEN 'purchase' ELSE 'manual' END);
64
+ SELECT * INTO s FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_source_id FOR UPDATE;
65
+ IF NOT FOUND THEN RAISE EXCEPTION 'FINORA_SOURCE_NOT_FOUND' USING ERRCODE='P0002'; END IF;
66
+ IF coalesce(s.data->>'documentStatus',s.data->>'accountingStatus',s.data->>'status','draft')<>'draft'
67
+ THEN RAISE EXCEPTION 'FINORA_ONLY_DRAFT_CAN_BE_DELETED' USING ERRCODE='23514'; END IF;
68
+ a:=NULLIF(s.data->>'atefNumber','')::bigint;
69
+ IF coalesce(s.data->>'journalVoucherId','')<>'' THEN
70
+ SELECT * INTO v FROM public.records WHERE organization_id=p_organization_id AND collection='journalVouchers' AND id=s.data->>'journalVoucherId' FOR UPDATE;
71
+ IF FOUND AND coalesce(v.data->>'status','draft')<>'draft' THEN RAISE EXCEPTION 'FINORA_LINKED_VOUCHER_NOT_DRAFT' USING ERRCODE='23514'; END IF;
72
+ IF FOUND THEN
73
+ INSERT INTO public.finora_document_lifecycle_audit(organization_id,atef_number,document_collection,document_id,event_type,temporary_number,related_collection,related_id,actor_id,original_status,metadata)
74
+ VALUES(p_organization_id,(v.data->>'atefNumber')::bigint,'journalVouchers',v.id,'DELETED_WITH_SOURCE',v.data->>'temporaryNumber',p_collection,p_source_id,auth.uid(),'draft',jsonb_build_object('source_atef',a))
75
+ ON CONFLICT DO NOTHING;
76
+ DELETE FROM public.records WHERE organization_id=p_organization_id AND collection IN ('journalLineDimensions','journalLines') AND data->>'voucherId'=v.id;
77
+ DELETE FROM public.records WHERE organization_id=p_organization_id AND collection='journalVouchers' AND id=v.id;
78
+ END IF;
79
+ END IF;
80
+ INSERT INTO public.finora_document_lifecycle_audit(organization_id,atef_number,document_collection,document_id,event_type,temporary_number,actor_id,original_status,metadata)
81
+ VALUES(p_organization_id,a,p_collection,p_source_id,'DELETED',s.data->>'temporaryNumber',auth.uid(),'draft',jsonb_build_object('cascade_voucher_id',s.data->>'journalVoucherId'))
82
+ ON CONFLICT DO NOTHING;
83
+ DELETE FROM public.records WHERE organization_id=p_organization_id AND collection=p_collection AND id=p_source_id;
84
+ RETURN jsonb_build_object('deleted',true,'atefNumber',a,'sourceId',p_source_id);
85
+END $$;
86
+
87
+CREATE OR REPLACE FUNCTION public.finora_delete_draft_voucher(p_organization_id uuid,p_voucher_id text)
88
+RETURNS jsonb
89
+LANGUAGE plpgsql SECURITY DEFINER SET search_path=''
90
+AS $$
91
+DECLARE v public.records%rowtype;
92
+BEGIN
93
+ IF auth.uid() IS NULL THEN RAISE EXCEPTION 'FINORA_AUTH_REQUIRED' USING ERRCODE='42501'; END IF;
94
+ PERFORM private.finora_phase1a_authorize(p_organization_id,'delete','manual');
95
+ SELECT * INTO v FROM public.records WHERE organization_id=p_organization_id AND collection='journalVouchers' AND id=p_voucher_id FOR UPDATE;
96
+ IF NOT FOUND THEN RAISE EXCEPTION 'FINORA_VOUCHER_NOT_FOUND' USING ERRCODE='P0002'; END IF;
97
+ IF coalesce(v.data->>'status','draft')<>'draft' THEN RAISE EXCEPTION 'FINORA_ONLY_DRAFT_CAN_BE_DELETED' USING ERRCODE='23514'; END IF;
98
+ IF coalesce(v.data->>'sourceType','manual') IN ('sale','purchase') AND coalesce(v.data->>'sourceId','')<>'' THEN
99
+ RAISE EXCEPTION 'FINORA_DELETE_SOURCE_REQUIRED: ابتدا سند تجاری منبع را حذف کنید' USING ERRCODE='23514';
100
+ END IF;
101
+ DELETE FROM public.records WHERE organization_id=p_organization_id AND collection IN ('journalLineDimensions','journalLines') AND data->>'voucherId'=p_voucher_id;
102
+ DELETE FROM public.records WHERE organization_id=p_organization_id AND collection='journalVouchers' AND id=p_voucher_id;
103
+ INSERT INTO public.finora_document_lifecycle_audit(organization_id,atef_number,document_collection,document_id,event_type,temporary_number,actor_id,original_status)
104
+ VALUES(p_organization_id,(v.data->>'atefNumber')::bigint,'journalVouchers',p_voucher_id,'DELETED_DRAFT_VOUCHER',v.data->>'temporaryNumber',auth.uid(),'draft')
105
+ ON CONFLICT DO NOTHING;
106
+ RETURN jsonb_build_object('deleted',true,'voucherId',p_voucher_id);
107
+END $$;
108
+
109
+GRANT EXECUTE ON FUNCTION public.finora_document_identity(uuid,text,text,text) TO authenticated;
110
+GRANT EXECUTE ON FUNCTION public.finora_assign_permanent_document_number(uuid,text,text,text,text,text) TO authenticated;
111
+GRANT EXECUTE ON FUNCTION public.finora_delete_draft_source(uuid,text,text) TO authenticated;
112
+GRANT EXECUTE ON FUNCTION public.finora_delete_draft_voucher(uuid,text) TO authenticated;