Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1149 from petracihalova/logs-improv…
Browse files Browse the repository at this point in the history
…ement

improve logs for internal endspoint for roles and permissions removal
  • Loading branch information
petracihalova authored Jul 24, 2024
2 parents a0024f7 + 19e2a9d commit a189487
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rbac/internal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from django.db.migrations.recorder import MigrationRecorder
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.html import escape
from management.cache import TenantCache
from management.models import Group, Permission, Role
from management.principal.proxy import (
Expand Down Expand Up @@ -414,13 +415,14 @@ def role_removal(request):
'Invalid request, must supply the "name" query parameter.',
status=400,
)
role_name = escape(role_name)
# Add tenant public to prevent deletion of custom roles
role_obj = get_object_or_404(Role, name=role_name, tenant=Tenant.objects.get(tenant_name="public"))
with transaction.atomic():
try:
logger.warning(f"Deleting role {role_name}. Requested by {request.user.username}")
logger.warning(f"Deleting role '{role_name}'. Requested by '{request.user.username}'")
role_obj.delete()
return HttpResponse(status=204)
return HttpResponse(f"Role '{role_name}' deleted.", status=204)
except Exception:
return HttpResponse("Role cannot be deleted.", status=400)
return HttpResponse('Invalid method, only "DELETE" is allowed.', status=405)
Expand All @@ -443,12 +445,13 @@ def permission_removal(request):
status=400,
)

permission = escape(permission)
permission_obj = get_object_or_404(Permission, permission=permission)
with transaction.atomic():
try:
logger.warning(f"Deleting permission {permission}. Requested by {request.user.username}")
logger.warning(f"Deleting permission '{permission}'. Requested by '{request.user.username}'")
permission_obj.delete()
return HttpResponse(status=204)
return HttpResponse(f"Permission '{permission}' deleted.", status=204)
except Exception:
return HttpResponse("Permission cannot be deleted.", status=400)
return HttpResponse('Invalid method, only "DELETE" is allowed.', status=405)
Expand Down

0 comments on commit a189487

Please sign in to comment.