0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 08:40:03 +01:00
posthog/ee/api/organization_resource_access.py
2024-02-20 11:02:58 +00:00

48 lines
1.6 KiB
Python

from rest_framework import mixins, serializers, viewsets
from ee.api.role import RolePermissions
from ee.models.organization_resource_access import OrganizationResourceAccess
from posthog.api.routing import TeamAndOrgViewSetMixin
class OrganizationResourceAccessSerializer(serializers.ModelSerializer):
class Meta:
model = OrganizationResourceAccess
fields = [
"id",
"resource",
"access_level",
"organization",
"created_at",
"updated_at",
"created_by",
]
read_only_fields = ["id", "created_at", "created_by", "organization"]
def validate_resource(self, resource):
if OrganizationResourceAccess.objects.filter(
organization=self.context["request"].user.organization,
resource=resource,
).exists():
raise serializers.ValidationError("This resource access already exists.", code="unique")
return resource
def create(self, validated_data):
validated_data["organization"] = self.context["request"].user.organization
return super().create(validated_data)
class OrganizationResourceAccessViewSet(
TeamAndOrgViewSetMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
scope_object = "INTERNAL"
permission_classes = [RolePermissions]
serializer_class = OrganizationResourceAccessSerializer
queryset = OrganizationResourceAccess.objects.all()