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

Plan Slice B tenant RBAC implementation

davoodmehraban89 committed Sep 30, 2026 at 06:42 UTC a6e8d2a14c658a6561bcd9603b8d2d4edfe07b00
1 file changed +100
docs/superpowers/plans/2026-09-30-slice-b-tenant-rbac.md new
+100
@@ -0,0 +1,100 @@
1 +# Slice B Tenant RBAC Implementation Plan
2 +
3 +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4 +
5 +**Goal:** Turn Finora's single-account record ownership into an organization-aware multi-user authorization boundary with module capabilities, row scopes, confidentiality and auditable office referrals while preserving existing users and accounting invariants.
6 +
7 +**Architecture:** Security metadata is relational and server-enforced; business payloads remain in `records`. Every existing account is backfilled into its own organization, licenses attach to that organization without removing the legacy billing owner, and records gain `organization_id` while retaining `owner_id` as creator/own-scope identity. RLS evaluates organization membership, commercial module entitlement, capability, row scope and confidentiality independently.
8 +
9 +**Tech Stack:** PostgreSQL 17 / Supabase RLS + Auth + Storage, vanilla JavaScript, GitHub Actions / Node 22 / Playwright.
10 +
11 +**Spec:** `docs/FINORA_MODULAR_ERP_OFFICE_AUTOMATION_V1.md`
12 +
13 +## Global Constraints
14 +
15 +- Effective access = commercial module entitlement ∩ organization membership permission ∩ record scope/confidentiality.
16 +- Existing production users and records must be backfilled without data loss; each existing account starts as owner of an isolated organization.
17 +- Posted accounting history remains immutable and balanced.
18 +- Expired/cancelled licenses retain read-only access to entitled history but cannot write.
19 +- UI hiding is never the authorization boundary.
20 +- `SECURITY DEFINER` helpers live in `private`, use `search_path=''`, are schema-qualified and have explicit execute grants.
21 +- New exposed tables use explicit grants plus RLS because Supabase is moving to non-automatic Data API grants.
22 +
23 +## Review Focus
24 +
25 +- A member of organization A must never read or mutate organization B records.
26 +- A member with `read` but not `edit` must not mutate shared records even if the UI is bypassed.
27 +- Company/unit/branch scoped permissions must deny records missing or mismatching the scope key.
28 +- Confidential correspondence above the member's clearance must be invisible even with module read permission.
29 +- Expired organization license must preserve authorized reads and deny writes/referrals/permission changes.
30 +
31 +---
32 +
33 +### Task 1: Organization identity and migration-safe authorization schema
34 +
35 +**Files:**
36 +- Create: `supabase/migrations/<production-version>_organization_membership_rbac_foundation.sql`
37 +- Create: `scripts/slice-b-rbac-check.mjs`
38 +- Modify: `.github/workflows/quality.yml`
39 +
40 +**Interfaces:**
41 +- Produces relational `organizations`, `organization_units`, `organization_members`, `member_module_permissions`, `organization_audit`; `licenses.organization_id`; `records.organization_id`; private authorization helpers; organization-aware RLS and sync RPC.
42 +- Existing clients may continue reading their own backfilled organization during the transition.
43 +
44 +- [ ] Write static/invariant tests for schema, backfill, grants, RLS, capability/scope/confidentiality helpers and audit immutability.
45 +- [ ] Run test and verify RED because the migration is absent.
46 +- [ ] Implement the migration with additive/backfill-first DDL and no destructive financial-history rewrite.
47 +- [ ] Run the invariant test and existing static suites; expect PASS.
48 +- [ ] Commit.
49 +
50 +### Task 2: Client organization context and capability-aware workspace
51 +
52 +**Files:**
53 +- Modify: `js/core.js`
54 +- Modify: `js/sync.js`
55 +- Modify: `js/platform-modules.js`
56 +- Modify: `scripts/slice-b-rbac-check.mjs`
57 +
58 +**Interfaces:**
59 +- Produces `currentUser.organizations`, `currentUser.organizationId`, `currentUser.modulePermissions`, `hasFinoraCapability(module, capability)` and organization-aware `pullAll` / `finora_sync_records` calls.
60 +- Consumes Task 1 membership/permission read RPC or tables and organization-aware sync contract.
61 +
62 +- [ ] Extend tests first for active organization, module/capability gating and organization-aware sync payload.
63 +- [ ] Verify RED.
64 +- [ ] Implement minimal client context and capability gating while keeping legacy owner-facing helpers compatible.
65 +- [ ] Verify new tests and existing quality checks PASS.
66 +- [ ] Commit.
67 +
68 +### Task 3: Office shared inbox, referral and permission administration foundation
69 +
70 +**Files:**
71 +- Modify: `js/office-automation.js`
72 +- Modify: `js/platform-modules.js`
73 +- Modify: `scripts/platform-office.spec.js`
74 +- Modify: `scripts/slice-b-rbac-check.mjs`
75 +
76 +**Interfaces:**
77 +- Produces capability-gated office actions: ordinary read/draft, secretariat register/archive, module-admin configure, manager refer; referral writes go through server RPC and immutable audit.
78 +- Consumes Task 1 authorization/referral RPCs and Task 2 `hasFinoraCapability`.
79 +
80 +- [ ] Add failing DOM/static tests for hidden/disabled actions without capability and referral surface for authorized users.
81 +- [ ] Verify RED.
82 +- [ ] Implement shared inbox/referral and role-aware office controls without enabling unverified OCR/signature features.
83 +- [ ] Verify DOM/static suites PASS.
84 +- [ ] Commit.
85 +
86 +### Task 4: Production migration, negative/positive security tests and release evidence
87 +
88 +**Files:**
89 +- Archive exact applied SQL under `supabase/migrations/`.
90 +- Modify: `docs/FINORA_MODULAR_ERP_OFFICE_AUTOMATION_V1.md`
91 +- Modify: `PROJECT_STATE.md`
92 +
93 +**Interfaces:**
94 +- Produces verified production Slice B checkpoint and exact next work for Slice C.
95 +
96 +- [ ] Apply the reviewed migration to Supabase production and verify migration history.
97 +- [ ] Run positive/negative SQL checks for cross-tenant isolation, read-vs-edit capability, confidentiality, scope, expired-license write denial, referral audit immutability and legacy-owner backfill.
98 +- [ ] Run Supabase security/performance advisors and fix new material findings.
99 +- [ ] Open/verify PR CI (Quality/Security) and merge only after green; verify Pages on main.
100 +- [ ] Update architecture/state with exact migration, commit, CI and unresolved external boundaries.