Skip to content

Commit badf24d

Browse files
fix(core): count and hand over the injected owner_id in a banned user's assigned work (#30677)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DKzfSXJ79oXBZ9JNgjMFa6R)_ Fixes #30646 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:封禁用户时「他手上有多少记录」的计数和「批量转交」现在会算上平台给每个业务对象注入的负责人字段 `owner_id`;`created_by` / `updated_by` 仍不计、不转。 - 为什么改:原逻辑跳过所有 `system: true` 的字段,而注入的 `owner_id` 正是 `system: true`(但不是只读),导致绝大多数对象上「拥有」恒为 0、转交也不移交负责人。 - 风险与回滚:只改字段筛选一行,排除依据从「system 或 readonly」改为「readonly」;访问控制不变(计数和转交仍以调用者身份读写)。回滚即 revert 本 PR。 - 先看:`packages/core/src/security/user-assigned-work.ts` 第 102 行附近的筛选条件;其次 `user-assigned-work.test.ts` 的 fixture(改用真实的 `OWNER_FIELD_DEF` / `AUDIT_FIELD_DEFS`)。 - 两分钟验证:`pnpm --filter @objectstack/core exec vitest run --project local src/security/user-assigned-work.test.ts`,预期 12 个测试全绿。 - 需要你做的:无,席位落地。 ## What changes `userAssignedWorkFields` (the field picker both the count and the hand-over read) skipped any user reference with `readonly: true` OR `system: true`. The platform's injected `owner_id` (`OWNER_FIELD_DEF` in `packages/spec/src/data/injected-system-column-provenance.ts`) is `system: true`, `readonly: false`, so on every object that did not declare `owner_id` itself, owned records were neither counted nor handed over. The picker now excludes by `readonly` only. The audit lookups `created_by` / `updated_by` are `readonly: true` both as injected (`AUDIT_FIELD_DEFS`) and when an author declares them (`applyAuditFieldGovernance` forces `readonly` / `system` over a declared audit field), so they stay out. `sys_*` objects, `managedBy` objects and unprovisioned injected columns are excluded exactly as before. The module header now says why `system` alone does not exclude a field. A repo grep for user references (`reference: 'sys_user'`) carrying `system: true` finds only the injected owner and the two audit lookups, so no other field changes classification. Access control is untouched: every count, read and write still runs under the caller's own context. ## Tests - The test fixture now builds `owner_id`, `created_by` and `updated_by` from the real `OWNER_FIELD_DEF` / `AUDIT_FIELD_DEFS`, so the existing count and hand-over cases pin the injected owner; the seed row also holds the banned user in `created_by` / `updated_by`, and the hand-over case asserts those stay unchanged. - New case: an object carrying only `OWNER_FIELD_DEF` plus the four audit columns yields exactly `owner_id`, and the case asserts the premise (`system: true`, `readonly: false`). Ablation (head `90919afc`, fix committed first, mutation via `scripts/ablation-replace.mjs`, restore proven by blob == HEAD and empty `git diff HEAD`): - Put the `system === true` skip back: `Tests 9 failed | 3 passed (12)`; the new pin fails with `expected [] to deeply equal [ { object: 'deal', … } ]`, and the count and hand-over cases fail. - Control leg, delete the `readonly` skip instead (nothing excluded by field flags): `Tests 5 failed | 7 passed (12)`; the new pin and the count and hand-over cases fail because `created_by` / `updated_by` get picked. ## Acceptance notes - None beyond the card's scope. ## Verification All on head `90919afc`. - `pnpm --filter @objectstack/core test`: Test Files 136 passed, Tests 3087 passed (exit 0). - `pnpm --filter @objectstack/core typecheck`: exit 0. - `pnpm lint`: exit 0. - `node scripts/pm/dispatch-gates.mjs --commands`: 60 families derived; `--ran` reconciliation: 59 run with exit 0, 1 NOT MEASURED (`pnpm check:dual-build-cjs-loads`, PREREQUISITE NOT MET: needs a full `pnpm build`; CI's `Lint & Repo Gates` runs it). `check-plugin-teardown-shape.mjs --self-test` and `check:lean-entry-closure` first exited 3 on prerequisites, then exited 0 after fetching the pinned fixture commit and building `@objectstack/objectql`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016WV4cytLmJxfAmgPVjUbbW Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9d5ec25 commit badf24d

3 files changed

Lines changed: 36 additions & 9 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/core': patch
3+
---
4+
5+
fix(core): a banned user's assigned-work count and hand-over now include the platform's injected `owner_id`
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The field picker behind the assigned-work count and the bulk hand-over skipped every `system: true` user reference. The `owner_id` the platform injects on every business object is `system: true` (it is a platform column) but not `readonly` (ownership is transferable), so it was skipped: on any object whose author had not declared `owner_id` themselves, the records a banned user owned were counted as zero and never handed over. The picker now leaves out `readonly` user references only, so `created_by` / `updated_by` stay out and the injected `owner_id` is counted and handed over. Platform tables (`sys_*`), `managedBy` objects and injected columns with no storage stay excluded as before.

‎packages/core/src/security/user-assigned-work.test.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { describe, it, expect } from 'vitest';
44
import { assertEngineUpdateDispatch } from '@objectstack/metadata-core';
5+
import { AUDIT_FIELD_DEFS, OWNER_FIELD_DEF } from '@objectstack/spec/data';
56
import {
67
countUserAssignedWork,
78
transferUserAssignedWork,
@@ -10,18 +11,22 @@ import {
1011
type UserWorkEngine,
1112
} from './user-assigned-work.js';
1213

14+
// [#30646] `owner_id`, `created_by` and `updated_by` are the platform's own
15+
// injected definitions, as `applySystemFields` spreads them: the owner column is
16+
// `system: true` but not `readonly`, the audit lookups are both.
1317
const ticket = {
1418
name: 'ticket',
1519
fields: {
1620
title: { type: 'text' },
17-
owner_id: { type: 'lookup', reference: 'sys_user' },
21+
owner_id: OWNER_FIELD_DEF,
1822
assignee: { type: 'user' },
1923
watchers: { type: 'lookup', reference: 'sys_user', multiple: true },
20-
created_by: { type: 'lookup', reference: 'sys_user', readonly: true, system: true },
24+
created_by: AUDIT_FIELD_DEFS.created_by,
25+
updated_by: AUDIT_FIELD_DEFS.updated_by,
2126
account: { type: 'lookup', reference: 'account' },
2227
},
2328
};
24-
const account = { name: 'account', fields: { owner_id: { type: 'lookup', reference: 'sys_user' } } };
29+
const account = { name: 'account', fields: { owner_id: OWNER_FIELD_DEF } };
2530
const grant = { name: 'sys_user_permission_set', fields: { user_id: { type: 'lookup', reference: 'sys_user' } } };
2631
const managed = { name: 'auth_session', managedBy: 'better-auth', fields: { user_id: { type: 'lookup', reference: 'sys_user' } } };
2732

@@ -37,6 +42,14 @@ describe('[#30205] userAssignedWorkFields', () => {
3742
]);
3843
});
3944

45+
it('[#30646] takes the injected owner_id as the platform defines it, and leaves the audit lookups out', () => {
46+
// The premise this pins: the injected owner column is a platform column
47+
// (`system`) that stays assignable, so `system` alone cannot exclude it.
48+
expect({ system: OWNER_FIELD_DEF.system, readonly: OWNER_FIELD_DEF.readonly }).toEqual({ system: true, readonly: false });
49+
const { fields } = userAssignedWorkFields([{ name: 'deal', fields: { owner_id: OWNER_FIELD_DEF, ...AUDIT_FIELD_DEFS } }]);
50+
expect(fields).toEqual([{ object: 'deal', field: 'owner_id', multiple: false }]);
51+
});
52+
4053
it('stops at the cap and says so', () => {
4154
const { fields, truncated } = userAssignedWorkFields([ticket, account], 2);
4255
expect(fields).toHaveLength(2);
@@ -98,7 +111,7 @@ function seed(): Record<string, Row[]> {
98111
{ id: 'u_also_banned', banned: true, ban_expires: null },
99112
],
100113
ticket: [
101-
{ id: 't1', owner_id: 'u_gone', assignee: 'u_gone', watchers: ['u_gone'] },
114+
{ id: 't1', owner_id: 'u_gone', assignee: 'u_gone', watchers: ['u_gone'], created_by: 'u_gone', updated_by: 'u_gone' },
102115
{ id: 't2', owner_id: 'u_gone', assignee: 'u_x', watchers: [] },
103116
{ id: 't3', owner_id: 'u_x', assignee: 'u_gone', watchers: ['u_x', 'u_gone'] },
104117
],
@@ -157,8 +170,9 @@ describe('[#30205] transferUserAssignedWork', () => {
157170
});
158171
const t = engine.tables.get('ticket')!;
159172
expect(t.map((r) => [r.owner_id, r.assignee])).toEqual([['u_next', 'u_next'], ['u_next', 'u_x'], ['u_x', 'u_next']]);
160-
// A multi-valued field is never rewritten.
173+
// A multi-valued field is never rewritten, and neither is who did what.
161174
expect(t[0].watchers).toEqual(['u_gone']);
175+
expect([t[0].created_by, t[0].updated_by]).toEqual(['u_gone', 'u_gone']);
162176
// Every write went through `update` under the caller's own context.
163177
const writes = engine.calls.filter((c) => c.op === 'update');
164178
expect(writes).toHaveLength(5);

‎packages/core/src/security/user-assigned-work.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
* platform's `owner_id`, which is itself a lookup to the user object, and the
1818
* app's own fields such as an `assignee`. Left out, on purpose:
1919
*
20-
* - `readonly` and `system` fields: `created_by` / `updated_by` record who did
21-
* something, not who holds the work, and moving them would rewrite history.
20+
* - `readonly` fields: `created_by` / `updated_by` record who did something,
21+
* not who holds the work, and moving them would rewrite history. `system`
22+
* alone does not exclude a field: it marks a column the platform provides,
23+
* and the injected `owner_id` is one (`OWNER_FIELD_DEF` is `system: true`,
24+
* `readonly: false`, because ownership is transferable).
2225
* - platform tables (`sys_*`) and `managedBy` objects: `sys_user_permission_set.user_id`
2326
* is a grant, not work, and moving it would move the person's permissions.
2427
* - a column the platform registers but provisions no storage for (an injected
@@ -98,8 +101,8 @@ export function userAssignedWorkFields(
98101
for (const [field, def] of Object.entries(obj.fields as Record<string, unknown>)) {
99102
if (phantom.has(field)) continue;
100103
if (referenceTargetOf(def) !== USER_OBJECT) continue;
101-
const d = def as { readonly?: unknown; system?: unknown; multiple?: unknown };
102-
if (d.readonly === true || d.system === true) continue;
104+
const d = def as { readonly?: unknown; multiple?: unknown };
105+
if (d.readonly === true) continue;
103106
if (fields.length >= cap) return { fields, truncated: true };
104107
fields.push({ object: name, field, multiple: d.multiple === true });
105108
}

0 commit comments

Comments
 (0)