feat(32.2-01): in-transaction BackendAccessRevokedEvent at every admin access change
- cabana: BackendAccessRevokedEvent with reasons deleted, deactivated, password_changed, password_set, password_reset and permissions, fired synchronously inside the save's transaction; a listener error rolls back - fired on admin delete, deactivate, a password set by another admin, an own password change, admin:reset-password, a role/permissions/superuser change and a role's permission change (every holder); MCPSubjectChanged unchanged - role delete fires nothing (refused while the role has holders); no sign out everywhere action exists - smoke test TestBackendAccessRevokedSmoke (Postgres)
This commit is contained in:
75
modules/cabana/admin_events.go
Normal file
75
modules/cabana/admin_events.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package cabana
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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 while Tx is set. 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.
|
||||
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
|
||||
}
|
||||
return app.Events.Fire(ctx, &BackendAccessRevokedEvent{AdminIDs: ids, Reason: reason, Tx: tx})
|
||||
}
|
||||
Reference in New Issue
Block a user