fix(32.2): WR-04 never fire BackendAccessRevokedEvent without the save's transaction
fireBackendAccessRevoked refuses a nil tx before any listener runs, and the Administrators and Roles update hooks fail closed (like the delete hook) when the save carries no transaction, instead of firing with whatever TxFromContext answered.
This commit is contained in:
@@ -2,6 +2,7 @@ package cabana
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"gorm.io/gorm"
|
||||
@@ -20,7 +21,8 @@ import (
|
||||
// commit, with that transaction in Tx. 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 while Tx is set. The after-commit pact.MCPSubjectChanged
|
||||
// transaction. Tx is never nil: a site without its save's transaction
|
||||
// fails instead of firing. The after-commit pact.MCPSubjectChanged
|
||||
// events of the same saves are unchanged and still fire.
|
||||
type BackendAccessRevokedEvent struct {
|
||||
AdminIDs []uint
|
||||
@@ -51,7 +53,11 @@ const (
|
||||
// fireBackendAccessRevoked fires one BackendAccessRevokedEvent for the
|
||||
// de-duplicated non-zero adminIDs with tx and returns the listeners' error,
|
||||
// which the caller returns so the save rolls back. A missing application or
|
||||
// bus, or no id left, is a no-op answering nil.
|
||||
// bus, or no id left, is a no-op answering nil. A nil tx with ids left is
|
||||
// refused with an error before anything fires (fail closed): the event's
|
||||
// contract is that listeners revoke inside the save's transaction, and a
|
||||
// listener handed no transaction would revoke on its own connection while
|
||||
// the save could still roll back.
|
||||
func fireBackendAccessRevoked(ctx context.Context, app *backpack.App, tx *gorm.DB, reason string, adminIDs ...uint) error {
|
||||
if app == nil || app.Events == nil {
|
||||
return nil
|
||||
@@ -71,5 +77,8 @@ func fireBackendAccessRevoked(ctx context.Context, app *backpack.App, tx *gorm.D
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
if tx == nil {
|
||||
return errors.New("cabana: BackendAccessRevokedEvent needs the save's transaction")
|
||||
}
|
||||
return app.Events.Fire(ctx, &BackendAccessRevokedEvent{AdminIDs: ids, Reason: reason, Tx: tx})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user