2019-03-04 12:45:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from common.permissions import IsOrgAdmin
|
2019-10-18 07:05:45 +00:00
|
|
|
from orgs.mixins.api import OrgModelViewSet
|
2019-03-04 12:45:57 +00:00
|
|
|
from common.utils import get_object_or_none
|
2019-07-02 08:45:26 +00:00
|
|
|
from ..models import AssetPermission
|
2019-03-04 12:45:57 +00:00
|
|
|
from ..hands import (
|
|
|
|
User, UserGroup, Asset, Node, SystemUser,
|
|
|
|
)
|
|
|
|
from .. import serializers
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2019-12-05 07:09:25 +00:00
|
|
|
'AssetPermissionViewSet',
|
2019-03-04 12:45:57 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
class AssetPermissionViewSet(OrgModelViewSet):
|
2019-03-04 12:45:57 +00:00
|
|
|
"""
|
|
|
|
资产授权列表的增删改查api
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = AssetPermission
|
2019-12-16 08:53:29 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.AssetPermissionCreateUpdateSerializer,
|
|
|
|
'display': serializers.AssetPermissionListSerializer
|
|
|
|
}
|
2019-03-04 12:45:57 +00:00
|
|
|
filter_fields = ['name']
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset().prefetch_related(
|
|
|
|
"nodes", "assets", "users", "user_groups", "system_users"
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def is_query_all(self):
|
|
|
|
query_all = self.request.query_params.get('all', '1') == '1'
|
|
|
|
return query_all
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
|
|
def filter_valid(self, queryset):
|
2019-08-28 03:43:55 +00:00
|
|
|
valid_query = self.request.query_params.get('is_valid', None)
|
|
|
|
if valid_query is None:
|
2019-03-04 12:45:57 +00:00
|
|
|
return queryset
|
2019-08-28 03:43:55 +00:00
|
|
|
invalid = valid_query in ['0', 'N', 'false', 'False']
|
|
|
|
if invalid:
|
|
|
|
queryset = queryset.invalid()
|
2019-03-04 12:45:57 +00:00
|
|
|
else:
|
2019-08-28 03:43:55 +00:00
|
|
|
queryset = queryset.valid()
|
2019-03-04 12:45:57 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_system_user(self, queryset):
|
|
|
|
system_user_id = self.request.query_params.get('system_user_id')
|
|
|
|
system_user_name = self.request.query_params.get('system_user')
|
|
|
|
if system_user_id:
|
|
|
|
system_user = get_object_or_none(SystemUser, pk=system_user_id)
|
|
|
|
elif system_user_name:
|
|
|
|
system_user = get_object_or_none(SystemUser, name=system_user_name)
|
|
|
|
else:
|
|
|
|
return queryset
|
|
|
|
if not system_user:
|
|
|
|
return queryset.none()
|
|
|
|
queryset = queryset.filter(system_users=system_user)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_node(self, queryset):
|
|
|
|
node_id = self.request.query_params.get('node_id')
|
|
|
|
node_name = self.request.query_params.get('node')
|
|
|
|
if node_id:
|
2019-10-17 03:59:18 +00:00
|
|
|
_nodes = Node.objects.filter(pk=node_id)
|
2019-03-04 12:45:57 +00:00
|
|
|
elif node_name:
|
2019-10-17 03:59:18 +00:00
|
|
|
_nodes = Node.objects.filter(value=node_name)
|
2019-03-04 12:45:57 +00:00
|
|
|
else:
|
|
|
|
return queryset
|
2019-10-17 03:59:18 +00:00
|
|
|
if not _nodes:
|
2019-03-04 12:45:57 +00:00
|
|
|
return queryset.none()
|
2019-10-17 03:03:06 +00:00
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
if not self.is_query_all():
|
|
|
|
queryset = queryset.filter(nodes__in=_nodes)
|
|
|
|
return queryset
|
|
|
|
nodes = set(_nodes)
|
2019-10-17 03:59:18 +00:00
|
|
|
for node in _nodes:
|
|
|
|
nodes |= set(node.get_ancestors(with_self=True))
|
2019-03-04 12:45:57 +00:00
|
|
|
queryset = queryset.filter(nodes__in=nodes)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_asset(self, queryset):
|
|
|
|
asset_id = self.request.query_params.get('asset_id')
|
|
|
|
hostname = self.request.query_params.get('hostname')
|
|
|
|
ip = self.request.query_params.get('ip')
|
|
|
|
if asset_id:
|
|
|
|
assets = Asset.objects.filter(pk=asset_id)
|
|
|
|
elif hostname:
|
|
|
|
assets = Asset.objects.filter(hostname=hostname)
|
|
|
|
elif ip:
|
|
|
|
assets = Asset.objects.filter(ip=ip)
|
|
|
|
else:
|
|
|
|
return queryset
|
|
|
|
if not assets:
|
|
|
|
return queryset.none()
|
2019-12-05 07:09:25 +00:00
|
|
|
if not self.is_query_all():
|
|
|
|
queryset = queryset.filter(assets__in=assets)
|
|
|
|
return queryset
|
2019-09-11 13:22:56 +00:00
|
|
|
inherit_all_nodes = set()
|
|
|
|
inherit_nodes_keys = assets.all().values_list('nodes__key', flat=True)
|
|
|
|
|
|
|
|
for key in inherit_nodes_keys:
|
2019-09-12 09:58:52 +00:00
|
|
|
if key is None:
|
|
|
|
continue
|
2019-09-24 07:00:32 +00:00
|
|
|
ancestor_keys = Node.get_node_ancestor_keys(key, with_self=True)
|
2019-09-11 13:22:56 +00:00
|
|
|
inherit_all_nodes.update(ancestor_keys)
|
|
|
|
queryset = queryset.filter(
|
|
|
|
Q(assets__in=assets) | Q(nodes__key__in=inherit_all_nodes)
|
2019-09-24 07:00:32 +00:00
|
|
|
).distinct()
|
2019-03-04 12:45:57 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_user(self, queryset):
|
|
|
|
user_id = self.request.query_params.get('user_id')
|
|
|
|
username = self.request.query_params.get('username')
|
|
|
|
if user_id:
|
|
|
|
user = get_object_or_none(User, pk=user_id)
|
|
|
|
elif username:
|
|
|
|
user = get_object_or_none(User, username=username)
|
|
|
|
else:
|
|
|
|
return queryset
|
|
|
|
if not user:
|
|
|
|
return queryset.none()
|
2019-12-05 07:09:25 +00:00
|
|
|
if not self.is_query_all():
|
|
|
|
queryset = queryset.filter(users=user)
|
|
|
|
return queryset
|
|
|
|
groups = user.groups.all()
|
|
|
|
queryset = queryset.filter(
|
|
|
|
Q(users=user) | Q(user_groups__in=groups)
|
|
|
|
).distinct()
|
|
|
|
return queryset
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
|
|
def filter_user_group(self, queryset):
|
|
|
|
user_group_id = self.request.query_params.get('user_group_id')
|
|
|
|
user_group_name = self.request.query_params.get('user_group')
|
|
|
|
if user_group_id:
|
|
|
|
group = get_object_or_none(UserGroup, pk=user_group_id)
|
|
|
|
elif user_group_name:
|
|
|
|
group = get_object_or_none(UserGroup, name=user_group_name)
|
|
|
|
else:
|
|
|
|
return queryset
|
|
|
|
if not group:
|
|
|
|
return queryset.none()
|
|
|
|
queryset = queryset.filter(user_groups=group)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_keyword(self, queryset):
|
|
|
|
keyword = self.request.query_params.get('search')
|
|
|
|
if not keyword:
|
|
|
|
return queryset
|
|
|
|
queryset = queryset.filter(name__icontains=keyword)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
queryset = self.filter_valid(queryset)
|
2019-08-08 10:32:44 +00:00
|
|
|
queryset = self.filter_user(queryset)
|
2019-03-04 12:45:57 +00:00
|
|
|
queryset = self.filter_keyword(queryset)
|
|
|
|
queryset = self.filter_asset(queryset)
|
|
|
|
queryset = self.filter_node(queryset)
|
|
|
|
queryset = self.filter_system_user(queryset)
|
|
|
|
queryset = self.filter_user_group(queryset)
|
2019-09-24 03:35:57 +00:00
|
|
|
queryset = queryset.distinct()
|
2019-03-04 12:45:57 +00:00
|
|
|
return queryset
|