permissions.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from glitchtip.permissions import ScopedPermission
  2. class OrganizationPermission(ScopedPermission):
  3. scope_map = {
  4. "GET": ["org:read", "org:write", "org:admin"],
  5. "POST": ["org:write", "org:admin"],
  6. "PUT": ["org:write", "org:admin"],
  7. "DELETE": ["org:admin"],
  8. }
  9. def get_user_scopes(self, obj, user):
  10. return obj.get_user_scopes(user)
  11. class OrganizationMemberPermission(ScopedPermission):
  12. scope_map = {
  13. "GET": ["member:read", "member:write", "member:admin"],
  14. "POST": ["member:write", "member:admin"],
  15. "PUT": ["member:write", "member:admin"],
  16. "DELETE": ["member:admin"],
  17. }
  18. def has_permission(self, request, view):
  19. # teams action has entirely different permissions
  20. if view.action == "teams":
  21. permission = OrganizationMemberTeamsPermission()
  22. if request.auth:
  23. allowed_scopes = permission.get_allowed_scopes(request, view)
  24. current_scopes = request.auth.get_scopes()
  25. return any(s in allowed_scopes for s in current_scopes)
  26. return bool(request.user and request.user.is_authenticated)
  27. return super().has_permission(request, view)
  28. def get_user_scopes(self, obj, user):
  29. return obj.organization.get_user_scopes(user)
  30. class OrganizationMemberTeamsPermission(OrganizationMemberPermission):
  31. _allowed_scopes = [
  32. "org:read",
  33. "org:write",
  34. "org:admin",
  35. "member:read",
  36. "member:write",
  37. "member:admin",
  38. ]
  39. scope_map = {
  40. "POST": _allowed_scopes,
  41. "DELETE": _allowed_scopes,
  42. }