Files
CatoandClaude Fable 5.2 7ef4611d44 feat(workspace): one sign-in, many organisations; private unless published (#217)
* feat(access): tools and data routes are for members of the organisation

Until now /fundraising and every data route under /api sat behind one shared
HTTP Basic password in the middleware, or behind nothing with
INTERNAL_AUTH=off. No handler asked who was calling, and the organisation
came from the Host header, so the password opened every organisation's data.

- requireOrgPage in one layout covers every tool page; requireOrgApi is the
  first statement of every data handler (28 handlers, and the PDF factory).
  The organisation is the one whose site the request is on, and the caller
  must be a signed-in member of it: 401 signed out, 403 for a member of
  another organisation.
- The shared register of funders is changed only by the platform operator
  (users.role, scripts/set-operator.ts). A member writes its own
  organisation's assessment; a register fact in the same request is refused
  whole, so no edit is half saved.
- Writes and the model route are rate-limited per person with
  @bitbaum/limitkit.
- The activity log records who did it, not 'api'.
- The password gate, the public mode and their helpers are removed; the
  middleware only routes hosts.
- A test reads every route file and refuses a data route without the gate.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* wip(access): the workspace URL names the organisation; the gate reads it from there

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* wip(workspace): tools and data routes move under /o/<slug>; organisations are private unless published

Work in progress on a draft branch: the route and link move is not finished
and the full verify has not been run on this commit.

- docs/PRODUCT.md: the product decision (person, organisations, funders)
- org_profiles.visibility (migration 0021), private by default; revamp-it
  private, its data kept; share links and the workspace still resolve
- scripts/set-visibility.ts

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* feat(workspace): one sign-in, many organisations; private unless published; the owner is never locked out

The tools and every data route now live in the workspace, /o/<slug>/…, on
the platform's host. The organisation is the one named in the URL and the
caller must be a signed-in member of it. An organisation's own host serves
its public pages only; old links to /fundraising/… there are sent to the
workspace.

Organisations are private unless published (org_profiles.visibility,
migration 0021). revamp-it becomes private: every row is kept, its public
pages stop being served, its share links and workspace still resolve.

The server names its owner in HIRNLI_OWNER_EMAIL. Signed in with that
address verified, they become the operator and the owner of every
organisation that has no member; never a member of one that has members.

Verified: pnpm run verify (1183 tests, build), fleet docs-truth,
composition and experience audits, and a local walk signed out, as an
unverified and as a verified owner address.

Known gap: links from tool pages to an organisation's public pages
(/wirkung, /finanzen, …) do not exist on the platform host yet.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* feat(access): the first admin by the fleet's rule: a role from accountkit and a mailed one-hour link

The owner bootstrap matched the signed-in account's address against a
server setting. The fleet's back-office rule (written the same day) says
access is a role on the account, never an address, and the fallback for the
first admin is one button that mails a signed one-hour link to the owner
mailbox. A sign-in through OrangeCat also carries an address OrangeCat made
up, so the match could never have worked for it.

- The operator is now accountkit's admin role.
- The account page offers the button while the account is not an admin and
  the server names a mailbox (OWNER_EMAILS, as in Heidi) and can send mail.
- The link works only for the signed-in account it names, for an hour. It
  makes that account admin and owner of every organisation without a
  member; never a member of one that has members.

Walked locally: signed out, expired, another account's link, the real one.
Not walked: the mail itself (no mail key here).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-30 21:06:57 +01:00

53 lines
1.9 KiB
TypeScript

/**
* Make an existing account the platform's operator, or take that away.
*
* npx tsx scripts/set-operator.ts <email> # shows what would change
* npx tsx scripts/set-operator.ts <email> --write
* npx tsx scripts/set-operator.ts <email> --remove --write
*
* The operator is the only account that may change the shared register of
* funders (src/lib/auth/require.ts, `requireOperator`). Being an owner of an
* organisation does not make someone the operator: the register is read by
* every organisation and owned by none.
*
* The account must already exist; this never creates one. A dry run unless
* `--write` is given.
*/
import { eq } from 'drizzle-orm';
import { db } from '../src/lib/db/client';
import { user } from '../src/lib/db/auth-schema';
import { PLATFORM_ADMIN_ROLE as OPERATOR_ROLE } from '../src/lib/auth/platform-owner';
async function main() {
const args = process.argv.slice(2);
const write = args.includes('--write');
const remove = args.includes('--remove');
const email = args.find((a) => !a.startsWith('--'));
if (!email) throw new Error('usage: set-operator.ts <email> [--remove] [--write]');
const rows = await db
.select({ id: user.id, role: user.role })
.from(user)
.where(eq(user.email, email));
if (rows.length === 0)
throw new Error(`No account with the address ${email}. It must register first.`);
const next = remove ? 'customer' : OPERATOR_ROLE;
console.log(`${email}: role '${rows[0].role}' -> '${next}'`);
if (rows[0].role === next) {
console.log('Nothing to change.');
} else if (!write) {
console.log('DRY RUN. Pass --write to change it.');
} else {
await db.update(user).set({ role: next }).where(eq(user.id, rows[0].id));
console.log('Changed.');
}
process.exit(0);
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : err);
process.exit(1);
});