From 60bc6ee68f51afc08863b3d9c0140c3ce1ed17dc Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 21 Nov 2026 09:21:59 +0100 Subject: [PATCH] fix(32.2): WR-05 hand the deactivate revocation the plain transaction The deactivate lifecycle fired BackendAccessRevokedEvent with actionDB's Unscoped session, so a listener's Delete through ev.Tx was a hard delete only for that reason. It now passes the save's transaction like every other site. --- modules/cabana/admin_events.go | 3 ++- modules/cabana/backend_access_revoked_smoke_test.go | 5 +++++ modules/cabana/backend_users_admin.go | 9 ++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/cabana/admin_events.go b/modules/cabana/admin_events.go index db7ee762..e8ba19a1 100644 --- a/modules/cabana/admin_events.go +++ b/modules/cabana/admin_events.go @@ -18,7 +18,8 @@ import ( // // It is fired synchronously through app.Events.Fire as a // *BackendAccessRevokedEvent inside the transaction of the save, before the -// commit, with that transaction in Tx. A listener's error is returned to the +// commit, with that transaction in Tx (the transaction itself at every +// site, never an Unscoped session). A listener's error is returned to the // save and rolls it back, so the change and the revocation commit together // or not at all. A listener must write through Tx and must not open its own // transaction. Tx is never nil: a site without its save's transaction diff --git a/modules/cabana/backend_access_revoked_smoke_test.go b/modules/cabana/backend_access_revoked_smoke_test.go index 8dd1e606..72f47c61 100644 --- a/modules/cabana/backend_access_revoked_smoke_test.go +++ b/modules/cabana/backend_access_revoked_smoke_test.go @@ -57,6 +57,11 @@ func TestBackendAccessRevokedSmoke(t *testing.T) { if got[0].Reason != reason || !slices.Equal(got[0].AdminIDs, ids) || got[0].Tx == nil { t.Fatalf("%s: event = %+v, want ids %v with a transaction", reason, got[0], ids) } + // Every site hands listeners the plain transaction: never an + // Unscoped session, on which Delete would be a hard delete. + if got[0].Tx.Statement != nil && got[0].Tx.Statement.Unscoped { + t.Fatalf("%s: event Tx is Unscoped", reason) + } } t.Run("delete", func(t *testing.T) { diff --git a/modules/cabana/backend_users_admin.go b/modules/cabana/backend_users_admin.go index 74e3b55c..df455d65 100644 --- a/modules/cabana/backend_users_admin.go +++ b/modules/cabana/backend_users_admin.go @@ -288,7 +288,14 @@ func (c usersAdmin) lifecycles() []usersLifecycle { Updates(map[string]any{"is_activated": false, "tokens_valid_after": revocationCutoff()}).Error; err != nil { return err } - if err := fireBackendAccessRevoked(ctx, c.application(), db, BackendAccessDeactivated, user.ID); err != nil { + // Listeners get the save's transaction itself, as at every + // other site, not db: that is an Unscoped session, on which + // a listener's Delete would be a hard delete. + tx, ok := TxFromContext(ctx) + if !ok { + return errors.New("cabana: administrators: an action needs its transaction") + } + if err := fireBackendAccessRevoked(ctx, c.application(), tx, BackendAccessDeactivated, user.ID); err != nil { return err } // A deactivated administrator can no longer act anywhere,