|
|
@ -1,8 +1,7 @@ |
|
|
|
from django.conf import settings |
|
|
|
from django.db.models import Subquery |
|
|
|
from rest_framework.permissions import BasePermission, SAFE_METHODS |
|
|
|
|
|
|
|
from .models import Member, Role |
|
|
|
from .models import Member |
|
|
|
|
|
|
|
|
|
|
|
class RolePermission(BasePermission): |
|
|
@ -25,7 +24,7 @@ class RolePermission(BasePermission): |
|
|
|
if not project_id and request.method in SAFE_METHODS: |
|
|
|
return True |
|
|
|
|
|
|
|
return is_in_role(self.role_name, request.user.id, project_id) |
|
|
|
return Member.objects.has_role(project_id, request.user, self.role_name) |
|
|
|
|
|
|
|
|
|
|
|
class IsProjectAdmin(RolePermission): |
|
|
@ -51,13 +50,5 @@ class IsAnnotationApprover(RolePermission): |
|
|
|
role_name = settings.ROLE_ANNOTATION_APPROVER |
|
|
|
|
|
|
|
|
|
|
|
def is_in_role(role_name, user_id, project_id): |
|
|
|
return Member.objects.filter( |
|
|
|
user_id=user_id, |
|
|
|
project_id=project_id, |
|
|
|
role_id=Subquery(Role.objects.filter(name=role_name).values('id')), |
|
|
|
).exists() |
|
|
|
|
|
|
|
|
|
|
|
IsInProjectReadOnlyOrAdmin = (IsAnnotatorAndReadOnly | IsAnnotationApproverAndReadOnly | IsProjectAdmin) |
|
|
|
IsInProjectOrAdmin = (IsAnnotator | IsAnnotationApprover | IsProjectAdmin) |