2016-08-09 09:27:37 +00:00
|
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
#
|
|
|
|
|
|
2017-01-17 08:34:47 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2016-10-19 11:30:55 +00:00
|
|
|
|
from rest_framework.views import APIView, Response
|
2018-12-10 02:11:54 +00:00
|
|
|
|
from rest_framework.generics import ListAPIView, get_object_or_404, \
|
|
|
|
|
RetrieveUpdateAPIView
|
2016-11-09 16:18:57 +00:00
|
|
|
|
from rest_framework import viewsets
|
2018-10-31 07:31:09 +00:00
|
|
|
|
from rest_framework.pagination import LimitOffsetPagination
|
2017-12-21 10:54:29 +00:00
|
|
|
|
|
2018-09-13 03:17:55 +00:00
|
|
|
|
from common.utils import set_or_append_attr_bulk
|
2018-08-07 04:34:35 +00:00
|
|
|
|
from common.permissions import IsValidUser, IsOrgAdmin, IsOrgAdminOrAppUser
|
2018-12-10 02:11:54 +00:00
|
|
|
|
from common.tree import TreeNode, TreeNodeSerializer
|
2018-08-07 04:34:35 +00:00
|
|
|
|
from orgs.mixins import RootOrgViewMixin
|
2018-12-10 02:11:54 +00:00
|
|
|
|
from orgs.utils import set_to_root_org
|
2018-04-08 12:02:40 +00:00
|
|
|
|
from .utils import AssetPermissionUtil
|
|
|
|
|
from .models import AssetPermission
|
|
|
|
|
from .hands import AssetGrantedSerializer, User, UserGroup, Asset, Node, \
|
2018-12-17 10:20:44 +00:00
|
|
|
|
SystemUser, NodeSerializer
|
2016-11-04 10:33:16 +00:00
|
|
|
|
from . import serializers
|
2018-10-31 07:31:09 +00:00
|
|
|
|
from .mixins import AssetsFilterMixin
|
2016-11-04 10:33:16 +00:00
|
|
|
|
|
|
|
|
|
|
2018-10-30 04:06:39 +00:00
|
|
|
|
__all__ = [
|
|
|
|
|
'AssetPermissionViewSet', 'UserGrantedAssetsApi', 'UserGrantedNodesApi',
|
|
|
|
|
'UserGrantedNodesWithAssetsApi', 'UserGrantedNodeAssetsApi', 'UserGroupGrantedAssetsApi',
|
|
|
|
|
'UserGroupGrantedNodesApi', 'UserGroupGrantedNodesWithAssetsApi', 'UserGroupGrantedNodeAssetsApi',
|
|
|
|
|
'ValidateUserAssetPermissionApi', 'AssetPermissionRemoveUserApi', 'AssetPermissionAddUserApi',
|
|
|
|
|
'AssetPermissionRemoveAssetApi', 'AssetPermissionAddAssetApi', 'UserGrantedNodeChildrenApi',
|
2018-12-10 02:11:54 +00:00
|
|
|
|
'UserGrantedNodesWithAssetsAsTreeApi',
|
2018-10-30 04:06:39 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2016-11-09 16:18:57 +00:00
|
|
|
|
class AssetPermissionViewSet(viewsets.ModelViewSet):
|
2017-11-23 03:26:17 +00:00
|
|
|
|
"""
|
|
|
|
|
资产授权列表的增删改查api
|
|
|
|
|
"""
|
2018-04-08 12:02:40 +00:00
|
|
|
|
queryset = AssetPermission.objects.all()
|
2018-02-01 09:14:15 +00:00
|
|
|
|
serializer_class = serializers.AssetPermissionCreateUpdateSerializer
|
2018-10-31 07:31:09 +00:00
|
|
|
|
pagination_class = LimitOffsetPagination
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2016-10-19 11:30:55 +00:00
|
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
|
def get_serializer_class(self):
|
|
|
|
|
if self.action in ("list", 'retrieve'):
|
|
|
|
|
return serializers.AssetPermissionListSerializer
|
|
|
|
|
return self.serializer_class
|
2016-11-15 11:33:04 +00:00
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
|
def get_queryset(self):
|
2018-10-31 07:31:09 +00:00
|
|
|
|
queryset = super().get_queryset().all()
|
|
|
|
|
search = self.request.query_params.get('search')
|
2018-04-10 12:29:06 +00:00
|
|
|
|
asset_id = self.request.query_params.get('asset')
|
|
|
|
|
node_id = self.request.query_params.get('node')
|
|
|
|
|
inherit_nodes = set()
|
2018-10-31 07:31:09 +00:00
|
|
|
|
|
|
|
|
|
if search:
|
|
|
|
|
queryset = queryset.filter(name__icontains=search)
|
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
|
if not asset_id and not node_id:
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
permissions = set()
|
|
|
|
|
if asset_id:
|
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
|
|
|
|
permissions = set(queryset.filter(assets=asset))
|
|
|
|
|
for node in asset.nodes.all():
|
2018-05-31 11:47:57 +00:00
|
|
|
|
inherit_nodes.update(set(node.get_ancestor(with_self=True)))
|
2018-04-10 12:29:06 +00:00
|
|
|
|
elif node_id:
|
|
|
|
|
node = get_object_or_404(Node, pk=node_id)
|
|
|
|
|
permissions = set(queryset.filter(nodes=node))
|
2018-05-31 11:47:57 +00:00
|
|
|
|
inherit_nodes = node.get_ancestor()
|
2018-04-10 12:29:06 +00:00
|
|
|
|
|
|
|
|
|
for n in inherit_nodes:
|
|
|
|
|
_permissions = queryset.filter(nodes=n)
|
|
|
|
|
set_or_append_attr_bulk(_permissions, "inherit", n.value)
|
|
|
|
|
permissions.update(_permissions)
|
2018-10-31 07:31:09 +00:00
|
|
|
|
|
|
|
|
|
return list(permissions)
|
2018-04-10 12:29:06 +00:00
|
|
|
|
|
2017-02-06 15:13:27 +00:00
|
|
|
|
|
2018-10-31 07:31:09 +00:00
|
|
|
|
class UserGrantedAssetsApi(AssetsFilterMixin, ListAPIView):
|
2017-11-23 03:26:17 +00:00
|
|
|
|
"""
|
|
|
|
|
用户授权的所有资产
|
|
|
|
|
"""
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2016-11-15 11:33:04 +00:00
|
|
|
|
serializer_class = AssetGrantedSerializer
|
2018-10-31 07:31:09 +00:00
|
|
|
|
pagination_class = LimitOffsetPagination
|
2018-09-13 04:16:49 +00:00
|
|
|
|
|
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
2018-09-13 03:17:55 +00:00
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
2018-09-13 04:16:49 +00:00
|
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
|
def get_queryset(self):
|
2018-09-13 04:16:49 +00:00
|
|
|
|
self.change_org_if_need()
|
2016-11-15 11:33:04 +00:00
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
2017-01-17 08:34:47 +00:00
|
|
|
|
queryset = []
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
|
if user_id:
|
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
else:
|
|
|
|
|
user = self.request.user
|
2016-11-15 11:33:04 +00:00
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user)
|
|
|
|
|
for k, v in util.get_assets().items():
|
2018-07-25 03:09:58 +00:00
|
|
|
|
system_users_granted = [s for s in v if s.protocol == k.protocol]
|
2018-02-27 11:39:27 +00:00
|
|
|
|
k.system_users_granted = system_users_granted
|
2018-02-07 15:25:15 +00:00
|
|
|
|
queryset.append(k)
|
2018-10-31 07:31:09 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
return queryset
|
2016-11-15 11:33:04 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
def get_permissions(self):
|
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
|
return super().get_permissions()
|
2017-11-23 03:26:17 +00:00
|
|
|
|
|
|
|
|
|
|
2018-09-13 03:17:55 +00:00
|
|
|
|
class UserGrantedNodesApi(ListAPIView):
|
|
|
|
|
"""
|
|
|
|
|
查询用户授权的所有节点的API, 如果是超级用户或者是 app,切换到root org
|
|
|
|
|
"""
|
2018-10-16 08:47:47 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-02-08 03:34:21 +00:00
|
|
|
|
serializer_class = NodeSerializer
|
2018-09-13 04:16:49 +00:00
|
|
|
|
|
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
2018-09-13 03:17:55 +00:00
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
|
def get_queryset(self):
|
2018-09-13 04:16:49 +00:00
|
|
|
|
self.change_org_if_need()
|
2018-02-08 03:34:21 +00:00
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
|
if user_id:
|
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
|
else:
|
|
|
|
|
user = self.request.user
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-02-08 03:34:21 +00:00
|
|
|
|
return nodes.keys()
|
|
|
|
|
|
2018-04-16 09:01:25 +00:00
|
|
|
|
def get_permissions(self):
|
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
2018-10-31 07:31:09 +00:00
|
|
|
|
class UserGrantedNodesWithAssetsApi(AssetsFilterMixin, ListAPIView):
|
2018-09-13 03:17:55 +00:00
|
|
|
|
"""
|
|
|
|
|
用户授权的节点并带着节点下资产的api
|
|
|
|
|
"""
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
serializer_class = serializers.NodeGrantedSerializer
|
2018-09-13 04:16:49 +00:00
|
|
|
|
|
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
2018-09-13 03:17:55 +00:00
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
2016-11-15 11:33:04 +00:00
|
|
|
|
def get_queryset(self):
|
2018-09-13 04:16:49 +00:00
|
|
|
|
self.change_org_if_need()
|
2016-11-15 11:33:04 +00:00
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
2017-11-14 01:44:16 +00:00
|
|
|
|
queryset = []
|
2018-02-07 15:25:15 +00:00
|
|
|
|
if not user_id:
|
|
|
|
|
user = self.request.user
|
|
|
|
|
else:
|
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
2017-03-29 07:26:32 +00:00
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
for node, _assets in nodes.items():
|
|
|
|
|
assets = _assets.keys()
|
2018-04-11 04:13:49 +00:00
|
|
|
|
for k, v in _assets.items():
|
2018-07-25 03:09:58 +00:00
|
|
|
|
system_users_granted = [s for s in v if s.protocol == k.protocol]
|
2018-04-11 04:13:49 +00:00
|
|
|
|
k.system_users_granted = system_users_granted
|
2018-04-08 12:02:40 +00:00
|
|
|
|
node.assets_granted = assets
|
2018-02-07 15:25:15 +00:00
|
|
|
|
queryset.append(node)
|
2017-11-23 03:26:17 +00:00
|
|
|
|
return queryset
|
2017-03-29 07:26:32 +00:00
|
|
|
|
|
2018-10-31 07:31:09 +00:00
|
|
|
|
def sort_assets(self, queryset):
|
|
|
|
|
for node in queryset:
|
|
|
|
|
node.assets_granted = super().sort_assets(node.assets_granted)
|
|
|
|
|
return queryset
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
def get_permissions(self):
|
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
|
return super().get_permissions()
|
2016-11-15 15:09:36 +00:00
|
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
|
class UserGrantedNodesWithAssetsAsTreeApi(ListAPIView):
|
|
|
|
|
serializer_class = TreeNodeSerializer
|
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
show_assets = True
|
|
|
|
|
system_user_id = None
|
|
|
|
|
|
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.show_assets = request.query_params.get('show_assets', '1') == '1'
|
|
|
|
|
self.system_user_id = request.query_params.get('system_user')
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_node_to_tree_node(node):
|
|
|
|
|
name = '{} ({})'.format(node.value, node.assets_amount)
|
|
|
|
|
node_serializer = serializers.GrantedNodeSerializer(node)
|
|
|
|
|
data = {
|
|
|
|
|
'id': node.key,
|
|
|
|
|
'name': name,
|
|
|
|
|
'title': name,
|
|
|
|
|
'pId': node.parent_key,
|
|
|
|
|
'isParent': True,
|
|
|
|
|
'open': node.is_root(),
|
|
|
|
|
'meta': {
|
|
|
|
|
'node': node_serializer.data,
|
|
|
|
|
'type': 'node'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
|
return tree_node
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_asset_to_tree_node(node, asset, system_users):
|
2018-12-12 04:11:12 +00:00
|
|
|
|
system_users_protocol_matched = [s for s in system_users if s.protocol == asset.protocol]
|
2018-12-10 02:11:54 +00:00
|
|
|
|
icon_skin = 'file'
|
|
|
|
|
if asset.platform.lower() == 'windows':
|
|
|
|
|
icon_skin = 'windows'
|
|
|
|
|
elif asset.platform.lower() == 'linux':
|
|
|
|
|
icon_skin = 'linux'
|
2018-12-18 12:01:59 +00:00
|
|
|
|
system_users = []
|
|
|
|
|
for system_user in system_users_protocol_matched:
|
|
|
|
|
system_users.append({
|
|
|
|
|
'id': system_user.id,
|
|
|
|
|
'name': system_user.name,
|
|
|
|
|
'username': system_user.username,
|
|
|
|
|
'protocol': system_user.protocol,
|
|
|
|
|
'priority': system_user.priority,
|
|
|
|
|
'login_mode': system_user.login_mode,
|
|
|
|
|
'comment': system_user.comment,
|
|
|
|
|
})
|
2018-12-10 02:11:54 +00:00
|
|
|
|
data = {
|
|
|
|
|
'id': str(asset.id),
|
|
|
|
|
'name': asset.hostname,
|
|
|
|
|
'title': asset.ip,
|
|
|
|
|
'pId': node.key,
|
|
|
|
|
'isParent': False,
|
|
|
|
|
'open': False,
|
|
|
|
|
'iconSkin': icon_skin,
|
|
|
|
|
'meta': {
|
2018-12-18 12:01:59 +00:00
|
|
|
|
'system_users': system_users,
|
2018-12-10 02:11:54 +00:00
|
|
|
|
'type': 'asset',
|
2018-12-18 12:01:59 +00:00
|
|
|
|
'asset': {
|
|
|
|
|
'id': asset.id,
|
|
|
|
|
'hostname': asset.hostname,
|
|
|
|
|
'ip': asset.ip,
|
|
|
|
|
'port': asset.port,
|
|
|
|
|
'protocol': asset.protocol,
|
|
|
|
|
'platform': asset.platform,
|
|
|
|
|
'domain': None if not asset.domain else asset.domain.id,
|
|
|
|
|
'is_active': asset.is_active,
|
|
|
|
|
'comment': asset.comment
|
|
|
|
|
},
|
2018-12-10 02:11:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
|
return tree_node
|
|
|
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
self.change_org_if_need()
|
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
|
queryset = []
|
|
|
|
|
if not user_id:
|
|
|
|
|
user = self.request.user
|
|
|
|
|
else:
|
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
|
util = AssetPermissionUtil(user)
|
|
|
|
|
if self.system_user_id:
|
|
|
|
|
util.filter_permission_with_system_user(system_user=self.system_user_id)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
|
|
|
|
for node, assets in nodes.items():
|
|
|
|
|
data = self.parse_node_to_tree_node(node)
|
|
|
|
|
queryset.append(data)
|
|
|
|
|
if not self.show_assets:
|
|
|
|
|
continue
|
|
|
|
|
for asset, system_users in assets.items():
|
|
|
|
|
data = self.parse_asset_to_tree_node(node, asset, system_users)
|
|
|
|
|
queryset.append(data)
|
|
|
|
|
queryset = sorted(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
2018-10-31 07:31:09 +00:00
|
|
|
|
class UserGrantedNodeAssetsApi(AssetsFilterMixin, ListAPIView):
|
2018-09-13 03:17:55 +00:00
|
|
|
|
"""
|
|
|
|
|
查询用户授权的节点下的资产的api, 与上面api不同的是,只返回某个节点下的资产
|
|
|
|
|
"""
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-04-08 12:02:40 +00:00
|
|
|
|
serializer_class = AssetGrantedSerializer
|
2018-10-31 07:31:09 +00:00
|
|
|
|
pagination_class = LimitOffsetPagination
|
|
|
|
|
|
2018-09-13 04:16:49 +00:00
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
2018-09-13 03:17:55 +00:00
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
2018-04-08 12:02:40 +00:00
|
|
|
|
def get_queryset(self):
|
2018-09-13 04:16:49 +00:00
|
|
|
|
self.change_org_if_need()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
|
node_id = self.kwargs.get('node_id')
|
|
|
|
|
|
|
|
|
|
if user_id:
|
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
|
else:
|
|
|
|
|
user = self.request.user
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user)
|
2018-05-28 08:44:00 +00:00
|
|
|
|
node = get_object_or_404(Node, id=node_id)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
assets = nodes.get(node, [])
|
|
|
|
|
for asset, system_users in assets.items():
|
|
|
|
|
asset.system_users_granted = system_users
|
2018-10-31 07:31:09 +00:00
|
|
|
|
|
|
|
|
|
assets = list(assets.keys())
|
2018-04-08 12:02:40 +00:00
|
|
|
|
return assets
|
|
|
|
|
|
2018-04-11 03:34:15 +00:00
|
|
|
|
def get_permissions(self):
|
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
|
return super().get_permissions()
|
|
|
|
|
|
2018-04-08 12:02:40 +00:00
|
|
|
|
|
2016-11-15 15:09:36 +00:00
|
|
|
|
class UserGroupGrantedAssetsApi(ListAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2016-11-15 15:09:36 +00:00
|
|
|
|
serializer_class = AssetGrantedSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
2018-02-07 15:25:15 +00:00
|
|
|
|
queryset = []
|
2016-11-15 15:09:36 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
if not user_group_id:
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user_group)
|
|
|
|
|
assets = util.get_assets()
|
2018-02-07 15:25:15 +00:00
|
|
|
|
for k, v in assets.items():
|
|
|
|
|
k.system_users_granted = v
|
|
|
|
|
queryset.append(k)
|
2016-11-15 15:09:36 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
|
class UserGroupGrantedNodesApi(ListAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-08 03:34:21 +00:00
|
|
|
|
serializer_class = NodeSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
group_id = self.kwargs.get('pk', '')
|
|
|
|
|
queryset = []
|
|
|
|
|
|
|
|
|
|
if group_id:
|
|
|
|
|
group = get_object_or_404(UserGroup, id=group_id)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(group)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
return nodes.keys()
|
2018-02-08 03:34:21 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupGrantedNodesWithAssetsApi(ListAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
serializer_class = serializers.NodeGrantedSerializer
|
2016-11-15 15:09:36 +00:00
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
2018-02-07 15:25:15 +00:00
|
|
|
|
queryset = []
|
2016-11-15 15:09:36 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
if not user_group_id:
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user_group)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
for node, _assets in nodes.items():
|
|
|
|
|
assets = _assets.keys()
|
|
|
|
|
for asset, system_users in _assets.items():
|
|
|
|
|
asset.system_users_granted = system_users
|
|
|
|
|
node.assets_granted = assets
|
2018-02-07 15:25:15 +00:00
|
|
|
|
queryset.append(node)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
2018-04-08 12:02:40 +00:00
|
|
|
|
class UserGroupGrantedNodeAssetsApi(ListAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-04-08 12:02:40 +00:00
|
|
|
|
serializer_class = AssetGrantedSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
|
|
|
|
node_id = self.kwargs.get('node_id')
|
|
|
|
|
|
|
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
|
|
|
|
node = get_object_or_404(Node, id=node_id)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user_group)
|
|
|
|
|
nodes = util.get_nodes_with_assets()
|
2018-04-08 12:02:40 +00:00
|
|
|
|
assets = nodes.get(node, [])
|
|
|
|
|
for asset, system_users in assets.items():
|
|
|
|
|
asset.system_users_granted = system_users
|
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 04:06:39 +00:00
|
|
|
|
class ValidateUserAssetPermissionApi(RootOrgViewMixin, APIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
2017-01-17 09:11:01 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get(request):
|
2017-01-20 05:59:53 +00:00
|
|
|
|
user_id = request.query_params.get('user_id', '')
|
|
|
|
|
asset_id = request.query_params.get('asset_id', '')
|
|
|
|
|
system_id = request.query_params.get('system_user_id', '')
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
2017-01-17 09:11:01 +00:00
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
|
asset = get_object_or_404(Asset, id=asset_id)
|
|
|
|
|
system_user = get_object_or_404(SystemUser, id=system_id)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
util = AssetPermissionUtil(user)
|
|
|
|
|
assets_granted = util.get_assets()
|
2017-01-17 09:11:01 +00:00
|
|
|
|
if system_user in assets_granted.get(asset, []):
|
|
|
|
|
return Response({'msg': True}, status=200)
|
|
|
|
|
else:
|
|
|
|
|
return Response({'msg': False}, status=403)
|
2018-04-11 07:24:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionRemoveUserApi(RetrieveUpdateAPIView):
|
|
|
|
|
"""
|
|
|
|
|
将用户从授权中移除,Detail页面会调用
|
|
|
|
|
"""
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-11 07:24:12 +00:00
|
|
|
|
serializer_class = serializers.AssetPermissionUpdateUserSerializer
|
|
|
|
|
queryset = AssetPermission.objects.all()
|
|
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
|
perm = self.get_object()
|
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
|
users = serializer.validated_data.get('users')
|
|
|
|
|
if users:
|
|
|
|
|
perm.users.remove(*tuple(users))
|
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
|
else:
|
|
|
|
|
return Response({"error": serializer.errors})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAddUserApi(RetrieveUpdateAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-11 07:24:12 +00:00
|
|
|
|
serializer_class = serializers.AssetPermissionUpdateUserSerializer
|
|
|
|
|
queryset = AssetPermission.objects.all()
|
|
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
|
perm = self.get_object()
|
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
|
users = serializer.validated_data.get('users')
|
|
|
|
|
if users:
|
|
|
|
|
perm.users.add(*tuple(users))
|
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
|
else:
|
|
|
|
|
return Response({"error": serializer.errors})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionRemoveAssetApi(RetrieveUpdateAPIView):
|
|
|
|
|
"""
|
|
|
|
|
将用户从授权中移除,Detail页面会调用
|
|
|
|
|
"""
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-11 07:24:12 +00:00
|
|
|
|
serializer_class = serializers.AssetPermissionUpdateAssetSerializer
|
|
|
|
|
queryset = AssetPermission.objects.all()
|
|
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
|
perm = self.get_object()
|
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
|
if assets:
|
|
|
|
|
perm.assets.remove(*tuple(assets))
|
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
|
else:
|
|
|
|
|
return Response({"error": serializer.errors})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionAddAssetApi(RetrieveUpdateAPIView):
|
2018-08-07 04:34:35 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-11 07:24:12 +00:00
|
|
|
|
serializer_class = serializers.AssetPermissionUpdateAssetSerializer
|
|
|
|
|
queryset = AssetPermission.objects.all()
|
|
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
|
perm = self.get_object()
|
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
|
if assets:
|
|
|
|
|
perm.assets.add(*tuple(assets))
|
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
|
else:
|
|
|
|
|
return Response({"error": serializer.errors})
|
2018-10-30 04:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedNodeChildrenApi(ListAPIView):
|
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
serializer_class = serializers.AssetPermissionNodeSerializer
|
|
|
|
|
|
|
|
|
|
def change_org_if_need(self):
|
|
|
|
|
if self.request.user.is_superuser or \
|
|
|
|
|
self.request.user.is_app or \
|
|
|
|
|
self.kwargs.get('pk') is None:
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
|
|
|
|
def get_children_queryset(self):
|
|
|
|
|
util = AssetPermissionUtil(self.request.user)
|
|
|
|
|
node_id = self.request.query_params.get('id')
|
|
|
|
|
nodes_granted = util.get_nodes_with_assets()
|
2018-11-14 04:42:26 +00:00
|
|
|
|
if not nodes_granted:
|
2018-10-30 04:06:39 +00:00
|
|
|
|
return []
|
2018-11-14 04:42:26 +00:00
|
|
|
|
root_nodes = [node for node in nodes_granted.keys() if node.is_root()]
|
|
|
|
|
|
|
|
|
|
queryset = []
|
2018-10-30 04:06:39 +00:00
|
|
|
|
if node_id and node_id in [str(node.id) for node in nodes_granted]:
|
|
|
|
|
node = [node for node in nodes_granted if str(node.id) == node_id][0]
|
2018-11-14 04:42:26 +00:00
|
|
|
|
elif len(root_nodes) == 1:
|
|
|
|
|
node = root_nodes[0]
|
2018-10-30 04:06:39 +00:00
|
|
|
|
node.assets_amount = len(nodes_granted[node])
|
|
|
|
|
queryset.append(node)
|
2018-11-14 04:42:26 +00:00
|
|
|
|
else:
|
|
|
|
|
for node in root_nodes:
|
|
|
|
|
node.assets_amount = len(nodes_granted[node])
|
|
|
|
|
queryset.append(node)
|
|
|
|
|
return queryset
|
2018-10-30 04:06:39 +00:00
|
|
|
|
|
|
|
|
|
children = []
|
|
|
|
|
for child in node.get_children():
|
|
|
|
|
if child in nodes_granted:
|
|
|
|
|
child.assets_amount = len(nodes_granted[node])
|
|
|
|
|
children.append(child)
|
|
|
|
|
children = sorted(children, key=lambda x: x.value)
|
|
|
|
|
queryset.extend(children)
|
|
|
|
|
fake_nodes = []
|
|
|
|
|
for asset, system_users in nodes_granted[node].items():
|
|
|
|
|
fake_node = asset.as_node()
|
|
|
|
|
fake_node.assets_amount = 0
|
2018-11-09 03:23:41 +00:00
|
|
|
|
system_users = [s for s in system_users if s.protocol == asset.protocol]
|
2018-10-30 04:06:39 +00:00
|
|
|
|
fake_node.asset.system_users_granted = system_users
|
|
|
|
|
fake_node.key = node.key + ':0'
|
|
|
|
|
fake_nodes.append(fake_node)
|
|
|
|
|
fake_nodes = sorted(fake_nodes, key=lambda x: x.value)
|
|
|
|
|
queryset.extend(fake_nodes)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def get_search_queryset(self, keyword):
|
|
|
|
|
util = AssetPermissionUtil(self.request.user)
|
|
|
|
|
nodes_granted = util.get_nodes_with_assets()
|
|
|
|
|
queryset = []
|
|
|
|
|
for node, assets in nodes_granted.items():
|
|
|
|
|
matched_assets = []
|
|
|
|
|
node_matched = node.value.lower().find(keyword.lower()) >= 0
|
|
|
|
|
asset_has_matched = False
|
|
|
|
|
for asset, system_users in assets.items():
|
|
|
|
|
asset_matched = (asset.hostname.lower().find(keyword.lower()) >= 0) \
|
|
|
|
|
or (asset.ip.find(keyword.lower()) >= 0)
|
|
|
|
|
if node_matched or asset_matched:
|
|
|
|
|
asset_has_matched = True
|
|
|
|
|
fake_node = asset.as_node()
|
|
|
|
|
fake_node.assets_amount = 0
|
2018-11-09 03:23:41 +00:00
|
|
|
|
system_users = [s for s in system_users if
|
|
|
|
|
s.protocol == asset.protocol]
|
2018-10-30 04:06:39 +00:00
|
|
|
|
fake_node.asset.system_users_granted = system_users
|
|
|
|
|
fake_node.key = node.key + ':0'
|
|
|
|
|
matched_assets.append(fake_node)
|
|
|
|
|
if asset_has_matched:
|
|
|
|
|
node.assets_amount = len(matched_assets)
|
|
|
|
|
queryset.append(node)
|
|
|
|
|
queryset.extend(sorted(matched_assets, key=lambda x: x.value))
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
self.change_org_if_need()
|
|
|
|
|
keyword = self.request.query_params.get('search')
|
|
|
|
|
if keyword:
|
|
|
|
|
return self.get_search_queryset(keyword)
|
|
|
|
|
else:
|
|
|
|
|
return self.get_children_queryset()
|