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:
Jakub Zych
2026-11-21 09:17:33 +01:00
parent 19bb3f9485
commit a261a8fdfe
6 changed files with 90 additions and 7 deletions

View File

@@ -851,13 +851,22 @@ 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)
privilegesChanged := !sameRoleID(user.priorRoleID, user.RoleID) || user.priorPermissions != user.Permissions || user.priorSuperuser != user.IsSuperuser
if !user.passwordSetByOther && !privilegesChanged {
return nil
}
// The revocation must commit with the save: fail closed, as the delete
// hook does, when the update carries no transaction.
tx, ok := TxFromContext(ctx)
if !ok {
return errors.New("cabana: administrators: an update needs its transaction")
}
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 privilegesChanged {
if err := fireBackendAccessRevoked(ctx, c.application(), tx, BackendAccessPermissions, user.ID); err != nil {
return err
}