You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

  1. from django.contrib.auth.mixins import UserPassesTestMixin
  2. from django.shortcuts import get_object_or_404
  3. from rest_framework.permissions import BasePermission, SAFE_METHODS, IsAdminUser
  4. from .models import Project
  5. class IsProjectUser(BasePermission):
  6. def has_permission(self, request, view):
  7. user = request.user
  8. project_id = view.kwargs.get('project_id')
  9. project = get_object_or_404(Project, pk=project_id)
  10. return user in project.users.all()
  11. class IsAdminUserAndWriteOnly(BasePermission):
  12. def has_permission(self, request, view):
  13. if request.method in SAFE_METHODS:
  14. return True
  15. return IsAdminUser().has_permission(request, view)
  16. class IsOwnAnnotation(BasePermission):
  17. def has_permission(self, request, view):
  18. user = request.user
  19. project_id = view.kwargs.get('project_id')
  20. annotation_id = view.kwargs.get('annotation_id')
  21. project = get_object_or_404(Project, pk=project_id)
  22. Annotation = project.get_annotation_class()
  23. annotation = Annotation.objects.get(id=annotation_id)
  24. return annotation.user == user
  25. class SuperUserMixin(UserPassesTestMixin):
  26. def test_func(self):
  27. return self.request.user.is_superuser