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
29 changes: 29 additions & 0 deletions .changeset/36186-36191-comment-doors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
'@objectstack/plugin-audit': patch
'@objectstack/core': patch
'@object-ui/app-shell': patch
'@object-ui/plugin-detail': patch
'@object-ui/react': patch
'@object-ui/core': patch
---

feat(comments): a member resolves and reopens any thread they can read, and deletes their own comments

Clause-②: yes (widening) — `@objectstack/plugin-audit`'s `AuditPlugin` mounts two new HTTP routes, `PUT /api/v1/comments/:id/resolution` and `DELETE /api/v1/comments/:id`, and the stamp of who resolved a thread now reads a system write's `provenance.attributedUserId`. `@objectstack/core`'s `OwnRowDeleteDoor` adds an optional member, `refuseRemoval(row, engine, context)`, asked after the owner check. `@object-ui/react`'s `DiscussionContextValue` adds the optional `ownCommentDeletesOnly`; `@object-ui/plugin-detail`'s `RecordCommentsData` adds the optional `ownDeletesOnly`, and `RecordCommentThreadProps['comments']` may carry it; `@object-ui/core`'s affordance-to-grant map adds the row `feedCommentDeleteOwn`. Nothing existing is removed or re-typed.
Metadata-protocol: none

Two server doors carry the maintainer's comment permission rulings past the platform's row floor (`owner_only_writes`, which lets a rank-and-file member update only the rows they created) and past the delete grant no every-member set may hold (ADR-0090 D5). Neither changes the floor, `sys_comment_moderation` or any permission set, and the data API answers exactly what it answered before.

- **Resolve and reopen: `PUT /api/v1/comments/:id/resolution`** with the body `{ "resolved": true }` or `{ "resolved": false }` and nothing else. Whoever can read the thread and may post a comment on it may, external members included (ruling record 7063285032, option A: anyone who can see the record, the same gate as posting a comment): the door reads the comment as the caller (a comment they cannot read answers `404 RECORD_NOT_FOUND`, as an unknown id does), refuses a caller who reads comments but may not post one (`403 PERMISSION_DENIED`), refuses a reply (`400 VALIDATION_ERROR`) and then writes `is_resolved` alone, attributed to the caller. `resolved_by` names the caller. It answers `200` with `{ id, is_resolved, resolved_by, resolved_at }`. Any other query parameter or body is `400 VALIDATION_ERROR`; a signed-out request is `401`.
- **Delete your own comment: `DELETE /api/v1/comments/:id`.** It removes a comment the caller wrote (`author_id`), with no delete grant needed, on the platform's shared own-row delete door. Another member's comment is `403 PERMISSION_DENIED` and stays, even for a record editor: moderating someone else's comment stays the data API's, behind the delete grant. A comment that has replies stays too, whoever wrote them, `409 DELETE_RESTRICTED`, because removing it would turn each reply into a thread of its own (ruling record 7069889899: no one deletes or rewrites someone else's comment); its author may still edit its text. It answers `200` with `{ id, deleted: true }`.
- **Unchanged:** comment text stays its author's alone; a member's data-API update of a colleague's comment, `is_resolved` included, and a member's data-API delete are still `403`; administrators keep the data API for resolving, editing and deleting anyone's comment.

The desktop comment panel follows the doors:

- ✓ is on every thread, the viewer's and anyone else's, for a viewer holding the `sys_comment` create grant, and writes through the resolution door. It no longer asks the explain engine; that probe now gates only Edit on someone else's comment.
- A viewer without the `sys_comment` delete grant (an ordinary member) is offered Delete on their own comments that have no reply, and it goes through the own-comment delete door; a viewer with the grant deletes through the data API as before, others' comments included where they may edit the record.

Corrections to two earlier notes of this release, which stay as written:

- A rank-and-file member now resolves and reopens a colleague's thread through the resolution door; this replaces the #36071 note's line "This is the one widening: an update that changes nothing but `is_resolved` is admitted without the author-or-record-editor rule", because the platform's row floor (`owner_only_writes`) runs first, so through the data API such a member still resolves only the threads they started and an administrator resolves any.
- The desktop comment panel offers ✓ on every thread to a viewer who may post a comment and writes it through the resolution door; this replaces the #36075 note's line "✓ is offered under the `sys_comment` update grant: on a thread the viewer started, and on anyone's only where the explain engine admits …", and its "Unchanged" line's "a deployment without the explain service shows ✓ and Edit on the viewer's own rows only" now holds for Edit alone.
28 changes: 19 additions & 9 deletions content/docs/frontend/plugins/plugin-detail.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,19 @@ the record drawer), and no longer inside the 动态 (Activity) tab
first, the comment box at the foot. A thread's replies fold to 「N 条回复」
and open in place. A comment's actions (Reply, an emoji, ✓ on a thread's
first comment, and More with Edit and Delete) appear at its top right while
the pointer is over it. Edit and Delete are on your own comments. On other
people's they appear only where the server would accept them: Delete for a
member who may edit the record, Edit when the server also lets you change a
comment you did not write (today, administrators).
the pointer is over it. Edit and Delete are on your own comments; Delete is
not on a comment of yours that has replies, unless you hold the
`sys_comment` delete grant (administrators do), because the platform keeps
it for the replies under it. On other people's comments they appear only
where the server would accept them: Delete for someone who holds the
`sys_comment` delete grant and may edit the record, Edit when the server
also lets you change a comment you did not write (today, administrators).
- **Resolved threads**: ✓ marks a thread resolved and it leaves the list; the
icon's number drops by one. It is on the threads you started, and on other
people's only where the server would accept it: today administrators, since
the platform lets a member change only the rows they wrote. The
icon's number drops by one. It is on every thread, yours and other people's:
whoever can read the record and may comment on it may resolve and reopen its
threads, through the platform's resolution door
(`PUT /api/v1/comments/:id/resolution`); someone who can read the comments
but not post one sees no ✓. The
filter menu at the panel's top right holds 「显示已解决」, which shows resolved
threads marked with who resolved them (their ✓ reopens them), and
「只看我参与的」, the threads you started, replied in or are mentioned in. A
Expand Down Expand Up @@ -392,8 +397,13 @@ affordances, so the feed offers them wherever its host does: on a `comment`
row (or a reply) whose `authoredByViewer` the host set, with the host's
`onEditComment` / `onDeleteComment`. The console's record page marks each row
with `@object-ui/collaboration`'s `isCommentAuthor`, the rule `CommentThread`
uses, and hands each handler over under the caller's update or delete grant on
`sys_comment`. Edit opens the text in place; Delete asks through the console's
uses, and hands Edit over under the caller's update grant on `sys_comment`.
Delete uses the data API under the caller's delete grant; a caller without it
(an ordinary member) deletes through the platform's own-comment delete door
(`DELETE /api/v1/comments/:id`) under the grant they comment with. That door
removes only the caller's own comment, and not one that has replies, so the
host sets `ownCommentDeletesOnly` and the feed offers Delete on exactly those
rows. Edit opens the text in place; Delete asks through the console's
destructive-action confirm. The server still decides the write, and a refusal
shows as an error with the comment unchanged. An edited comment reads as
edited (`is_edited` on the row).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ a row or a number here — fix the code, or the census, and regenerate.

| Measure | Value |
|---|---:|
| Write call sites | 20 |
| Object name statically decidable | 14 |
| Write call sites | 21 |
| Object name statically decidable | 15 |
| Object name chosen at run time | 6 |
| Object name spelled inline | 11 |
| Object name spelled through a `const` | 3 |
| Object name spelled through a `const` | 4 |
| Object name is an `object: string` parameter | 0 |
| Object name is some other run-time expression | 6 |
| Against a tenancy-enabled object | 14 |
| Against a tenancy-enabled object | 15 |
| Against an object declaring tenancy off | 0 |
| Threading a tenant context | 15 |
| Threading a tenant context | 16 |
| Provably carrying none (options read, no context key) | 0 |
| …and decidably tenancy-enabled | 0 |
| Options argument unreadable | 5 |
| …and decidably tenancy-enabled | 4 |
| Threading a decidably elevated (`isSystem`) context | 15 |
| Threading a decidably elevated (`isSystem`) context | 16 |
| Threading a decidably non-elevated context | 0 |
| Threading a context of undecidable elevation | 5 |
| Receiver carried a readable engine type | 20 |
| Receiver carried a readable engine type | 21 |
| Receiver erased, placed by the object NAME | 0 |
| Receiver erased, placed by an `object: string` PARAMETER | 0 |
| Receiver erased, placed by an `UNTYPED_RECEIVERS` row | 0 |
Expand All @@ -58,6 +58,7 @@ a row or a number here — fix the code, or the census, and regenerate.
| `packages/plugins/plugin-audit/src/auth-event-audit.ts` | `insert` | `sys_audit_log` | enabled | options unreadable | 1 |
| `packages/plugins/plugin-audit/src/comment-parent-pair-backfill.ts` | `update` | `sys_comment` | enabled | elevated | 1 |
| `packages/plugins/plugin-audit/src/comment-resolution.ts` | `update` | `sys_comment` | enabled | elevated | 1 |
| `packages/plugins/plugin-audit/src/comment-routes.ts` | `update` | `sys_comment` | enabled | elevated | 1 |
| `packages/plugins/plugin-audit/src/credential-use-audit.ts` | `insert` | `sys_audit_log` | enabled | elevated | 1 |
| `packages/plugins/plugin-audit/src/data-export-audit.ts` | `insert` | `sys_audit_log` | enabled | elevated | 1 |
| `packages/plugins/plugin-audit/src/dataset-query-audit.ts` | `insert` | `sys_audit_log` | enabled | elevated | 1 |
Expand Down
58 changes: 57 additions & 1 deletion packages/core/src/security/own-row-delete-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { IHttpRequest } from '@objectstack/spec/contracts';

import { hashApiKey } from './api-key.js';
import { currentCredentialUseClient } from './credential-use-client.js';
import { ownRowDeleteCallerResolver } from './own-row-delete-routes.js';
import { ownRowDeleteCallerResolver, removeOwnRow, type OwnRowDeleteDoor } from './own-row-delete-routes.js';

const KEY = 'osk_own_row_delete_31898';
const OWNER = 'u_key_owner';
Expand Down Expand Up @@ -63,3 +63,59 @@ describe('[#31898] the own-row delete doors hand their client facts to the key\'
expect(stamps).toEqual([{ ipAddress: '203.0.113.7', userAgent: 'ci-agent/3.1' }]);
});
});

describe("the own-row delete door's step 7: the object's own refusal", () => {
const OWN = { id: 'row_1', owner_id: 'u_me' };
const makeEngine = () => {
const deleted: unknown[] = [];
return {
deleted,
findOne: async () => ({ ...OWN }),
delete: async (_object: string, options: { where: Record<string, unknown> }) => {
deleted.push(options.where);
return 1;
},
};
};
const door = (refuseRemoval?: OwnRowDeleteDoor['refuseRemoval']): OwnRowDeleteDoor => ({
route: '/api/v1/things/:id',
object: 'thing',
ownerField: 'owner_id',
words: { noun: 'thing', shortNoun: 'thing', owner: 'member' },
...(refuseRemoval ? { refuseRemoval } : {}),
});
const ME = { userId: 'u_me', tenantId: 'org_1' } as never;

it('a refusal it throws reaches the caller and nothing is removed', async () => {
const engine = makeEngine();
const refusal = Object.assign(new Error('kept'), { code: 'DELETE_RESTRICTED', status: 409 });
const seen: unknown[] = [];
await expect(
removeOwnRow(engine, door(async (row) => {
seen.push(row);
throw refusal;
}), 'row_1', ME),
).rejects.toBe(refusal);
expect(seen).toEqual([OWN]);
expect(engine.deleted).toEqual([]);
});

it('is asked only after the owner check: a foreign row is refused 403 before it runs', async () => {
const engine = { ...makeEngine(), findOne: async () => ({ id: 'row_1', owner_id: 'u_other' }) };
let asked = false;
await expect(
removeOwnRow(engine, door(async () => {
asked = true;
}), 'row_1', ME),
).rejects.toMatchObject({ code: 'PERMISSION_DENIED', status: 403 });
expect(asked).toBe(false);
});

it('resolving admits the removal, as a door that declares none does', async () => {
for (const refuse of [async () => undefined, undefined]) {
const engine = makeEngine();
await removeOwnRow(engine, door(refuse), 'row_1', ME);
expect(engine.deleted).toEqual([{ id: 'row_1' }]);
}
});
});
26 changes: 21 additions & 5 deletions packages/core/src/security/own-row-delete-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ import { resolveRequestExecutionContext, type RequestAuthService } from './reque
* gives both, so the door says nothing about a row the caller cannot see.
* 6. **The owner.** A row the caller CAN read whose owner column names anyone
* else is refused loudly, `PERMISSION_DENIED` / `403`, and stays.
* 7. **The object's own refusal**, for a door whose object declares one
* ({@link OwnRowDeleteDoor.refuseRemoval}): a row the caller owns that the
* object still may not lose. The door that declares it says why; its
* refusal is answered with its own `code` and `status`, and the row stays.
*
* ## The removal
*
* Only after all six does the door remove the row, by id, under a system
* Only after all seven does the door remove the row, by id, under a system
* context. That elevation is what lets the door reach a row the caller holds
* no `delete` grant on, and step 6 is the only thing that admits it: the
* elevated write is reached by no other path. The context keeps the caller as
Expand All @@ -89,6 +93,14 @@ export interface OwnRowDeleteDoor {
ownerField: string;
/** The words the door's answers name the row with. */
words: OwnRowDeleteWords;
/**
* Step 7, optional: refuse the removal of a row the caller owns that the
* object still may not lose. Asked after the owner check with the row as the
* caller read it, the engine and the caller's context; it throws the refusal
* (an error carrying the ADR-0112 `code` and `status` the door answers) or
* resolves to admit. Nothing is removed before it resolves.
*/
refuseRemoval?(row: Record<string, unknown>, engine: OwnRowDeleteEngine, context: ExecutionContext): Promise<void>;
}

/** How a door's answers name its row: `reaction` / `reaction` / `member`, `personal view` / `view` / `user`. */
Expand Down Expand Up @@ -197,10 +209,11 @@ function notYourRowError(door: OwnRowDeleteDoor, id: string): Error {
}

/**
* Remove the caller's own row `id` of `door.object` — steps 5 and 6 of the
* Remove the caller's own row `id` of `door.object` — steps 5 to 7 of the
* door, and the removal. Throws `RECORD_NOT_FOUND` / `404` for a row the
* caller cannot read or that does not exist, and `PERMISSION_DENIED` / `403`
* for a row whose owner is someone else. `context` must name a user.
* caller cannot read or that does not exist, `PERMISSION_DENIED` / `403`
* for a row whose owner is someone else, and the door's own refusal (step 7).
* `context` must name a user.
*/
export async function removeOwnRow(
engine: OwnRowDeleteEngine,
Expand All @@ -221,6 +234,9 @@ export async function removeOwnRow(
// Step 6: the structural check, and the only thing that admits the write below.
if (row[door.ownerField] !== callerId) throw notYourRowError(door, id);

// Step 7: the object's own refusal, where it declares one.
if (door.refuseRemoval) await door.refuseRemoval(row, engine, context);

const removed = await engine.delete(door.object, {
where: { id },
context: {
Expand Down Expand Up @@ -292,7 +308,7 @@ async function handleRemoveOwnRow(
return;
}

// ⑤ ⑥ and the removal.
// ⑤ ⑥ ⑦ and the removal.
try {
await removeOwnRow(engine, door, id, context);
} catch (err) {
Expand Down
Loading
Loading