@davoodmehraban89 / Factor-easy / commits / 56a9a87
Fork 0

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

Harden legacy office attachment writes for members

davoodmehraban89 committed Sep 30, 2026 at 06:57 UTC 56a9a87e53c53f415fd8dda88ed2783ef2463c40
1 file changed +17
supabase/migrations/20260930032654_harden_legacy_office_storage_member_writes.sql new
+17
@@ -0,0 +1,17 @@
1 +create or replace function private.can_access_office_storage(p_name text,p_capability text) returns boolean
2 +language plpgsql stable security definer set search_path='' as $$
3 +declare parts text[]:=string_to_array(p_name,'/'); v_org uuid; v_cor text; v_row public.records%rowtype;
4 +begin
5 + if array_length(parts,1) is null then return false; end if;
6 + if parts[1]=(select auth.uid())::text then
7 + if p_capability='read' then return private.has_module_entitlement('office_automation'); end if;
8 + return exists(select 1 from private.current_organization_ids() org_id where private.is_org_owner(org_id) and private.organization_has_module_entitlement(org_id,'office_automation',true));
9 + end if;
10 + if parts[1] !~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$' then return false; end if;
11 + v_org:=parts[1]::uuid;
12 + if p_capability='create' then return parts[2]=(select auth.uid())::text and private.organization_has_module_entitlement(v_org,'office_automation',true) and private.has_org_capability(v_org,'office_automation','create'); end if;
13 + v_cor:=parts[3];
14 + select * into v_row from public.records where organization_id=v_org and collection='correspondence' and id=v_cor;
15 + if not found then return false; end if;
16 + return private.can_access_record(v_org,'correspondence',v_row.owner_id,v_row.data,p_capability);
17 +end $$;