Skip to content

Commit 7fd0264

Browse files
committed
fix(core): Change Owner's queue target asks for the provisioned queue column, as the engine does
The queue-target check looked for a field named queue in the definition. The injection registers queue wherever it registers owner_id, external objects included, where no column backs it, so an external object with an owner got a queue write instead of the 400; and list-shaped fields were refused with an untrue sentence. The check is now the engine's provenance test, resolveInjectedColumnProvenance(def, 'queue') === 'injected-provisioned', which reads both fields shapes. Claude-Session: https://claude.ai/code/session_011y7As8fiqghPfkY5FTbE3X Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a4ed495 commit 7fd0264

3 files changed

Lines changed: 86 additions & 13 deletions

File tree

‎packages/core/src/security/change-record-owner.test.ts‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { describe, it, expect, vi } from 'vitest';
66
import { assertEngineUpdateDispatch } from '@objectstack/metadata-core';
7-
import { OWNER_FIELD_DEF } from '@objectstack/spec/data';
7+
import { OWNER_FIELD_DEF, QUEUE_FIELD_DEF } from '@objectstack/spec/data';
88
import {
99
CHANGE_OWNER_DISABLED,
1010
CHANGE_OWNER_NOTIFICATION_TOPIC,
@@ -19,8 +19,6 @@ import { recordNotFoundError } from '../utils/record-not-found.js';
1919

2020
type Row = Record<string, any>;
2121

22-
const QUEUE_FIELD_DEF = { type: 'text', system: true };
23-
2422
// Registered shapes, as `applySystemFields` leaves them: the owner column and
2523
// the queue column ride together on an owner-bearing object (ADR-0142 D3).
2624
const lead = { name: 'lead', label: 'Lead', sharingModel: 'private', fields: { name: { type: 'text' }, owner_id: OWNER_FIELD_DEF, queue: QUEUE_FIELD_DEF } };
@@ -31,6 +29,19 @@ const orgOwned = { name: 'setting_row', ownership: 'org', fields: { name: { type
3129
const managed = { name: 'auth_session', managedBy: 'better-auth', fields: { user_id: { type: 'lookup', reference: 'sys_user' } } };
3230
const platform = { name: 'sys_team', fields: { name: { type: 'text' } } };
3331
const noQueue = { name: 'memo', fields: { owner_id: OWNER_FIELD_DEF } };
32+
// [#36262] An external (ADR-0015) object with a provable owner: the injection
33+
// still registers `queue` beside `owner_id`, with no column behind it.
34+
const extAccount = {
35+
name: 'ext_account', label: 'Account', sharingModel: 'private', external: { remoteName: 'accounts' },
36+
fields: { name: { type: 'text' }, owner_id: { type: 'lookup', reference: 'sys_user', label: 'Account owner' }, queue: QUEUE_FIELD_DEF },
37+
};
38+
// [#36262] The registered list shape of `fields`, the platform's queue column among them.
39+
const listed = {
40+
name: 'listed_lead', sharingModel: 'private',
41+
fields: [{ name: 'name', type: 'text' }, { name: 'owner_id', ...OWNER_FIELD_DEF }, { name: 'queue', ...QUEUE_FIELD_DEF }],
42+
};
43+
// [#36262] A field the author named `queue`: theirs, not the ownership queue.
44+
const authoredQueue = { name: 'ticket', sharingModel: 'private', fields: { owner_id: OWNER_FIELD_DEF, queue: { type: 'text', label: 'Support queue' } } };
3445
// [#35315 phase 2] The cascade's children of `lead`: one declared cascade, one
3546
// plain lookup, and — registered raw, past the parse that refuses it — a
3647
// cascade on a controlled_by_parent detail, which is never a candidate.
@@ -39,7 +50,7 @@ const leadCall = { name: 'lead_call', sharingModel: 'private', fields: { lead: {
3950
const leadLine = { name: 'lead_line', sharingModel: 'controlled_by_parent', fields: { lead: { type: 'master_detail', reference: 'lead', transferBehavior: 'cascade' }, owner_id: OWNER_FIELD_DEF } };
4051

4152
const SCHEMAS: Record<string, unknown> = Object.fromEntries(
42-
[lead, pinned, line, lineOptedIn, orgOwned, managed, platform, noQueue, leadTask, leadCall, leadLine].map((o) => [o.name, o]),
53+
[lead, pinned, line, lineOptedIn, orgOwned, managed, platform, noQueue, extAccount, listed, authoredQueue, leadTask, leadCall, leadLine].map((o) => [o.name, o]),
4354
);
4455

4556
const CALLER = { userId: 'u_manager', tenantId: 'org_1' };
@@ -66,6 +77,9 @@ function fakeEngine(opts: {
6677
lead: [{ id: 'l1', owner_id: 'u_a' }, { id: 'l2', owner_id: 'u_b' }, { id: 'l3', owner_id: 'u_a' }],
6778
contract_line: [{ id: 'c1', owner_id: 'u_a' }],
6879
memo: [{ id: 'm1', owner_id: 'u_a' }],
80+
ext_account: [{ id: 'x1', owner_id: 'u_a' }],
81+
listed_lead: [{ id: 'd1', owner_id: 'u_a' }],
82+
ticket: [{ id: 'k1', owner_id: 'u_a' }],
6983
lead_task: [
7084
{ id: 't1', lead: 'l1', owner_id: 'u_a' },
7185
{ id: 't2', lead: 'l1', owner_id: 'u_b' },
@@ -199,7 +213,7 @@ describe('[#35402] changeRecordOwner — one record', () => {
199213

200214
it('a queue target on an object with no queue column is refused before any write', async () => {
201215
const engine = fakeEngine();
202-
const err = await refusalOf(changeRecordOwner(engine, 'memo', 'm1', { queue: 'triage' }, CALLER));
216+
const err = await refusalOf(changeRecordOwner(engine, 'ext_account', 'x1', { queue: 'triage' }, CALLER));
203217
expect(err).toBeInstanceOf(UserWorkTransferRefusal);
204218
expect({ code: err.code, status: err.status }).toEqual({ code: 'VALIDATION_ERROR', status: 400 });
205219
expect(engine.calls.some((c) => c.op === 'update')).toBe(false);
@@ -245,6 +259,47 @@ describe('[#35402] changeRecordOwner — one record', () => {
245259
});
246260
});
247261

262+
describe('[#36262] a queue target asks for the queue column with storage behind it, as the engine does', () => {
263+
it('an external object with an owner, whose injected queue has no column, is refused 400 before any write: single and bulk', async () => {
264+
const engine = fakeEngine();
265+
const single = await refusalOf(changeRecordOwner(engine, 'ext_account', 'x1', { queue: 'triage' }, CALLER));
266+
expect({ code: single.code, status: single.status }).toEqual({ code: 'VALIDATION_ERROR', status: 400 });
267+
expect(single.message).toMatch(/^'ext_account' has no ownership queue column/);
268+
const bulk = await refusalOf(changeRecordsOwner(engine, 'ext_account', ['x1'], { queue: 'triage' }, CALLER));
269+
expect({ code: bulk.code, status: bulk.status }).toEqual({ code: 'VALIDATION_ERROR', status: 400 });
270+
expect(engine.calls.some((c) => c.op === 'update')).toBe(false);
271+
expect(engine.tables.ext_account[0]).toEqual({ id: 'x1', owner_id: 'u_a' });
272+
});
273+
274+
it('the external object still takes a person target: only the queue column is missing', async () => {
275+
const result = await changeRecordOwner(fakeEngine(), 'ext_account', 'x1', { ownerId: 'u_next' }, CALLER);
276+
expect(result).toEqual({ object: 'ext_account', id: 'x1', ownerId: 'u_next', notified: false });
277+
});
278+
279+
it('list-shaped fields with the platform\'s queue column are admitted, single and bulk', async () => {
280+
const engine = fakeEngine();
281+
const result = await changeRecordOwner(engine, 'listed_lead', 'd1', { queue: 'triage' }, CALLER);
282+
expect(result).toEqual({ object: 'listed_lead', id: 'd1', ownerId: null, queue: 'triage', notified: false });
283+
const report = await changeRecordsOwner(engine, 'listed_lead', ['d1'], { queue: 'triage' }, CALLER);
284+
expect(report).toMatchObject({ updated: 1, failed: 0 });
285+
});
286+
287+
it('control: a local object with the provisioned queue column is admitted, whether its document lists the column or the injection adds it', async () => {
288+
for (const [object, id] of [['lead', 'l1'], ['memo', 'm1']] as const) {
289+
const engine = fakeEngine();
290+
const result = await changeRecordOwner(engine, object, id, { queue: 'triage' }, CALLER);
291+
expect(result, object).toEqual({ object, id, ownerId: null, queue: 'triage', notified: false });
292+
}
293+
});
294+
295+
it('a field the author named queue is not the ownership queue: refused 400 before any write', async () => {
296+
const engine = fakeEngine();
297+
const err = await refusalOf(changeRecordOwner(engine, 'ticket', 'k1', { queue: 'triage' }, CALLER));
298+
expect({ code: err.code, status: err.status }).toEqual({ code: 'VALIDATION_ERROR', status: 400 });
299+
expect(engine.calls.some((c) => c.op === 'update')).toBe(false);
300+
});
301+
});
302+
248303
describe('[#35402] changeRecordsOwner — the bulk form', () => {
249304
it('a mixed batch: one moves, one refused by the owner guard, one out of reach — every failure listed with its id', async () => {
250305
const engine = fakeEngine({ refuse: (id) => (id === 'l2' ? transferDenied() : undefined) });

‎packages/core/src/security/change-record-owner.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import {
9393
canServeApiOperation,
9494
type EnableLike,
9595
referenceTargetOf,
96+
resolveInjectedColumnProvenance,
9697
resolveUserActionTransfer,
9798
transferCascadeIneligibilityReason,
9899
transferIneligibilityReason,
@@ -356,23 +357,40 @@ export function changeOwnerOffRefusal(object: string, reason: string): Error {
356357
* read and before any write or notification. `getSchema` still resolves such
357358
* an object (disable is reversible and keeps it registered), so the switch
358359
* alone would let it through.
360+
*
361+
* Whether the object can take a queue target is {@link carriesQueueColumn}.
359362
*/
360363
function offeredDefinition(engine: ChangeOwnerEngine, object: string, target: ChangeOwnerTarget): AnyRec {
361364
const def = asRecord(engine.getSchema(object));
362365
if (!def) throw objectNotFoundError(object);
363366
if (engine.registry?.isObjectPackageDisabled?.(object) === true) throw objectPackageDisabledError(object);
364367
const reason = changeOwnerOffReason(object, def);
365368
if (reason) throw changeOwnerOffRefusal(object, reason);
366-
if (target.queue !== undefined) {
367-
const fields = asRecord(def.fields);
368-
if (!fields || !Object.prototype.hasOwnProperty.call(fields, QUEUE_FIELD)) {
369-
throw new UserWorkTransferRefusal(400, 'VALIDATION_ERROR',
370-
`'${object}' has no ownership queue column, so its records cannot be moved to a queue; name a person in ownerId instead`);
371-
}
369+
if (target.queue !== undefined && !carriesQueueColumn(def)) {
370+
throw new UserWorkTransferRefusal(400, 'VALIDATION_ERROR',
371+
`'${object}' has no ownership queue column, so its records cannot be moved to a queue; name a person in ownerId instead`);
372372
}
373373
return def;
374374
}
375375

376+
/**
377+
* [ADR-0142 D3] Does `def` carry the platform's `queue` column with storage
378+
* behind it — the engine's own test (`carriesQueueColumn`, `@objectstack/objectql`'s
379+
* assignment rules), asked of the spec's provenance verdict so this package
380+
* and the engine judge one answer.
381+
*
382+
* Not "does a field named `queue` appear in `fields`". The injection registers
383+
* `queue` wherever it registers `owner_id`, on an external (ADR-0015) object
384+
* too, where no column backs it: a name test admits that object and sends the
385+
* remote system a queue write instead of refusing. A `queue` field the author
386+
* declared is the author's, not the ownership queue, and admits nothing. And
387+
* the verdict reads either registered `fields` shape, a list as well as a
388+
* record keyed by name.
389+
*/
390+
function carriesQueueColumn(def: AnyRec): boolean {
391+
return resolveInjectedColumnProvenance(def, QUEUE_FIELD) === 'injected-provisioned';
392+
}
393+
376394
/** The change-set one record's owner change writes. */
377395
function ownerChangeSet(target: ChangeOwnerTarget): AnyRec {
378396
return target.queue !== undefined

‎packages/rest/src/change-owner-routes.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ObjectQL } from '@objectstack/objectql';
88
import { SqlDriver } from '@objectstack/driver-sql';
99
import { assertEngineUpdateDispatch } from '@objectstack/metadata-core';
1010
import { recordNotFoundError } from '@objectstack/core';
11-
import { OWNER_FIELD_DEF } from '@objectstack/spec/data';
11+
import { OWNER_FIELD_DEF, QUEUE_FIELD_DEF } from '@objectstack/spec/data';
1212
import { RestServer } from './rest-server';
1313
import { DEFAULT_ORGANIZATION_ID, installDefaultOrganization } from './default-organization.testkit.js';
1414

@@ -32,7 +32,7 @@ function mockRes() {
3232
const CALLER = { userId: 'u_manager', tenantId: 'org_1' };
3333

3434
const SCHEMAS: Record<string, unknown> = {
35-
lead: { name: 'lead', label: 'Lead', sharingModel: 'private', fields: { owner_id: OWNER_FIELD_DEF, queue: { type: 'text' } } },
35+
lead: { name: 'lead', label: 'Lead', sharingModel: 'private', fields: { owner_id: OWNER_FIELD_DEF, queue: QUEUE_FIELD_DEF } },
3636
// A `private` object whose author switched the action off (ruling 7000907189, acceptance).
3737
pinned_lead: { name: 'pinned_lead', sharingModel: 'private', userActions: { transfer: false }, fields: { owner_id: OWNER_FIELD_DEF } },
3838
// [#35315 phase 2] A declared cascade to `lead`, and a plain lookup beside it.

0 commit comments

Comments
 (0)