Fix backup cancellation user reference issues

🐛 Enhanced Error Handling:
- Fixed 'User matching query does not exist' error in backup_cancel
- Handle case where backup_job.created_by is None (deleted user)
- Added error handling for audit logging to prevent cancellation failure
- Improved permission check to handle null created_by references

 Backup Cancellation Robustness:
- Now works even if the user who created the backup was deleted
- Audit logging failure won't prevent successful cancellation
- Better error isolation and reporting

The error occurred because the system tried to compare a null user reference
or failed during audit logging when user records were inconsistent.
This commit is contained in:
2025-09-29 20:40:51 +02:00
parent bdcede9a74
commit 33888a3b18

View File

@@ -6023,7 +6023,8 @@ def backup_cancel(request, backup_id):
return redirect("stiftung:backup_management")
# Check if user has permission to cancel (either own job or admin)
if backup_job.created_by != request.user and not request.user.is_staff:
# Handle case where created_by might be None (user deleted)
if backup_job.created_by is not None and backup_job.created_by != request.user and not request.user.is_staff:
messages.error(request, "Sie können nur Ihre eigenen Backup-Jobs abbrechen.")
return redirect("stiftung:backup_management")
@@ -6034,14 +6035,18 @@ def backup_cancel(request, backup_id):
backup_job.error_message = f"Abgebrochen von {request.user.username}"
backup_job.save()
# Log the cancellation
from stiftung.audit import log_system_action
log_system_action(
request=request,
action="backup_cancel",
description=f"Backup-Job abgebrochen: {backup_job.get_backup_type_display()}",
details={"backup_job_id": str(backup_job.id)},
)
# Log the cancellation (with error handling)
try:
from stiftung.audit import log_system_action
log_system_action(
request=request,
action="backup_cancel",
description=f"Backup-Job abgebrochen: {backup_job.get_backup_type_display()}",
details={"backup_job_id": str(backup_job.id)},
)
except Exception as audit_error:
# Don't fail the cancellation if logging fails
print(f"Warning: Could not log backup cancellation: {audit_error}")
messages.success(request, f"Backup-Job wurde abgebrochen.")