2019-12-05 07:09:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import generics
|
2022-07-28 10:50:58 +00:00
|
|
|
from django.db.models import F
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from orgs.mixins.api import OrgRelationMixin
|
2019-12-05 07:09:25 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
from orgs.utils import current_org
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
|
|
|
from perms import models
|
2022-08-23 02:23:48 +00:00
|
|
|
from perms.utils.user_permission import UserGrantedAssetsQueryUtils
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionUserRelationViewSet', 'AssetPermissionUserGroupRelationViewSet',
|
|
|
|
'AssetPermissionAssetRelationViewSet', 'AssetPermissionNodeRelationViewSet',
|
2022-07-28 10:50:58 +00:00
|
|
|
'AssetPermissionAllAssetListApi', 'AssetPermissionAllUserListApi',
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
class RelationMixin(OrgRelationMixin, OrgBulkModelViewSet):
|
2022-03-07 10:22:21 +00:00
|
|
|
perm_model = models.AssetPermission
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def get_queryset(self):
|
2020-08-16 15:08:58 +00:00
|
|
|
queryset = super().get_queryset()
|
2019-12-05 07:09:25 +00:00
|
|
|
org_id = current_org.org_id()
|
|
|
|
if org_id is not None:
|
|
|
|
queryset = queryset.filter(assetpermission__org_id=org_id)
|
|
|
|
queryset = queryset.annotate(assetpermission_display=F('assetpermission__name'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUserRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionUserRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.users.field
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', "user", "assetpermission",
|
|
|
|
]
|
|
|
|
search_fields = ("user__name", "user__username", "assetpermission__name")
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2022-09-08 12:31:57 +00:00
|
|
|
queryset = queryset.annotate(user_display=F('user__name'))
|
2019-12-05 07:09:25 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAllUserListApi(generics.ListAPIView):
|
|
|
|
serializer_class = serializers.AssetPermissionAllUserSerializer
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("username", "name")
|
|
|
|
search_fields = filterset_fields
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
pk = self.kwargs.get("pk")
|
|
|
|
perm = get_object_or_404(models.AssetPermission, pk=pk)
|
|
|
|
users = perm.get_all_users().only(
|
|
|
|
*self.serializer_class.Meta.only_fields
|
|
|
|
)
|
|
|
|
return users
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUserGroupRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionUserGroupRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.user_groups.field
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', "usergroup", "assetpermission"
|
|
|
|
]
|
|
|
|
search_fields = ["usergroup__name", "assetpermission__name"]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2022-09-08 12:31:57 +00:00
|
|
|
queryset = queryset.annotate(usergroup_display=F('usergroup__name'))
|
2019-12-05 07:09:25 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAssetRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionAssetRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.assets.field
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'asset', 'assetpermission',
|
|
|
|
]
|
2022-08-19 10:49:00 +00:00
|
|
|
search_fields = ["id", "asset__name", "asset__ip", "assetpermission__name"]
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2022-09-08 12:31:57 +00:00
|
|
|
queryset = queryset.annotate(asset_display=F('asset__name'))
|
2019-12-05 07:09:25 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAllAssetListApi(generics.ListAPIView):
|
|
|
|
serializer_class = serializers.AssetPermissionAllAssetSerializer
|
2022-08-11 07:45:03 +00:00
|
|
|
filterset_fields = ("name", "ip")
|
2021-01-07 02:53:10 +00:00
|
|
|
search_fields = filterset_fields
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
pk = self.kwargs.get("pk")
|
2021-02-25 06:45:21 +00:00
|
|
|
query_utils = UserGrantedAssetsQueryUtils(None, asset_perm_ids=[pk])
|
|
|
|
assets = query_utils.get_all_granted_assets()
|
2019-12-05 07:09:25 +00:00
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionNodeRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionNodeRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.nodes.field
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'node', 'assetpermission',
|
|
|
|
]
|
|
|
|
search_fields = ["node__value", "assetpermission__name"]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2022-09-08 12:31:57 +00:00
|
|
|
queryset = queryset.annotate(node_key=F('node__key'))
|
2019-12-05 07:09:25 +00:00
|
|
|
return queryset
|
|
|
|
|