package cabana import ( "context" "errors" "git.golem15.com/golem15/summercms/modules/backpack" "gorm.io/gorm" ) // BackendAccessRevokedEvent is fired when administrators lose access that a // credential issued earlier still holds elsewhere: an administrator deleted // or deactivated, a password changed or set, or what an administrator may do // changed (role, own permissions, superuser flag, or the permissions of the // role they hold). Reason is one of the BackendAccess* constants. Listeners // revoke what is keyed on these administrators, for example OAuth grants an // administrator consented to. // // 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 // 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 // fails instead of firing. The after-commit pact.MCPSubjectChanged // events of the same saves are unchanged and still fire. type BackendAccessRevokedEvent struct { AdminIDs []uint Reason string Tx *gorm.DB } // Reasons of a BackendAccessRevokedEvent. const ( // BackendAccessDeleted: the administrator was deleted. BackendAccessDeleted = "deleted" // BackendAccessDeactivated: the administrator was deactivated. BackendAccessDeactivated = "deactivated" // BackendAccessPasswordChanged: the administrator changed their own // password on My account. BackendAccessPasswordChanged = "password_changed" // BackendAccessPasswordSet: another administrator set the password on // the Administrators screen. BackendAccessPasswordSet = "password_set" // BackendAccessPasswordReset: the password was reset with the // admin:reset-password command. BackendAccessPasswordReset = "password_reset" // BackendAccessPermissions: the administrator's role, own permissions or // superuser flag changed, or the permissions of the role they hold. BackendAccessPermissions = "permissions" ) // 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. 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 } if ctx == nil { ctx = context.Background() } seen := make(map[uint]bool, len(adminIDs)) ids := make([]uint, 0, len(adminIDs)) for _, id := range adminIDs { if id == 0 || seen[id] { continue } seen[id] = true ids = append(ids, id) } 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}) }