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

Document Slice B tenant RBAC verification

davoodmehraban89 committed Sep 30, 2026 at 06:57 UTC 013e0e77dc16bcd4f79f19f47d4f024bfb12489f
1 file changed +77
docs/SLICE_B_TENANT_RBAC_VERIFICATION.md new
+77
@@ -0,0 +1,77 @@
1 +# Slice B — Organization Tenant + RBAC Verification
2 +
3 +Date: 2026-09-30
4 +Status: implementation complete; PR CI is the release gate.
5 +
6 +## Delivered boundary
7 +
8 +Finora now separates four authorization dimensions that must all allow an operation:
9 +
10 +1. **Organization membership** — `organizations`, `organization_units`, `organization_members`.
11 +2. **Commercial entitlement** — the organization's existing license and module list.
12 +3. **Member authorization** — `member_module_permissions` with `read/create/edit/delete/approve/register/refer/archive/configure`.
13 +4. **Record boundary** — `own/unit/branch/company/organization` scope plus confidentiality clearance 0..3.
14 +
15 +Existing accounts were migrated conservatively: every existing license received an isolated organization, the license owner became that organization's owner, and every existing `records` row was backfilled to the matching organization without rewriting posted accounting payloads. The record primary key is now `(organization_id, collection, id)` while `owner_id` remains the creator/own-scope identity.
16 +
17 +## Production migrations
18 +
19 +- `20260930031853_organization_membership_rbac_foundation`
20 +- `20260930031942_office_shared_rbac_and_referrals`
21 +- `20260930032615_slice_b_rbac_fk_indexes`
22 +- `20260930032654_harden_legacy_office_storage_member_writes`
23 +
24 +Exact applied SQL is archived under `supabase/migrations/`.
25 +
26 +## Server enforcement
27 +
28 +`records` SELECT/INSERT/UPDATE/DELETE policies now evaluate organization membership, organization license/module entitlement, capability, row scope and confidentiality. Writes additionally require an active organization license. Expired/cancelled organizations retain authorized historical reads.
29 +
30 +`finora_sync_records` accepts an explicit `p_organization_id`; it refuses organizations outside the caller's memberships and refuses inactive-license writes. The client pulls only the selected organization.
31 +
32 +Organization owners have implicit full member authorization inside commercially entitled modules. Non-owner members receive explicit module permissions. A module manager with `configure` can assign permissions for that module but cannot turn on a commercially unlicensed module.
33 +
34 +## Office automation
35 +
36 +- `office_register_correspondence` is organization-aware and requires active office entitlement + `register` capability.
37 +- `office_refer_correspondence` requires active office entitlement + `refer` capability and only targets active members of the same organization.
38 +- Referral records are immutable after insertion.
39 +- Permission changes and referrals write `organization_audit`; audit rows are immutable.
40 +- New attachment paths are `<organization>/<uploader>/<correspondence>/<file>` and Storage access resolves back to correspondence RLS/confidentiality.
41 +- Legacy UID-prefixed attachment reads remain available for historical compatibility. Legacy-path writes are restricted to organization owners with active office entitlement; ordinary members must use the organization-aware path.
42 +
43 +## Client behavior
44 +
45 +`js/organization-rbac.js` loads organization memberships, selected organization, organization license and member permissions before app entry. Non-owner module navigation is derived from both commercial entitlement and member `read` capability. Organization-aware sync includes `p_organization_id`.
46 +
47 +The organization access surface lets an organization owner add an already-registered Finora user by email and lets owners/module-configurers assign capability, scope and confidentiality. External invitation/email onboarding is intentionally not claimed yet.
48 +
49 +`js/office-rbac.js` gates registry configuration, draft creation, registration and referral by capability and uses organization-aware attachment paths. Authorized office users see the shared records returned by server RLS; referral is exposed only to members with `refer`.
50 +
51 +## Database verification executed in production transactions and rolled back
52 +
53 +- own-scope member could not see another user's organization record;
54 +- organization-scope `read` exposed normal correspondence but not correspondence above confidentiality clearance;
55 +- increasing clearance exposed the confidential record;
56 +- read-only member updated **0 rows**;
57 +- cancelled-license organization owner could read historical correspondence and updated **0 rows**;
58 +- referral RPC succeeded for a member with `read+refer` in organization scope;
59 +- non-configure member could not read organization audit;
60 +- referral mutation hit the immutable trigger (`42501`);
61 +- organization-audit mutation hit the immutable trigger (`42501`).
62 +
63 +All test data was inside transactions and rolled back.
64 +
65 +## Advisor review
66 +
67 +Security advisor after Slice B reports the same seven reviewed public `SECURITY DEFINER` warnings from the pre-existing admin/license helper set plus leaked-password protection disabled; Slice B introduced no new exposed `SECURITY DEFINER` RPC warning. Performance advisor initially identified four unindexed new foreign keys; migration `20260930032615` added the covering indexes. The remaining performance findings are only newly-unused-index informational notices immediately after creation.
68 +
69 +## Still not claimed
70 +
71 +- invitation of a person who does not yet have a Finora account;
72 +- SSO/SCIM/enterprise identity provisioning;
73 +- real Persian OCR accuracy;
74 +- qualified digital signature / certificate validation;
75 +- external ECE/email/webhook gateways;
76 +- full visual workflow designer, delegation/substitution and SLA escalation;
77 +- authenticated multi-device production browser E2E with two real user sessions (server RLS was tested by authenticated-role simulation).