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.

38 lines
1.1 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, SequenceAnnotation
  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 IsMyEntity(BasePermission):
  17. def has_permission(self, request, view):
  18. entity_id = view.kwargs.get('entity_id')
  19. entity = get_object_or_404(SequenceAnnotation, pk=entity_id)
  20. return entity.user == request.user
  21. class SuperUserMixin(UserPassesTestMixin):
  22. def test_func(self):
  23. return self.request.user.is_superuser