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 SuperUserMixin(UserPassesTestMixin):
  17. def test_func(self):
  18. return self.request.user.is_superuser
  19. class IsOwnAnnotation(BasePermission):
  20. def has_permission(self, request, view):
  21. project_id = view.kwargs.get('project_id')
  22. annotation_id = view.kwargs.get('annotation_id')
  23. project = get_object_or_404(Project, pk=project_id)
  24. model = project.get_annotation_class()
  25. annotation = model.objects.filter(id=annotation_id, user=request.user)
  26. return annotation.exists()