0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 08:40:03 +01:00
posthog/ee/api/rbac/organization_resource_access.py
Zach Waterfield 8d01d5ef54
feat: rbac initial set up (#25745)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2024-10-29 17:17:27 -04:00

48 lines
1.6 KiB
Python

from rest_framework import mixins, serializers, viewsets
from ee.api.rbac.role import RolePermissions
from ee.models.rbac.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()