feat(access): the dashboard operator is a person owning org-sys

This commit is contained in:
sam committed 2026-10-05 20:33:22 +11:00
1 parent c1fd42f575
commit 0920be65e3
12 files changed
+320 -17

No files matched your search

@@ -0,0 +1,15 @@
-- The dashboard acts as a person who owns org-sys. That person is created by
-- a new resumable upgrade phase, so the ledger's phase vocabulary grows by
-- one.
-- +goose Up
ALTER TABLE org_upgrades DROP CONSTRAINT org_upgrades_phase_check;
ALTER TABLE org_upgrades ADD CONSTRAINT org_upgrades_phase_check
CHECK (phase IN ('state_store', 'edastore', 'operator'));
-- +goose Down
ALTER TABLE org_upgrades DROP CONSTRAINT org_upgrades_phase_check;
ALTER TABLE org_upgrades ADD CONSTRAINT org_upgrades_phase_check
CHECK (phase IN ('state_store', 'edastore'));
@@ -11,7 +11,9 @@ import (
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
)
@@ -101,6 +103,63 @@ func TestOrgSysMigrationMovesExistingDefaultRows(t *testing.T) {
assertNoOrg(t, pool, "org-sys")
}
// TestOrgOperatorUpgrade pins the operator phase: a fresh install and an
// existing one each end with exactly one person owner of org-sys, and running
// the upgrade again adds no second.
func TestOrgOperatorUpgrade(t *testing.T) {
ctx := t.Context()
db := pgstore.Open(t)
if err := db.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("fresh upgrade: %v", err)
}
assertOneOperator(t, db.Pool)
if err := db.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("second upgrade: %v", err)
}
assertOneOperator(t, db.Pool)
// An existing install that ran the first two phases but never the operator
// phase: drop the phase's ledger row and what it wrote, then upgrade again.
if _, err := db.Pool.Exec(ctx, `DELETE FROM org_upgrades WHERE phase = 'operator'`); err != nil {
t.Fatalf("reset operator phase: %v", err)
}
for _, sql := range []string{
`DELETE FROM memberships WHERE identity_id = $1`,
`DELETE FROM identities WHERE id = $1`,
} {
if _, err := db.Pool.Exec(ctx, sql, string(identity.OperatorID())); err != nil {
t.Fatalf("reset operator phase: %v", err)
}
}
if err := db.UpgradeDefaultOrg(ctx); err != nil {
t.Fatalf("upgrade on an existing install: %v", err)
}
assertOneOperator(t, db.Pool)
}
func assertOneOperator(t *testing.T, pool *pgxpool.Pool) {
t.Helper()
var owners int
if err := pool.QueryRow(t.Context(), `
SELECT count(*) FROM memberships m
JOIN identities i ON i.id = m.identity_id
WHERE m.org_id = 'org-sys' AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
`).Scan(&owners); err != nil {
t.Fatal(err)
}
if owners != 1 {
t.Fatalf("person owners of org-sys = %d, want 1", owners)
}
var kind string
if err := pool.QueryRow(t.Context(), `SELECT kind FROM identities WHERE id = $1`, string(identity.OperatorID())).Scan(&kind); err != nil {
t.Fatalf("operator identity: %v", err)
}
if kind != string(identity.KindUser) {
t.Fatalf("operator kind = %q, want %q", kind, identity.KindUser)
}
}
func seedDefaultOrg(t *testing.T, pool *pgxpool.Pool) {
t.Helper()
ctx := t.Context()
+20
View File
@@ -221,6 +221,11 @@ func (s *Store) UpgradeDefaultOrg(ctx context.Context) error {
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreEventTypes(ctx) },
func(ctx context.Context, q *postgresdb.Queries) (int64, error) { return q.StampEdastoreToolCalls(ctx) },
}},
// The operator phase runs last: it needs the org the state_store phase
// creates, and gives the dashboard a person to act as where none exists.
{name: "operator", stamps: []func(context.Context, *postgresdb.Queries) (int64, error){
ensureOrgOperator,
}},
}
for _, p := range phases {
if _, err := s.queries().GetOrgUpgradePhase(ctx, p.name); err == nil {
@@ -235,6 +240,21 @@ func (s *Store) UpgradeDefaultOrg(ctx context.Context) error {
return nil
}
// ensureOrgOperator creates the dashboard's operator person as org-sys's owner
// when no person owns it yet. Both writes re-check that condition, so a
// re-run leaves an existing person owner alone and never adds a second.
func ensureOrgOperator(ctx context.Context, q *postgresdb.Queries) (int64, error) {
params := postgresdb.EnsureOrgOperatorIdentityParams{
ID: string(identity.OperatorID()), OrgID: string(org.DefaultOrgID),
}
if _, err := q.EnsureOrgOperatorIdentity(ctx, params); err != nil {
return 0, err
}
return q.EnsureOrgOperatorMembership(ctx, postgresdb.EnsureOrgOperatorMembershipParams{
IdentityID: params.ID, OrgID: params.OrgID,
})
}
// runUpgradePhase stamps one phase's tables and records its ledger row in one
// transaction. An interrupted phase reruns from the start.
func (s *Store) runUpgradePhase(ctx context.Context, name string, stamps []func(context.Context, *postgresdb.Queries) (int64, error)) error {
@@ -42,6 +42,59 @@ func (q *Queries) EnsureDefaultWorkspace(ctx context.Context) error {
return err
}
const ensureOrgOperatorIdentity = `-- name: EnsureOrgOperatorIdentity :execrows
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
SELECT $1, 'user', 'Operator', 'active', 1, now(), now()
WHERE NOT EXISTS (
SELECT 1 FROM memberships m
JOIN identities i ON i.id = m.identity_id
WHERE m.org_id = $2 AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
)
ON CONFLICT (id) DO NOTHING
`
type EnsureOrgOperatorIdentityParams struct {
ID string
OrgID string
}
// The operator phase: if org-sys has no person member with the owner role,
// create the dashboard's operator person and make it that owner. Both writes
// re-check the condition, so a re-run on an install that already has a person
// owner (or already has the operator) changes nothing.
func (q *Queries) EnsureOrgOperatorIdentity(ctx context.Context, arg EnsureOrgOperatorIdentityParams) (int64, error) {
result, err := q.db.Exec(ctx, ensureOrgOperatorIdentity, arg.ID, arg.OrgID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const ensureOrgOperatorMembership = `-- name: EnsureOrgOperatorMembership :execrows
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
SELECT $1, $2, NULL, 'owner'
WHERE NOT EXISTS (
SELECT 1 FROM memberships m
JOIN identities i ON i.id = m.identity_id
WHERE m.org_id = $2 AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
)
ON CONFLICT DO NOTHING
`
type EnsureOrgOperatorMembershipParams struct {
IdentityID string
OrgID string
}
func (q *Queries) EnsureOrgOperatorMembership(ctx context.Context, arg EnsureOrgOperatorMembershipParams) (int64, error) {
result, err := q.db.Exec(ctx, ensureOrgOperatorMembership, arg.IdentityID, arg.OrgID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getOrgUpgradePhase = `-- name: GetOrgUpgradePhase :one
SELECT completed_at FROM org_upgrades WHERE phase = $1
@@ -58,4 +58,29 @@ UPDATE sources SET org_id = 'org-sys', workspace_id = 'default' WHERE org_id = '
UPDATE event_types SET org_id = 'org-sys', workspace_id = 'default' WHERE org_id = '';
-- name: StampEdastoreToolCalls :execrows
UPDATE tool_calls SET org_id = 'org-sys', workspace_id = 'default' WHERE org_id = '';
UPDATE tool_calls SET org_id = 'org-sys', workspace_id = 'default' WHERE org_id = '';
-- The operator phase: if org-sys has no person member with the owner role,
-- create the dashboard's operator person and make it that owner. Both writes
-- re-check the condition, so a re-run on an install that already has a person
-- owner (or already has the operator) changes nothing.
-- name: EnsureOrgOperatorIdentity :execrows
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
SELECT $1, 'user', 'Operator', 'active', 1, now(), now()
WHERE NOT EXISTS (
SELECT 1 FROM memberships m
JOIN identities i ON i.id = m.identity_id
WHERE m.org_id = $2 AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
)
ON CONFLICT (id) DO NOTHING;
-- name: EnsureOrgOperatorMembership :execrows
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
SELECT $1, $2, NULL, 'owner'
WHERE NOT EXISTS (
SELECT 1 FROM memberships m
JOIN identities i ON i.id = m.identity_id
WHERE m.org_id = $2 AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user'
)
ON CONFLICT DO NOTHING;