-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpermissions.py
More file actions
62 lines (43 loc) · 1.74 KB
/
permissions.py
File metadata and controls
62 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from django.contrib.auth import get_user_model
from rest_framework.permissions import BasePermission
from projects.models import Project
User = get_user_model()
def get_lookup_kwarg(request, view, *names):
view_kwargs = getattr(view, "kwargs", {}) or {}
parser_kwargs = (getattr(request, "parser_context", None) or {}).get("kwargs", {})
for name in names:
if name in view_kwargs:
return view_kwargs[name]
if name in parser_kwargs:
return parser_kwargs[name]
return None
def is_project_member(user, project) -> bool:
if not getattr(user, "is_authenticated", False):
return False
if project.leader_id == user.id:
return True
return project.collaborator_set.filter(user_id=user.id).exists()
class IsProjectChatMember(BasePermission):
def has_permission(self, request, view) -> bool:
project_id = get_lookup_kwarg(request, view, "id", "pk")
if project_id is None:
return True
try:
project = Project.objects.only("id", "leader_id").get(pk=project_id)
except (Project.DoesNotExist, TypeError, ValueError):
return False
return is_project_member(request.user, project)
def has_object_permission(self, request, view, obj):
return is_project_member(request.user, obj.project)
class IsChatMember(BasePermission):
"""
Allows access only to authenticated users.
"""
def has_permission(self, request, view) -> bool:
chat_id = get_lookup_kwarg(request, view, "id")
if chat_id is None:
return True
user_id = getattr(request.user, "id", None)
if user_id is None:
return False
return str(user_id) in str(chat_id).split("_")