-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import re | ||
|
||
from django.http import JsonResponse | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
|
||
from .models import Case | ||
|
||
|
||
class SensitiveCaseMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.get_response(request) | ||
if request.path.startswith("/api/v1/cases/"): | ||
match = re.match(r"^/api/v1/cases/(\d+)/", request.path) | ||
if match: | ||
case_id = match.group(1) | ||
case = get_object_or_404(Case, pk=case_id) | ||
if ( | ||
case.sensitive | ||
and request.user.is_authenticated | ||
and not request.user.has_perm("users.access_sensitive_dossiers") | ||
): | ||
return JsonResponse( | ||
{"detail": "Forbidden"}, status=status.HTTP_403_FORBIDDEN | ||
) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters