2019-12-05 07:09:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import generics
|
|
|
|
from django.db.models import F, Value
|
2020-08-16 15:08:58 +00:00
|
|
|
from django.db.models import Q
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.db.models.functions import Concat
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.models import Node, Asset
|
|
|
|
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
|
|
|
|
from common.permissions import IsOrgAdmin
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
|
|
|
from perms import models
|
2021-02-25 06:45:21 +00:00
|
|
|
from perms.utils.asset.user_permission import UserGrantedAssetsQueryUtils
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionUserRelationViewSet', 'AssetPermissionUserGroupRelationViewSet',
|
|
|
|
'AssetPermissionAssetRelationViewSet', 'AssetPermissionNodeRelationViewSet',
|
|
|
|
'AssetPermissionSystemUserRelationViewSet', 'AssetPermissionAllAssetListApi',
|
|
|
|
'AssetPermissionAllUserListApi',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
class RelationMixin(OrgRelationMixin, OrgBulkModelViewSet):
|
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
|
2019-12-05 07:09:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
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()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(user_display=F('user__name'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAllUserListApi(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
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
|
2019-12-05 07:09:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
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()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(usergroup_display=F('usergroup__name'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAssetRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionAssetRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.assets.field
|
2019-12-05 07:09:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'asset', 'assetpermission',
|
|
|
|
]
|
|
|
|
search_fields = ["id", "asset__hostname", "asset__ip", "assetpermission__name"]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(asset_display=F('asset__hostname'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAllAssetListApi(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
serializer_class = serializers.AssetPermissionAllAssetSerializer
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("hostname", "ip")
|
|
|
|
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
|
2019-12-05 07:09:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
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()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(node_key=F('node__key'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionSystemUserRelationViewSet(RelationMixin):
|
|
|
|
serializer_class = serializers.AssetPermissionSystemUserRelationSerializer
|
2020-08-16 15:08:58 +00:00
|
|
|
m2m_field = models.AssetPermission.system_users.field
|
2019-12-05 07:09:25 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'id', 'systemuser', 'assetpermission',
|
|
|
|
]
|
|
|
|
search_fields = [
|
|
|
|
"assetpermission__name", "systemuser__name", "systemuser__username"
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset \
|
|
|
|
.annotate(systemuser_display=Concat(
|
|
|
|
F('systemuser__name'), Value('('), F('systemuser__username'),
|
|
|
|
Value(')')
|
|
|
|
))
|
|
|
|
return queryset
|