3.33 Organisation Document Privileges — Per-Role Override Matrix
DOCUMENT PRIVILEGES MODEL 1. Role Def Org roles defined Read/Write/Admin 2. Scope Document types Per-category rules 3. Inherit Org→Team→User Hierarchical cascade 4. Enforce API gate + RLS Row-level security DOCUMENT PRIVILEGES MODEL — sequential pipeline through all phases. Per-organisation, per-role overrides on top of each document type’s default scope — for directors and administrators only.
Per-organisation, per-role overrides on top of each document type’s default scope — for directors and administrators only.
Section A In Plain English
Every document type in Paperclip (gas safety, EPC, ID, rent statements, title deeds…) has a default audience: who is allowed to see it. That default is the same for every agency. Sometimes your firm needs a tighter or looser rule — e.g. “managers should not open landlord-only title packs” or “staff can view inspection reports.” Document Privileges is the settings screen where the company director or administrator paints those exceptions on a grid. Leave a cell on Default and the global rule wins. Pick a specific level and that override is stored for your organisation only.
Think of document types as labelled drawers. The privileges screen does not rewrite files — it changes which staff keys open which drawers for your agency. Other organisations keep their own lock chart.
Who can change them?
| Role | Can open the screen? | Can save overrides? |
|---|---|---|
| Director | Yes | Yes |
| Administrator | Yes | Yes |
| Manager | No (403 / restricted banner) | No |
| Staff | No | No |
Section B User Guide — How to Use It
Open the Privileges Screen
-
1Sign in as director or administrator
Use your staff account on the portal (
app.estateaigents.com). Manager and staff roles are blocked server-side. -
2Go to Organisation settings
Navigate to Settings → Organisation (
/settings/organisation). Open the Document Permissions tab, or go directly to/settings/organisation/privileges. -
3Review the matrix by section
Document types are grouped: Compliance & Safety, Financial & Billing, Tenancy, Licensing, Insurance, Title & Ownership, Personal / ID, Property Info, Services, Appliances, Other.
-
4Change only what you need, then Save
The blue bar counts unsaved overrides. Click Save Changes. A green toast confirms. Leaving cells on Default keeps platform defaults.
Reading the Permission Matrix
Each row is one document type. Columns:
| Column | Meaning |
|---|---|
| Document Type | Human label plus machine key (e.g. gas_safety) |
| Scope | The catalogue default from tbl_document_types.default_scope |
| Admin / Director / Manager / Staff | Override dropdown per role. Empty = Default (no override row stored) |
Grey “Default” cells mean “use the document type’s built-in scope.” That is usually correct. Only override when your firm’s policy differs from the platform default.
Permission Levels
| Level | Who it is for | Chip colour (UI) |
|---|---|---|
shared | Visible broadly (typical compliance packs) | Green |
internal | Agency / internal staff | Blue |
landlord_only | Property owner side | Amber |
tenant_only | Tenant side (IDs, R2R) | Purple |
staff_only | Operational staff only | Gray |
| Default (empty) | No org override — catalogue default wins | Dashed gray |
Save and Revert Overrides
- Save sends only dirty cells as a PATCH batch.
- To revert a cell to default: set the dropdown back to Default and save. The API deletes that override row.
- Changes apply organisation-wide immediately after a successful save (no per-branch copy).
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
| Amber “Restricted area” banner | Your staff role is not director/administrator | Ask a director/admin to make the change |
| “Authentication required” | Session expired | Sign in again |
| “No organisation linked to your profile” | Staff row missing organisation_id | Fix staff identity linkage in Supabase / admin tools |
| Empty matrix / “No document types” | tbl_document_types has no active rows | Check catalogue seed / is_active |
| Save fails with invalid permission | Client sent a value outside the five levels | Hard refresh; report if reproducible |
Section C Developer Guide
What shipped
Commit 4785e826 — feat(settings): organisation document privileges screen — DB migration, API route, UI matrix. Portal paths under 03_technology/11_frontend/portal/.
flowchart LR
subgraph UI["Portal UI"]
P["/settings/organisation/privileges"]
O["/settings/organisation"]
end
subgraph API["Next.js route"]
G["GET document-permissions"]
K["PATCH document-permissions"]
end
subgraph DB["Supabase"]
DT["tbl_document_types"]
OV["tbl_organisation_document_permissions"]
ST["staff identity / role"]
end
O -->|tab| P
P --> G
P --> K
G --> DT
G --> OV
K --> OV
G --> ST
K --> ST
Data Model
Org-scoped overrides. Absence of a row = default. Unique key: (organisation_id, document_type, role).
| Column | Role |
|---|---|
organisation_id | Tenant scope for the override |
document_type | FK-style key into tbl_document_types.document_type |
role | One of administrator | director | manager | staff |
permission | One of shared | internal | landlord_only | tenant_only | staff_only |
Catalogue defaults live on tbl_document_types (document_type, label, default_scope, scope_is_fixed, ui_section, is_active). GET only returns is_active = true, ordered by ui_section, label.
API Contract
Route file: src/app/api/settings/organisation/document-permissions/route.ts
Auth gate: resolveOrgEditor — Supabase session user → resolveStaffIdentity → role ∈ {administrator, director} and non-null organisationId.
GET /api/settings/organisation/document-permissions
Returns merged view for the caller’s organisation:
{
"document_types": [
{
"document_type": "gas_safety",
"label": "Gas Safety Certificate",
"default_scope": "shared",
"scope_is_fixed": false,
"ui_section": "compliance",
"roles": [
{ "role": "administrator", "effective_permission": null },
{ "role": "director", "effective_permission": null },
{ "role": "manager", "effective_permission": "internal" },
{ "role": "staff", "effective_permission": null }
]
}
]
}
effective_permission: null means no override (UI shows Default). Non-null is the stored override — not necessarily equal to default_scope.
PATCH /api/settings/organisation/document-permissions
{
"overrides": [
{ "document_type": "gas_safety", "role": "manager", "permission": "internal" },
{ "document_type": "title_deed", "role": "staff", "permission": null }
]
}
permissionnon-null → upsert on conflictorganisation_id,document_type,rolepermissionnull → delete that override row (revert to default)- Validation: role ∈ VALID_ROLES; permission ∈ VALID_PERMISSIONS or null
- Response: same shape as GET (re-fetch after write)
UI Paths & Components
| Path | File |
|---|---|
/settings/organisation/privileges | src/app/settings/organisation/privileges/page.tsx |
/settings/organisation (tab link) | src/app/settings/organisation/page.tsx |
| Permission chips / labels | src/components/documents/PermissionChip.tsx |
UI keeps a local dirty map keyed ${document_type}::${role}. Save flattens dirty entries into the PATCH body. Section order is hard-coded in the page (SECTION_ORDER / SECTION_LABELS).
AuthZ Rules
ORG_EDITOR_ROLES = administrator | director. Non-editors get HTTP 403 on both GET and PATCH. Unauthenticated → 401. Missing org on staff → 404. The restricted-area UI is a convenience; the API is the real gate.
Pitfalls
- Default derivation vs override:
PermissionChipalso has helper sets that map document types → suggested levels for display elsewhere. The privileges screen does not re-derive those on save — it only reads/writes overrides + catalogue defaults. - scope_is_fixed: returned on GET but the matrix still renders dropdowns for all roles; if product later locks fixed scopes, gate the cell in UI using this flag.
- Effective permission semantics: GET’s
effective_permissionis the override value only (or null), not “override ?? default_scope”. Consumers that need the final resolved level must coalesce themselves. - Batch deletes: PATCH deletes run sequentially per null entry; large “reset all” payloads are many round-trips inside one request.
References & External Sources
| Source | What it covers | Link |
|---|---|---|
| Feature commit | UI matrix, API route, PermissionChip touch — ship SHA | 4785e826 on main |
| API route | GET/PATCH contract and org-editor gate | 03_technology/11_frontend/portal/src/app/api/settings/organisation/document-permissions/route.ts |
| Privileges page | Client matrix, dirty map, section grouping | 03_technology/11_frontend/portal/src/app/settings/organisation/privileges/page.tsx |
| PermissionChip | Level labels/colours and type-level derivation helpers | 03_technology/11_frontend/portal/src/components/documents/PermissionChip.tsx |
| Person / contact media | Related portal media access patterns (signed URLs) | 3.32 Contact Avatar Pipeline |
| Security architecture | Broader trust and access posture | Security Architecture Guide |