سامانه صدور فاکتور و حسابداری
Allow active members to read core organization context
davoodmehraban89 committed
Sep 30, 2026 at 07:00 UTC
21209becdb02fbc4049294fc8521e11cb81cd74a
1 file changed
+20
supabase/migrations/20260930033010_allow_member_core_read_context.sql
new
+20
@@ -0,0 +1,20 @@
1
+create or replace function private.has_org_capability(p_organization_id uuid,p_module_key text,p_capability text) returns boolean
2
+language sql stable security definer set search_path='' as $$
3
+ select exists(select 1 from public.organization_members m
4
+ where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active'
5
+ and ((p_module_key='core' and p_capability='read') or m.is_owner or exists(select 1 from public.member_module_permissions p where p.organization_id=p_organization_id and p.member_id=m.id and (p.module_key=p_module_key or p.module_key='full_suite') and p_capability=any(p.capabilities))))
6
+$$;
7
+create or replace function private.record_in_member_scope(p_organization_id uuid,p_module_key text,p_owner_id uuid,p_data jsonb,p_capability text) returns boolean
8
+language sql stable security definer set search_path='' as $$
9
+ select exists(select 1 from public.organization_members m
10
+ where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active'
11
+ and ((p_module_key='core' and p_capability='read') or m.is_owner or exists(select 1 from public.member_module_permissions p where p.organization_id=p_organization_id and p.member_id=m.id and (p.module_key=p_module_key or p.module_key='full_suite') and p_capability=any(p.capabilities) and (p.scope_type='organization' or (p.scope_type='own' and p_owner_id=(select auth.uid())) or (p.scope_type='company' and p.scope_id=coalesce(p_data->>'companyId',p_data->>'company_id')) or (p.scope_type='branch' and p.scope_id=coalesce(p_data->>'branchId',p_data->>'branch_id')) or (p.scope_type='unit' and p.scope_id=coalesce(p_data->>'unitId',p_data->>'unit_id'))))))
12
+$$;
13
+create or replace function private.record_confidentiality_allowed(p_organization_id uuid,p_module_key text,p_data jsonb) returns boolean
14
+language sql stable security definer set search_path='' as $$
15
+ select case when p_module_key='core' then p_organization_id in (select private.current_organization_ids()) else exists(
16
+ select 1 from public.organization_members m
17
+ where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active'
18
+ and (m.is_owner or coalesce((select max(p.confidentiality_level) from public.member_module_permissions p where p.organization_id=p_organization_id and p.member_id=m.id and (p.module_key=p_module_key or p.module_key='full_suite')),-1) >= (case lower(coalesce(p_data->>'confidentiality','normal')) when 'محرمانه' then 1 when 'confidential' then 1 when 'خیلی محرمانه' then 2 when 'very_confidential' then 2 when 'سری' then 3 when 'secret' then 3 else 0 end))
19
+ ) end
20
+$$;