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:
Jakub Zych
2026-11-21 00:32:26 +01:00
parent 3f7ce6020d
commit fcc7bbde25
10 changed files with 270 additions and 8 deletions

View File

@@ -211,8 +211,9 @@ func (c usersAdmin) FormBeforeDelete(ctx context.Context, model any) error {
return guardLastSuperuser(ctx, user)
}
// FormAfterDelete ends the deleted administrator's sessions (D-12). The
// delete itself stays the framework's soft delete through deleted_at: a
// FormAfterDelete ends the deleted administrator's sessions (D-12) and
// fires BackendAccessRevokedEvent (deleted) inside the delete's transaction.
// The delete itself stays the framework's soft delete through deleted_at: a
// backend user is never hard-deleted, so tokens, linked_by references and
// attachments keep valid foreign keys (D-10). Personal API tokens stop
// because the guard refuses a deleted administrator.
@@ -231,6 +232,9 @@ func (c usersAdmin) FormAfterDelete(ctx context.Context, model any) error {
Update("tokens_valid_after", cutoff).Error; err != nil {
return err
}
if err := fireBackendAccessRevoked(ctx, c.application(), tx, BackendAccessDeleted, user.ID); err != nil {
return err
}
fireSubjectChanged(ctx, c.application(), pact.MCPSubjectDeleted, user.ID)
return nil
}
@@ -284,6 +288,9 @@ 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 {
return err
}
// A deactivated administrator can no longer act anywhere,
// an MCP session included.
fireSubjectChanged(ctx, c.application(), pact.MCPSubjectDeleted, user.ID)
@@ -818,15 +825,20 @@ func (c usersAdmin) FormBeforeUpdate(ctx context.Context, model any) error {
if changed {
cutoff := revocationCutoff()
user.TokensValidAfter = &cutoff
// Your own password is refused above, so another administrator
// set this one: FormAfterUpdate fires password_set.
user.passwordSetByOther = true
}
return c.checkIdentity(ctx, user)
}
// FormAfterUpdate refuses a change of the editor's own role (D-08). The
// submitted role is assigned after FormBeforeUpdate, so it is compared here,
// with the row written: the refusal rolls the save back. A save that changed
// the administrator's role, own permissions or superuser flag then fires
// pact.MCPSubjectChanged (permissions) for them.
// with the row written: the refusal rolls the save back. A password another
// administrator set fires BackendAccessRevokedEvent (password_set), and a
// save that changed the administrator's role, own permissions or superuser
// flag fires it (permissions) and pact.MCPSubjectChanged (permissions) for
// them; the events' listener errors roll the save back.
func (c usersAdmin) FormAfterUpdate(ctx context.Context, model any) error {
user, err := asBackendUser(model)
if err != nil {
@@ -839,7 +851,16 @@ func (c usersAdmin) FormAfterUpdate(ctx context.Context, model any) error {
if principal != nil && principal.ID == user.ID && !sameRoleID(user.priorRoleID, user.RoleID) {
return &ForbiddenError{Message: usersLang + "self_privileged", Details: map[string]any{usersRoleField: []string{usersLang + "self_privileged"}}}
}
tx, _ := TxFromContext(ctx)
if user.passwordSetByOther {
if err := fireBackendAccessRevoked(ctx, c.application(), tx, BackendAccessPasswordSet, user.ID); err != nil {
return err
}
}
if !sameRoleID(user.priorRoleID, user.RoleID) || user.priorPermissions != user.Permissions || user.priorSuperuser != user.IsSuperuser {
if err := fireBackendAccessRevoked(ctx, c.application(), tx, BackendAccessPermissions, user.ID); err != nil {
return err
}
fireSubjectChanged(ctx, c.application(), pact.MCPSubjectPermissions, user.ID)
}
return nil