@davoodmehraban89 / Factor-easy / commits / ba4537c
Fork 0

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

test: define Slice B tenant RBAC invariants

davoodmehraban89 committed Sep 30, 2026 at 06:43 UTC ba4537c63489c266f2b6a7139234b136a41a98f1
1 file changed +41
scripts/slice-b-rbac-check.mjs new
+41
@@ -0,0 +1,41 @@
1 +import fs from 'node:fs';
2 +import path from 'node:path';
3 +
4 +const root=process.cwd();
5 +const migrationsDir=path.join(root,'supabase','migrations');
6 +const files=fs.readdirSync(migrationsDir).filter(x=>x.includes('organization_membership_rbac_foundation')&&x.endsWith('.sql'));
7 +if(files.length!==1)throw new Error(`expected exactly one Slice B RBAC migration, found ${files.length}`);
8 +const sql=fs.readFileSync(path.join(migrationsDir,files[0]),'utf8');
9 +const must=[
10 + 'create table public.organizations',
11 + 'create table public.organization_units',
12 + 'create table public.organization_members',
13 + 'create table public.member_module_permissions',
14 + 'create table public.organization_audit',
15 + 'add column if not exists organization_id uuid',
16 + 'private.current_organization_ids',
17 + 'private.has_org_capability',
18 + 'private.record_in_member_scope',
19 + 'private.record_confidentiality_allowed',
20 + "capability in ('read','create','edit','delete','approve','register','refer','archive','configure')",
21 + "scope_type in ('own','unit','branch','company','organization')",
22 + 'alter table public.organizations enable row level security',
23 + 'alter table public.organization_members enable row level security',
24 + 'alter table public.member_module_permissions enable row level security',
25 + 'alter table public.organization_audit enable row level security',
26 + 'grant select on public.organizations to authenticated',
27 + 'grant select on public.organization_members to authenticated',
28 + 'grant select on public.member_module_permissions to authenticated',
29 + 'create or replace function public.finora_sync_records',
30 + 'p_organization_id uuid',
31 + 'create or replace function public.office_refer_correspondence',
32 + 'create or replace function public.organization_set_member_permission',
33 + 'raise exception \'forbidden\' using errcode = \'42501\'',
34 + 'organization_audit_immutable',
35 + 'revoke all on function private.',
36 + "set search_path = ''"
37 +];
38 +for(const token of must)if(!sql.includes(token))throw new Error(`missing Slice B invariant: ${token}`);
39 +if(/drop\s+table\s+public\.records/i.test(sql))throw new Error('Slice B must not destructively replace records');
40 +if(/user_metadata/i.test(sql))throw new Error('authorization must not trust user_metadata');
41 +console.log(`Slice B RBAC migration invariants PASS (${files[0]})`);