From b1b96d85dad49a2a1b3458fcbba388f7e12828a3 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 21 Nov 2026 02:14:12 +0100 Subject: [PATCH] fix(32.2-05): run admin:reset-password in a lagoon transaction The reset fired BackendAccessRevokedEvent inside a plain GORM transaction, where lagoon.AfterCommit refuses to run, so a listener's after-commit work (sm-oauthserver-plugin's revoked MCPSubjectChanged events that end MCP sessions) never ran for this D-13 site. TestBackendAccessRevokedEvent now asserts the after-commit callback runs once for a committed reset and not for a vetoed one. --- modules/cabana/backend_access_revoked_test.go | 31 ++++++++++++++----- modules/cabana/commands.go | 7 ++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/modules/cabana/backend_access_revoked_test.go b/modules/cabana/backend_access_revoked_test.go index 5003e417..bcc0cbff 100644 --- a/modules/cabana/backend_access_revoked_test.go +++ b/modules/cabana/backend_access_revoked_test.go @@ -13,25 +13,32 @@ import ( "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bonfire" "git.golem15.com/golem15/summercms/modules/cabana" + "git.golem15.com/golem15/summercms/modules/lagoon" "git.golem15.com/golem15/summercms/modules/pact" + "gorm.io/gorm" ) // accessEvents records the in-transaction BackendAccessRevokedEvents and -// the after-commit pact.MCPSubjectChanged events of one app; while fail is -// set the first listener answers an error, so the save rolls back. +// the after-commit pact.MCPSubjectChanged events of one app, and counts the +// after-commit callbacks the revoked listener registers on the event's Tx +// that ran (as a listener that fires session events after the commit +// does); while fail is set the first listener answers an error, so the +// save rolls back. type accessEvents struct { - mu sync.Mutex - revoked []cabana.BackendAccessRevokedEvent - subjects []pact.MCPSubjectChanged - fail atomic.Bool + mu sync.Mutex + revoked []cabana.BackendAccessRevokedEvent + subjects []pact.MCPSubjectChanged + fail atomic.Bool + committed atomic.Int64 } func listenAccessEvents(app *backpack.App) *accessEvents { rec := &accessEvents{} - app.Events.Listen("test-revoked", func(_ context.Context, ev *cabana.BackendAccessRevokedEvent) error { + app.Events.Listen("test-revoked", func(ctx context.Context, ev *cabana.BackendAccessRevokedEvent) error { rec.mu.Lock() rec.revoked = append(rec.revoked, *ev) rec.mu.Unlock() + lagoon.AfterCommit(ctx, ev.Tx, func(context.Context, *gorm.DB) { rec.committed.Add(1) }) if rec.fail.Load() { return errors.New("listener refused") } @@ -136,6 +143,7 @@ func TestBackendAccessRevokedEvent(t *testing.T) { t.Run("admin:reset-password", func(t *testing.T) { target, _ := env.admin(t, "access-reset", false, "") rec.take() + before := rec.committed.Load() reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password") var buf bytes.Buffer if err := reset.Run(context.Background(), flagInput{ @@ -145,12 +153,18 @@ func TestBackendAccessRevokedEvent(t *testing.T) { t.Fatalf("admin:reset-password: %v", err) } expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID) + // The command commits through lagoon, so after-commit work a + // listener registers on the event's Tx runs, once. + if got := rec.committed.Load() - before; got != 1 { + t.Fatalf("after-commit callbacks run by the reset = %d, want 1", got) + } }) t.Run("admin:reset-password rolls back on a listener error", func(t *testing.T) { target, _ := env.admin(t, "access-reset-veto", false, "") before := env.storedUser(t, target.ID).Password rec.take() + committed := rec.committed.Load() rec.fail.Store(true) reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password") var buf bytes.Buffer @@ -165,6 +179,9 @@ func TestBackendAccessRevokedEvent(t *testing.T) { if env.storedUser(t, target.ID).Password != before { t.Fatal("a vetoed reset wrote the new hash") } + if rec.committed.Load() != committed { + t.Fatal("after-commit work ran for a rolled-back reset") + } expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID) }) diff --git a/modules/cabana/commands.go b/modules/cabana/commands.go index b2fd1ec6..4c409d79 100644 --- a/modules/cabana/commands.go +++ b/modules/cabana/commands.go @@ -138,7 +138,12 @@ func adminResetPassword(ctx context.Context, app *backpack.App, in bonfire.Input return err } return withAdminDB(ctx, app, func(gdb *gorm.DB) error { - return gdb.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + // A lagoon transaction, so the after-commit work a + // BackendAccessRevokedEvent listener registers on tx (for example + // the revoked events that end MCP sessions) runs once the reset + // commits; inside a plain GORM transaction lagoon.AfterCommit + // refuses to run it. + return lagoon.Transaction(ctx, gdb, func(ctx context.Context, tx *gorm.DB) error { var users []BackendUser err := tx.Where("login = ? OR lower(email) = ?", identifier, strings.ToLower(identifier)).Find(&users).Error if err != nil {