Repository navigation
Commit babd5d4
fix(plugin-auth): create-user reports the membership the store holds for the new account (#33587)
Fixes #33339
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:管理员创建用户接口(`POST /api/v1/auth/admin/create-user`)的返回值与审计行里的
`membershipCreated`,现在按"新账号在调用结束时是否已有组织成员关系"来回答,不再只看是不是本接口自己插入的那一行。
- 为什么改:单组织部署下,用户创建钩子(默认组织自动加入)先一步写入了 `sys_member`,接口自己的补绑看到已有行就让路,于是回答
`false`;数据库里明明有成员关系,管理员却被告知"创建了一个无组织的账号"。
- 风险与回滚:只改一个布尔字段在两种已有分支下的取值(让路 / 多组织下已存在的行),不改写入、不改权限;回滚即还原
`admin-user-endpoints.ts` 两行。仅邀请制(invite-only)仍回答 `false`、不写成员行,测试钉住未变。
-
先看:`packages/plugins/plugin-auth/src/admin-user-endpoints.ts`(`bindUserToSoleOrganization`
的返回行与 `joinCurrentOrganization`
的已存在分支);`packages/plugins/plugin-auth/src/admin-user-endpoints.test.ts`(两条改写的钉子)。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 src/admin-user-endpoints.test.ts`,预期 100 条全绿,其中 "the
bootstrap join already bound the new user" 一条断言返回与审计行都是 `true`。
- 需要你做的:无,席位落地。
## What changes
`membershipCreated`, in the create-user answer and in the
`user.admin_created` audit row, now reports that the store holds a
membership for the new account after the call, whichever writer made the
row. The account did not exist before this call, so any membership it
holds was created by this creation.
- **Single-org posture** (`bindUserToSoleOrganization`):
`membershipCreated` is `outcome === 'bound' || outcome === 'yielded'`. A
`yielded` outcome is the reconciler reading an existing `sys_member` row
for the user; on create-user that row is the `user.create.after`
bootstrap join's, written moments earlier inside the same `createUser`.
This is the same reading the sign-up hook already makes
(`auth-manager-database-hooks.ts` treats `bound` and `yielded` alike as
"the user holds a membership").
- **Multi-org posture** (`joinCurrentOrganization`): the card asked
whether the same reasoning applies to the "membership already there"
branch. It does: the user is brand-new, so a row in the administrator's
current organization can only have been written during this creation (a
host's `user.create.after` hook). It is still respected (no second
`addMember`) and is now reported as `membershipCreated: true`.
- **Unchanged, pinned as the control**: `invite-only` on the single-org
posture answers `policy-skip`, no `organizationId`, `membershipCreated:
false`, no `sys_member` insert (the existing pin, unedited). `failed`,
`no-target-org` and `agent-account` still answer `false`.
- The jsdoc that stated the old rule (`bindUserToSoleOrganization`,
`AdminAuditDecisions.membershipCreated`, `joinCurrentOrganization`) now
states the new one.
**Why two existing pins were rewritten.** Both pinned
`yielded`/already-there as `false` by seeding a `sys_member` row for
`user-9` before the call. For a brand-new account that seed can only
stand for a row written during the creation, which is exactly the
reported defect, so the old expectation pinned the bug.
- `is idempotent when a membership already exists` becomes `the
bootstrap join already bound the new user: …`: the row is now written by
the `createUser` double itself (the hook's bind, during the creation),
and the pin asserts `true` in the answer AND the audit row, plus no
second `sys_member` insert.
- `a membership already there is respected: … membershipCreated false`
becomes `a membership written during the creation is respected: …
membershipCreated true …`, and gains the audit-row assertion.
**Item 2 of the card (business-unit member rows "not stamped"): premise
does not hold on `main` (4bba8b8), so nothing is changed for it.**
- create-user writes no `sys_business_unit_member` row. `git grep
"'sys_business_unit_member'" -- 'packages/**/src/**/*.ts'
':!**/*.test.ts'` at 4bba8b8 answers four hits: the object
definition, the `sys_user` page's related list, the CLI ownership
inventory and the platform-object name list. No runtime code inserts
that object; plugin-auth only reads it (`external-membership.ts`).
- The warning in the QA record is plugin-sharing's boot-time
`warnOnEmptyUnitExpansion` (`sharing-rule-service.ts`): "active
business-unit rule expands to NO recipients … membership rows written by
seed replay or by an elevated system write are not
organization-stamped". It names two causes worth checking first; it does
not say rows were written. The rule it named,
`showcase_share_new_inquiries_with_field_ops` (organization null),
points at `bu_field_ops`, and the showcase seed writes the unit tree
but, by design, no user-to-unit membership
(`examples/app-showcase/src/data/seed/index.ts`: "Users can't be seeded
… so user↔unit membership … stay runtime admin actions"). An empty
expansion on a fresh showcase boot is the true answer, and the warning
reports it correctly.
## Tests
Run on the branch head before the PR opened (see Verification for the
sha).
- `pnpm --filter @objectstack/plugin-auth test`: Test Files 274 passed
(274), Tests 5303 passed, 10 skipped (5313).
- `pnpm --filter @objectstack/plugin-auth typecheck` (after building the
plugin-auth closure): the first run went red only on
`check:test-typecheck` reporting the debt SHRANK
(`admin-user-endpoints.test.ts`: 6 errors, ledger 7, because the
rewritten pin reads calls through `callsOf`); re-recorded with
`gen:test-typecheck-debt` and committed.
- `eslint --no-inline-config` on both changed source files: exit 0.
Ablation (the fix committed first, each mutation through
`scripts/ablation-replace.mjs`, which verified the anchor 1 to 0 on disk
and restored to the HEAD blob `ee8f0ff9d8c6` with `git diff HEAD` empty;
the test imports `./admin-user-endpoints.js` from `src`, so no build
sits between):
- `membershipCreated: result.outcome === 'bound',` (the old rule): `×
the bootstrap join already bound the new user …`, Tests 1 failed, 99
passed.
- `existing.length > 0) return { organizationId, membershipCreated:
false };` (the old multi-org rule): `× a membership written during the
creation is respected …`, Tests 1 failed, 99 passed.
## Acceptance notes
- Pins per behaviour: bootstrap join then endpoint (answer and audit
`true`), this call bound (`binds the created user to the sole
organization`, unchanged, `true` in both), invite-only control
(unchanged, `false`, no insert, no org lookup), multi-org already-there
(`true` in both).
- A `failed` outcome after a unique-key race (another writer landed the
pair between the reconciler's re-check and its insert) still answers
`false` although the store then holds the row. Reading the store back
after every create would close it at the cost of one more read per
create; not done here, noted only.
## Verification
Local derived gate subset (`node scripts/pm/dispatch-gates.mjs
--commands`, reconciled with `--ran`) and every CI family: see the
report comment on #33339 for the readings and the head they were taken
against.
---
_Generated by [Claude
Code](https://claude.ai/code/session_016WV4d1NJYFGVi2FRztyBNo)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 6446935 commit babd5d4
4 files changed
Lines changed: 63 additions & 21 deletions
File tree
- .changeset
- packages/plugins/plugin-auth
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
458 | 466 | | |
| 467 | + | |
459 | 468 | | |
460 | 469 | | |
461 | 470 | | |
462 | 471 | | |
463 | 472 | | |
464 | 473 | | |
| 474 | + | |
465 | 475 | | |
466 | 476 | | |
467 | | - | |
468 | | - | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
469 | 483 | | |
470 | 484 | | |
471 | 485 | | |
| |||
1521 | 1535 | | |
1522 | 1536 | | |
1523 | 1537 | | |
1524 | | - | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
1525 | 1542 | | |
1526 | 1543 | | |
1527 | 1544 | | |
1528 | 1545 | | |
1529 | 1546 | | |
1530 | 1547 | | |
1531 | 1548 | | |
1532 | | - | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
1533 | 1552 | | |
1534 | 1553 | | |
1535 | 1554 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
476 | 481 | | |
477 | 482 | | |
478 | 483 | | |
| |||
497 | 502 | | |
498 | 503 | | |
499 | 504 | | |
500 | | - | |
| 505 | + | |
501 | 506 | | |
502 | 507 | | |
503 | 508 | | |
| |||
512 | 517 | | |
513 | 518 | | |
514 | 519 | | |
515 | | - | |
| 520 | + | |
| 521 | + | |
516 | 522 | | |
517 | 523 | | |
518 | 524 | | |
| |||
967 | 973 | | |
968 | 974 | | |
969 | 975 | | |
970 | | - | |
971 | | - | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
972 | 981 | | |
973 | 982 | | |
974 | 983 | | |
| |||
983 | 992 | | |
984 | 993 | | |
985 | 994 | | |
986 | | - | |
| 995 | + | |
987 | 996 | | |
988 | 997 | | |
989 | 998 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
0 commit comments