Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions plans/projects/document-builder/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Five-wave delivery, derived from the `Wave: N` annotations in each Linear ticket
| 13 | Wave 2 — comments + mentions + sink | OBJ-84, OBJ-85, OBJ-86 | sequential | OBJ-82's branch | ✅ OBJ-84 Done — PR #TBD merged. ✅ OBJ-85 Done — PR #31 merged. ✅ OBJ-86 Done — PR #32 merged. ✅ Step 13 complete. |
| 14 | Wave 2 — dynamic table impl | OBJ-73, OBJ-74, OBJ-75 | sequential | `main` (OBJ-71 merged) | ✅ OBJ-73 Done — PR #18 merged. ✅ OBJ-74 Done — PR #20 merged. ✅ OBJ-75 Done — PR #23 merged. ✅ Step 14 complete. |
| 15 | Wave 2 — PDF export | OBJ-87, OBJ-88, OBJ-100, OBJ-89 | sequential | OBJ-73's branch | ✅ OBJ-87 Done — PR #33 merged. ✅ OBJ-88 Done — PR #34 merged. ✅ OBJ-100 Done — PR #35 merged. ✅ OBJ-89 Done — PR #36 merged. ✅ Step 15 complete. |
| 16 | Wave 3 — share platform | OBJ-96, OBJ-97, OBJ-98, OBJ-99 | sequential | OBJ-100's branch | ✅ OBJ-96 Done — PR #37 merged. ✅ OBJ-97 Done — PR #38 merged. OBJ-98, OBJ-99 not started. |
| 16 | Wave 3 — share platform | OBJ-96, OBJ-97, OBJ-98, OBJ-99 | sequential | OBJ-100's branch | ✅ OBJ-96 Done — PR #37 merged. ✅ OBJ-97 Done — PR #38 merged. ✅ OBJ-98 Done — PR #TBD merged. OBJ-99 not started. |
| 17 | Wave 3 — test UI build-out | OBJ-90, OBJ-91, OBJ-92, OBJ-93, OBJ-94, OBJ-95, OBJ-105 | sequential | OBJ-98's branch | Not started |
| 14b | Wave 2 — templates chain | OBJ-102, OBJ-103, OBJ-104 | sequential | `main` (OBJ-69 merged) | ✅ OBJ-102 Done — PR #16 merged. ✅ OBJ-103 Done — PR #21 merged. ✅ OBJ-104 Done — PR #24 merged. ✅ Step 14b complete. |

Expand Down Expand Up @@ -1497,11 +1497,11 @@ Replace `AllowAllAuthorizationPolicy` (OBJ-70's default) with `DefaultAuthorizat
**Work Items**
| # | Item | Status |
|---|------|--------|
| 1 | `DefaultAuthorizationPolicy` implementation | Not Started |
| 2 | Action enum (`Read`, `Edit`, `Comment`, `Share`) | Not Started |
| 3 | Authz hook in every existing endpoint | Not Started |
| 4 | Personas × actions integration test matrix (4 × 3 = 12 cases) | Not Started |
| 5 | `scripts/validate-obj-98.ps1` runs the matrix test | Not Started |
| 1 | `DefaultAuthorizationPolicy` implementation | Done |
| 2 | Action enum (`Read`, `Edit`, `Comment`, `Share`) | Done |
| 3 | Authz hook in every existing endpoint | Done |
| 4 | Personas × actions integration test matrix (4 × 3 = 12 cases) | Done |
| 5 | `scripts/validate-obj-98.ps1` runs the matrix test | Done |

**Acceptance Criteria**
- 12-case persona × action matrix passes: owner = full access; editor = read/edit/comment but not share; commenter = read/comment but not edit; viewer = read only.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# OBJ-98 — Authorization policy in all read/write paths

## Summary

Replaces `AllowAllAuthorizationPolicy` (OBJ-70 dev default) with `DefaultAuthorizationPolicy` that enforces tenant match, ownership, and share role hierarchy on every read/write endpoint. Failures return `403 { code: "forbidden" }`.

## Action Enum

```csharp
namespace DocumentBuilder.Core.Auth;

public enum AuthorizationAction
{
Read,
Edit,
Comment,
Share
}
```

(`Write` removed; `Edit` and `Comment` added.)

## Role Hierarchy

| Role | Read | Edit | Comment | Share |
|-----------|------|------|---------|-------|
| **owner** | ✓ | ✓ | ✓ | ✓ |
| editor | ✓ | ✓ | ✓ | |
| commenter | ✓ | | ✓ | |
| viewer | ✓ | | | |

Ownership: `document.CreatedBy == userId` → full access.
Share role: resolved from `document_shares` table for the current user + document.

## DefaultAuthorizationPolicy

**Location:** `DocumentBuilder.Infrastructure.Auth.DefaultAuthorizationPolicy`

**DI lifetime:** `Scoped` (requires `AppDbContext`, which is also Scoped).

**Algorithm:**
1. Cast `document` to `Document` entity — deny if wrong type.
2. Check `doc.TenantId == tenantAccessor.CurrentTenantId` — deny if mismatch.
3. If `doc.CreatedBy == userId` → allow (owner).
4. Query `document_shares` for `(doc.Id, userId)` → get role.
5. No share row → deny.
6. Apply role hierarchy table above.

## Endpoints and Actions

| Method | Path | Action Required |
|--------|------|-----------------|
| POST | /api/documents | _(no authz — creating new, current user becomes owner)_ |
| GET | /api/documents | _(list — returns all tenant docs; owner filter via service)_ |
| GET | /api/documents/{id} | Read |
| PUT | /api/documents/{id} | Edit |
| DELETE | /api/documents/{id} | Share (owner-only) |
| GET | /api/documents/{id}/versions | Read |
| GET | /api/documents/{id}/versions/{n} | Read |
| POST | /api/documents/{id}/restore | Edit |
| GET | /api/documents/{id}/diff | Read |
| GET | /api/documents/{id}/render-html | Read |
| POST | /api/documents/{id}/export-html | Read |
| POST | /api/documents/{id}/export-pdf | Read |
| GET | /api/documents/{id}/comments | Read |
| POST | /api/documents/{id}/comments | Comment |
| POST | /api/comments/{id}/replies | Comment (via parent comment's document) |
| PATCH | /api/comments/{id} | Comment (via parent comment's document) |
| POST | /api/documents/{id}/shares | Share (via ShareService) |
| GET | /api/documents/{id}/shares | Share |
| DELETE | /api/documents/{id}/shares/{shareId} | Share (via ShareService) |
| GET | /ws/docs/{id} | Read |

Exempt (no authz): `GET /`, `GET /health`, `GET /api/debug/pdf-smoke`, `GET /api/shared/{token}`, `GET /api/dynamic-tables/*`, `/api/templates/*`, `GET /api/whoami`, `GET /api/controls`.

## Error Response

```json
{ "code": "forbidden" }
```

HTTP 403. Some endpoints (share POST/DELETE) use `UnauthorizedAccessException` caught by handler, returning plain 403 with empty body — both are valid.

## DI Registration

**Production (Program.cs):**
```csharp
services.AddScoped<IAuthorizationPolicy, DefaultAuthorizationPolicy>();
```

**Tests that need open access:**
```csharp
services.AddSingleton<IAuthorizationPolicy, AllowAllAuthorizationPolicy>();
```

`AllowAllAuthorizationPolicy` remains in `DocumentBuilder.Infrastructure.Auth` for test use.

## 12-Case Matrix Tested By

`tests/DocumentBuilder.Tests/Integration/AuthzPolicyMatrixTests.cs` — 16 test cases (4 personas × 4 actions, some actions tested for both allowed and denied).

Key integration test methods:
- `Owner_Read/Edit/Comment/Share_Allowed`
- `Editor_Read/Edit/Comment_Allowed`, `Editor_Share_Denied`
- `Commenter_Read/Comment_Allowed`, `Commenter_Edit_Denied_AndDbUnchanged`, `Commenter_Share_Denied`
- `Viewer_Read_Allowed`, `Viewer_Edit/Comment/Share_Denied`
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Outcome — OBJ-98: M9b.3 — Authorization policy callback
Branch: objectgyan/obj-98-m9b3-authorization-policy-callback-in-all-readwrite-paths Date: 2026-05-17
Build: PASS Tests: 282/282 total
Validation script: scripts/validate-obj-98.ps1 Exit: 0
- validation/obj-98-db.txt — document_shares columns verified, DB count unchanged after blocked write
- validation/obj-98-api.txt — 12-case persona×action matrix all pass (owner/editor/commenter/viewer × Read/Edit/Comment/Share)
Acceptance Criteria → evidence:
- 12-case persona×action matrix passes — proved via 16 integration tests in AuthzPolicyMatrixTests.cs + validation script live API checks
- Failed write leaves DB unchanged — proved via document count assertion before/after blocked PUT in validation script + Commenter_Edit_Denied_AndDbUnchanged integration test
- Custom policy overridable in tests — proved by AllowAllAuthorizationPolicy still registered and usable; AuthzPolicyMatrixTests uses ConfigureServices override to inject DefaultAuthorizationPolicy explicitly
VERDICT: PASS
Loading
Loading