2017-02-06 15:13:27 +00:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
2019-08-31 04:07:03 +00:00
|
|
|
|
import pickle
|
2018-04-07 16:16:37 +00:00
|
|
|
|
from collections import defaultdict
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from functools import reduce
|
2019-03-26 11:46:04 +00:00
|
|
|
|
|
2019-08-31 04:07:03 +00:00
|
|
|
|
from django.core.cache import cache
|
2018-05-31 11:47:57 +00:00
|
|
|
|
from django.db.models import Q
|
2019-08-28 03:43:55 +00:00
|
|
|
|
from django.conf import settings
|
2016-09-16 01:38:07 +00:00
|
|
|
|
|
2019-06-21 09:09:18 +00:00
|
|
|
|
from orgs.utils import set_to_root_org
|
2019-09-19 09:27:49 +00:00
|
|
|
|
from common.utils import get_logger, timeit, lazyproperty
|
2019-02-21 09:35:28 +00:00
|
|
|
|
from common.tree import TreeNode
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from assets.utils import TreeService
|
|
|
|
|
from ..models import AssetPermission
|
2019-10-18 07:05:45 +00:00
|
|
|
|
from ..hands import Node, Asset, SystemUser, User, FavoriteAsset
|
2017-03-09 06:55:33 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2016-09-16 01:38:07 +00:00
|
|
|
|
|
|
|
|
|
|
2019-05-20 11:39:53 +00:00
|
|
|
|
__all__ = [
|
2019-08-21 12:27:21 +00:00
|
|
|
|
'is_obj_attr_has', 'sort_assets',
|
|
|
|
|
'ParserNode', 'AssetPermissionUtilV2',
|
2019-05-20 11:39:53 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
def get_user_permissions(user, include_group=True):
|
|
|
|
|
if include_group:
|
|
|
|
|
groups = user.groups.all()
|
2018-06-06 02:49:16 +00:00
|
|
|
|
arg = Q(users=user) | Q(user_groups__in=groups)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
else:
|
|
|
|
|
arg = Q(users=user)
|
2019-06-30 12:10:34 +00:00
|
|
|
|
return AssetPermission.get_queryset_with_prefetch().filter(arg)
|
2018-05-28 05:20:26 +00:00
|
|
|
|
|
2018-05-25 09:28:53 +00:00
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
def get_user_group_permissions(user_group):
|
2019-06-30 12:10:34 +00:00
|
|
|
|
return AssetPermission.get_queryset_with_prefetch().filter(
|
2018-06-01 07:34:08 +00:00
|
|
|
|
user_groups=user_group
|
|
|
|
|
)
|
2018-05-25 09:28:53 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
def get_asset_permissions(asset, include_node=True):
|
|
|
|
|
if include_node:
|
|
|
|
|
nodes = asset.get_all_nodes(flat=True)
|
2018-06-06 02:49:16 +00:00
|
|
|
|
arg = Q(assets=asset) | Q(nodes__in=nodes)
|
2018-06-01 07:34:08 +00:00
|
|
|
|
else:
|
|
|
|
|
arg = Q(assets=asset)
|
2019-06-28 14:07:22 +00:00
|
|
|
|
return AssetPermission.objects.valid().filter(arg)
|
2018-05-25 09:28:53 +00:00
|
|
|
|
|
2018-04-07 16:16:37 +00:00
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
def get_node_permissions(node):
|
2019-06-28 14:07:22 +00:00
|
|
|
|
return AssetPermission.objects.valid().filter(nodes=node)
|
2018-04-07 16:16:37 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-01 07:34:08 +00:00
|
|
|
|
def get_system_user_permissions(system_user):
|
2019-06-28 14:07:22 +00:00
|
|
|
|
return AssetPermission.objects.valid().filter(
|
2018-06-01 07:34:08 +00:00
|
|
|
|
system_users=system_user
|
|
|
|
|
)
|
2018-04-07 16:16:37 +00:00
|
|
|
|
|
|
|
|
|
|
2019-09-11 10:52:42 +00:00
|
|
|
|
class AssetPermissionUtilCacheMixin:
|
2019-09-17 04:34:47 +00:00
|
|
|
|
user_tree_cache_key = 'USER_PERM_TREE_{}_{}'
|
2019-09-11 10:52:42 +00:00
|
|
|
|
user_tree_cache_ttl = settings.ASSETS_PERM_CACHE_TIME
|
|
|
|
|
user_tree_cache_enable = settings.ASSETS_PERM_CACHE_ENABLE
|
|
|
|
|
cache_policy = '0'
|
|
|
|
|
obj_id = ''
|
2019-09-17 04:34:47 +00:00
|
|
|
|
_filter_id = 'None'
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def cache_key(self):
|
|
|
|
|
return self.user_tree_cache_key.format(self.obj_id, self._filter_id)
|
2019-09-11 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
def expire_user_tree_cache(self):
|
2019-09-17 04:34:47 +00:00
|
|
|
|
cache.delete(self.cache_key)
|
2019-09-11 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def expire_all_user_tree_cache(cls):
|
2019-09-17 04:34:47 +00:00
|
|
|
|
key = cls.user_tree_cache_key.format('*', '*')
|
|
|
|
|
key = key.split('_')[:-1]
|
|
|
|
|
key = '_'.join(key)
|
2019-09-11 10:52:42 +00:00
|
|
|
|
cache.delete_pattern(key)
|
|
|
|
|
|
|
|
|
|
def set_user_tree_to_cache(self, user_tree):
|
|
|
|
|
data = pickle.dumps(user_tree)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
cache.set(self.cache_key, data, self.user_tree_cache_ttl)
|
2019-09-11 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
def get_user_tree_from_cache(self):
|
2019-09-17 04:34:47 +00:00
|
|
|
|
data = cache.get(self.cache_key)
|
2019-09-11 10:52:42 +00:00
|
|
|
|
if not data:
|
|
|
|
|
return None
|
|
|
|
|
user_tree = pickle.loads(data)
|
|
|
|
|
return user_tree
|
|
|
|
|
|
2019-09-26 07:09:10 +00:00
|
|
|
|
@timeit
|
2019-09-11 10:52:42 +00:00
|
|
|
|
def get_user_tree_from_cache_if_need(self):
|
|
|
|
|
if not self.user_tree_cache_enable:
|
|
|
|
|
return None
|
|
|
|
|
if self.cache_policy == '1':
|
|
|
|
|
return self.get_user_tree_from_cache()
|
|
|
|
|
elif self.cache_policy == '2':
|
|
|
|
|
self.expire_user_tree_cache()
|
|
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def set_user_tree_to_cache_if_need(self, user_tree):
|
|
|
|
|
if self.cache_policy == '0':
|
|
|
|
|
return
|
|
|
|
|
if not self.user_tree_cache_enable:
|
|
|
|
|
return None
|
|
|
|
|
self.set_user_tree_to_cache(user_tree)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUtilV2(AssetPermissionUtilCacheMixin):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
get_permissions_map = {
|
|
|
|
|
"User": get_user_permissions,
|
|
|
|
|
"UserGroup": get_user_group_permissions,
|
|
|
|
|
"Asset": get_asset_permissions,
|
|
|
|
|
"Node": get_node_permissions,
|
|
|
|
|
"SystemUser": get_system_user_permissions,
|
|
|
|
|
}
|
2019-07-02 14:08:50 +00:00
|
|
|
|
assets_only = (
|
|
|
|
|
'id', 'hostname', 'ip', "platform", "domain_id",
|
|
|
|
|
'comment', 'is_active', 'os', 'org_id'
|
|
|
|
|
)
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
|
def __init__(self, obj=None, cache_policy='0'):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
self.object = obj
|
2019-08-31 04:07:03 +00:00
|
|
|
|
self.cache_policy = cache_policy
|
2019-09-18 14:06:46 +00:00
|
|
|
|
self.obj_id = str(obj.id) if obj else None
|
2019-06-27 13:43:10 +00:00
|
|
|
|
self._permissions = None
|
|
|
|
|
self._filter_id = 'None' # 当通过filter更改 permission是标记
|
|
|
|
|
self.change_org_if_need()
|
2019-08-21 12:27:21 +00:00
|
|
|
|
self._user_tree = None
|
2019-09-17 04:34:47 +00:00
|
|
|
|
self._user_tree_filter_id = 'None'
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def change_org_if_need():
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
2019-09-19 09:27:49 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def full_tree(self):
|
|
|
|
|
return Node.tree()
|
|
|
|
|
|
2019-06-27 13:43:10 +00:00
|
|
|
|
@property
|
|
|
|
|
def permissions(self):
|
|
|
|
|
if self._permissions:
|
|
|
|
|
return self._permissions
|
2019-09-18 14:06:46 +00:00
|
|
|
|
if self.object is None:
|
|
|
|
|
return AssetPermission.objects.none()
|
2019-06-27 13:43:10 +00:00
|
|
|
|
object_cls = self.object.__class__.__name__
|
|
|
|
|
func = self.get_permissions_map[object_cls]
|
|
|
|
|
permissions = func(self.object)
|
|
|
|
|
self._permissions = permissions
|
|
|
|
|
return permissions
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-06-27 13:43:10 +00:00
|
|
|
|
def filter_permissions(self, **filters):
|
2019-09-26 02:32:39 +00:00
|
|
|
|
self.cache_policy = '0'
|
|
|
|
|
# filters_json = json.dumps(filters, sort_keys=True)
|
2019-06-27 13:43:10 +00:00
|
|
|
|
self._permissions = self.permissions.filter(**filters)
|
2019-09-26 02:32:39 +00:00
|
|
|
|
# self._filter_id = md5(filters_json.encode()).hexdigest()
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-09-19 09:27:49 +00:00
|
|
|
|
@lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def user_tree(self):
|
|
|
|
|
return self.get_user_tree()
|
2019-07-02 04:08:25 +00:00
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-06-27 13:43:10 +00:00
|
|
|
|
def get_assets_direct(self):
|
|
|
|
|
"""
|
2019-07-11 10:12:14 +00:00
|
|
|
|
返回直接授权的资产,
|
|
|
|
|
并添加到tree.assets中
|
|
|
|
|
:return:
|
|
|
|
|
{asset.id: {system_user.id: actions, }, }
|
2019-06-27 13:43:10 +00:00
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
assets_ids = self.permissions.values_list('assets', flat=True)
|
|
|
|
|
return Asset.objects.filter(id__in=assets_ids)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
|
|
@timeit
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def get_nodes_direct(self):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
返回直接授权的节点,
|
|
|
|
|
并将节点添加到tree.nodes中,并将节点下的资产添加到tree.assets中
|
2019-07-11 10:12:14 +00:00
|
|
|
|
:return:
|
2019-08-21 12:27:21 +00:00
|
|
|
|
{node.key: {system_user.id: actions,}, }
|
2019-06-27 13:43:10 +00:00
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
nodes_ids = self.permissions.values_list('nodes', flat=True)
|
|
|
|
|
return Node.objects.filter(id__in=nodes_ids)
|
|
|
|
|
|
|
|
|
|
@timeit
|
|
|
|
|
def add_direct_nodes_to_user_tree(self, user_tree):
|
2019-08-28 03:43:55 +00:00
|
|
|
|
"""
|
|
|
|
|
将授权规则的节点放到用户树上, 从full tree中粘贴子树
|
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
nodes_direct_keys = self.permissions \
|
|
|
|
|
.exclude(nodes__isnull=True) \
|
|
|
|
|
.values_list('nodes__key', flat=True) \
|
|
|
|
|
.distinct()
|
|
|
|
|
nodes_direct_keys = list(nodes_direct_keys)
|
|
|
|
|
# 排序,保证从上层节点开始加
|
|
|
|
|
nodes_direct_keys.sort(key=lambda x: len(x))
|
|
|
|
|
for key in nodes_direct_keys:
|
|
|
|
|
# 如果树上已经有这个节点,代表子树已经存在
|
|
|
|
|
if user_tree.contains(key):
|
|
|
|
|
continue
|
|
|
|
|
# 找到这个节点的父节点,如果父节点不在树上,则挂到ROOT上
|
|
|
|
|
parent = self.full_tree.parent(key)
|
|
|
|
|
if not user_tree.contains(parent.identifier):
|
|
|
|
|
parent = user_tree.root_node()
|
|
|
|
|
subtree = self.full_tree.subtree(key)
|
|
|
|
|
user_tree.paste(parent.identifier, subtree, deep=True)
|
|
|
|
|
|
|
|
|
|
for node in user_tree.all_nodes_itr():
|
|
|
|
|
assets = list(self.full_tree.assets(node.identifier))
|
|
|
|
|
user_tree.set_assets(node.identifier, assets)
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def add_single_assets_node_to_user_tree(self, user_tree):
|
2019-08-28 03:43:55 +00:00
|
|
|
|
"""
|
|
|
|
|
将单独授权的资产放到树上,如果设置了单独资产到 未分组中,则放到未分组中
|
|
|
|
|
如果没有,则查询资产属于的资产组,放到树上
|
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
# 添加单独授权资产的节点
|
|
|
|
|
nodes_single_assets = defaultdict(set)
|
|
|
|
|
queryset = self.permissions.exclude(assets__isnull=True) \
|
|
|
|
|
.values_list('assets', 'assets__nodes__key') \
|
|
|
|
|
.distinct()
|
|
|
|
|
|
|
|
|
|
for item in queryset:
|
|
|
|
|
nodes_single_assets[item[1]].add(item[0])
|
|
|
|
|
nodes_single_assets.pop(None, None)
|
|
|
|
|
|
|
|
|
|
for key in tuple(nodes_single_assets.keys()):
|
|
|
|
|
if user_tree.contains(key):
|
|
|
|
|
nodes_single_assets.pop(key)
|
|
|
|
|
|
2019-10-21 09:14:49 +00:00
|
|
|
|
if not nodes_single_assets:
|
|
|
|
|
return
|
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
# 如果要设置到ungroup中
|
|
|
|
|
if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE:
|
|
|
|
|
node_key = Node.ungrouped_key
|
|
|
|
|
node_value = Node.ungrouped_value
|
|
|
|
|
user_tree.create_node(
|
|
|
|
|
identifier=node_key, tag=node_value,
|
|
|
|
|
parent=user_tree.root,
|
|
|
|
|
)
|
|
|
|
|
assets = set()
|
|
|
|
|
for _assets in nodes_single_assets.values():
|
|
|
|
|
assets.update(set(_assets))
|
|
|
|
|
user_tree.set_assets(node_key, assets)
|
|
|
|
|
return
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
# 获取单独授权资产,并没有在授权的节点上
|
|
|
|
|
for key, assets in nodes_single_assets.items():
|
|
|
|
|
node = self.full_tree.get_node(key, deep=True)
|
|
|
|
|
parent_id = self.full_tree.parent(key).identifier
|
|
|
|
|
parent = user_tree.get_node(parent_id)
|
|
|
|
|
if not parent:
|
|
|
|
|
parent = user_tree.root_node()
|
|
|
|
|
user_tree.add_node(node, parent)
|
|
|
|
|
user_tree.set_assets(node.identifier, assets)
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
@timeit
|
|
|
|
|
def parse_user_tree_to_full_tree(self, user_tree):
|
2019-08-28 03:43:55 +00:00
|
|
|
|
"""
|
|
|
|
|
经过前面两个动作,用户授权的节点已放到树上,但是树不是完整的,
|
|
|
|
|
这里要讲树构造成一个完整的书
|
|
|
|
|
"""
|
2019-08-21 12:27:21 +00:00
|
|
|
|
# 开始修正user_tree,保证父节点都在树上
|
|
|
|
|
root_children = user_tree.children('')
|
|
|
|
|
for child in root_children:
|
|
|
|
|
if child.identifier.isdigit():
|
|
|
|
|
continue
|
2019-08-28 03:43:55 +00:00
|
|
|
|
if child.identifier.startswith('-'):
|
|
|
|
|
continue
|
2019-08-21 12:27:21 +00:00
|
|
|
|
ancestors = self.full_tree.ancestors(
|
|
|
|
|
child.identifier, with_self=False, deep=True
|
|
|
|
|
)
|
|
|
|
|
if not ancestors:
|
|
|
|
|
continue
|
2019-09-26 07:09:10 +00:00
|
|
|
|
user_tree.safe_add_ancestors(child, ancestors)
|
|
|
|
|
# parent_id = ancestors[0].identifier
|
|
|
|
|
# user_tree.move_node(child.identifier, parent_id)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
2019-08-28 03:43:55 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def add_empty_node_if_need(user_tree):
|
|
|
|
|
"""
|
|
|
|
|
添加空节点,如果根节点没有子节点的话
|
|
|
|
|
"""
|
|
|
|
|
if not user_tree.children(user_tree.root):
|
|
|
|
|
node_key = Node.empty_key
|
|
|
|
|
node_value = Node.empty_value
|
|
|
|
|
user_tree.create_node(
|
|
|
|
|
identifier=node_key, tag=node_value,
|
|
|
|
|
parent=user_tree.root,
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
|
def add_favorite_node_if_need(self, user_tree):
|
|
|
|
|
if not isinstance(self.object, User):
|
|
|
|
|
return
|
|
|
|
|
node_key = Node.favorite_key
|
|
|
|
|
node_value = Node.favorite_value
|
|
|
|
|
user_tree.create_node(
|
|
|
|
|
identifier=node_key, tag=node_value,
|
|
|
|
|
parent=user_tree.root,
|
|
|
|
|
)
|
|
|
|
|
assets_id = FavoriteAsset.get_user_favorite_assets_id(self.object)
|
|
|
|
|
all_valid_assets = user_tree.all_valid_assets(user_tree.root)
|
|
|
|
|
valid_assets_id = set(assets_id) & all_valid_assets
|
|
|
|
|
user_tree.set_assets(node_key, valid_assets_id)
|
|
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
|
def set_user_tree_to_local(self, user_tree):
|
|
|
|
|
self._user_tree = user_tree
|
|
|
|
|
self._user_tree_filter_id = self._filter_id
|
|
|
|
|
|
|
|
|
|
def get_user_tree_from_local(self):
|
|
|
|
|
if self._user_tree and self._user_tree_filter_id == self._filter_id:
|
|
|
|
|
return self._user_tree
|
|
|
|
|
return None
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
@timeit
|
|
|
|
|
def get_user_tree(self):
|
2019-09-11 10:52:42 +00:00
|
|
|
|
# 使用锁,保证多次获取tree的时候顺序执行,可以使用缓存
|
2019-09-19 09:27:49 +00:00
|
|
|
|
user_tree = self.get_user_tree_from_local()
|
|
|
|
|
if user_tree:
|
|
|
|
|
return user_tree
|
|
|
|
|
user_tree = self.get_user_tree_from_cache_if_need()
|
|
|
|
|
if user_tree:
|
2019-09-17 04:34:47 +00:00
|
|
|
|
self.set_user_tree_to_local(user_tree)
|
2019-08-31 04:07:03 +00:00
|
|
|
|
return user_tree
|
2019-09-19 09:27:49 +00:00
|
|
|
|
user_tree = TreeService()
|
2019-09-26 07:09:10 +00:00
|
|
|
|
user_tree._invalid_assets = self.full_tree._invalid_assets
|
2019-09-19 09:27:49 +00:00
|
|
|
|
full_tree_root = self.full_tree.root_node()
|
|
|
|
|
user_tree.create_node(
|
|
|
|
|
tag=full_tree_root.tag,
|
|
|
|
|
identifier=full_tree_root.identifier
|
|
|
|
|
)
|
|
|
|
|
self.add_direct_nodes_to_user_tree(user_tree)
|
|
|
|
|
self.add_single_assets_node_to_user_tree(user_tree)
|
|
|
|
|
self.parse_user_tree_to_full_tree(user_tree)
|
2019-10-18 07:05:45 +00:00
|
|
|
|
self.add_favorite_node_if_need(user_tree)
|
2019-10-21 09:14:49 +00:00
|
|
|
|
self.add_empty_node_if_need(user_tree)
|
2019-09-19 09:27:49 +00:00
|
|
|
|
self.set_user_tree_to_cache_if_need(user_tree)
|
|
|
|
|
self.set_user_tree_to_local(user_tree)
|
|
|
|
|
return user_tree
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
# Todo: 是否可以获取多个资产的系统用户
|
|
|
|
|
def get_asset_system_users_with_actions(self, asset):
|
|
|
|
|
nodes = asset.get_nodes()
|
|
|
|
|
nodes_keys_related = set()
|
|
|
|
|
for node in nodes:
|
|
|
|
|
ancestor_keys = node.get_ancestor_keys(with_self=True)
|
|
|
|
|
nodes_keys_related.update(set(ancestor_keys))
|
|
|
|
|
kwargs = {"assets": asset}
|
|
|
|
|
|
2019-09-19 14:12:53 +00:00
|
|
|
|
if nodes_keys_related:
|
|
|
|
|
kwargs["nodes__key__in"] = nodes_keys_related
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
queryset = self.permissions
|
2019-09-19 14:12:53 +00:00
|
|
|
|
if kwargs == 1:
|
2019-08-21 12:27:21 +00:00
|
|
|
|
queryset = queryset.filter(**kwargs)
|
|
|
|
|
elif len(kwargs) > 1:
|
|
|
|
|
kwargs = [{k: v} for k, v in kwargs.items()]
|
|
|
|
|
args = [Q(**kw) for kw in kwargs]
|
|
|
|
|
args = reduce(lambda x, y: x | y, args)
|
|
|
|
|
queryset = queryset.filter(args)
|
|
|
|
|
else:
|
|
|
|
|
queryset = queryset.none()
|
|
|
|
|
queryset = queryset.distinct().prefetch_related('system_users')
|
|
|
|
|
system_users_actions = defaultdict(int)
|
|
|
|
|
for perm in queryset:
|
|
|
|
|
system_users = perm.system_users.all()
|
|
|
|
|
if not system_users or not perm.actions:
|
|
|
|
|
continue
|
|
|
|
|
for s in system_users:
|
|
|
|
|
if not asset.has_protocol(s.protocol):
|
|
|
|
|
continue
|
|
|
|
|
system_users_actions[s] |= perm.actions
|
|
|
|
|
return system_users_actions
|
|
|
|
|
|
|
|
|
|
def get_permissions_nodes_and_assets(self):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
from assets.models import Node
|
2019-08-21 12:27:21 +00:00
|
|
|
|
permissions = self.permissions.values_list('assets', 'nodes__key').distinct()
|
|
|
|
|
nodes_keys = set()
|
|
|
|
|
assets_ids = set()
|
|
|
|
|
for asset_id, node_key in permissions:
|
|
|
|
|
if asset_id:
|
|
|
|
|
assets_ids.add(asset_id)
|
|
|
|
|
if node_key:
|
|
|
|
|
nodes_keys.add(node_key)
|
2019-08-23 10:23:07 +00:00
|
|
|
|
nodes_keys = Node.clean_children_keys(nodes_keys)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
return nodes_keys, assets_ids
|
|
|
|
|
|
|
|
|
|
@timeit
|
|
|
|
|
def get_assets(self):
|
|
|
|
|
nodes_keys, assets_ids = self.get_permissions_nodes_and_assets()
|
2019-09-19 14:12:53 +00:00
|
|
|
|
queryset = Node.get_nodes_all_assets(nodes_keys, extra_assets_ids=assets_ids)
|
|
|
|
|
return queryset.valid()
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
def get_nodes_assets(self, node, deep=False):
|
|
|
|
|
if deep:
|
|
|
|
|
assets_ids = self.user_tree.all_assets(node.key)
|
|
|
|
|
else:
|
|
|
|
|
assets_ids = self.user_tree.assets(node.key)
|
|
|
|
|
queryset = Asset.objects.filter(id__in=assets_ids)
|
2019-09-19 14:12:53 +00:00
|
|
|
|
return queryset.valid()
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
|
return [n.identifier for n in self.user_tree.all_nodes_itr()]
|
|
|
|
|
|
|
|
|
|
def get_system_users(self):
|
|
|
|
|
system_users_id = self.permissions.values_list('system_users', flat=True).distinct()
|
|
|
|
|
return SystemUser.objects.filter(id__in=system_users_id)
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
2018-10-31 07:31:09 +00:00
|
|
|
|
def is_obj_attr_has(obj, val, attrs=("hostname", "ip", "comment")):
|
|
|
|
|
if not attrs:
|
|
|
|
|
vals = [val for val in obj.__dict__.values() if isinstance(val, (str, int))]
|
|
|
|
|
else:
|
|
|
|
|
vals = [getattr(obj, attr) for attr in attrs if
|
|
|
|
|
hasattr(obj, attr) and isinstance(hasattr(obj, attr), (str, int))]
|
|
|
|
|
|
|
|
|
|
for v in vals:
|
|
|
|
|
if str(v).find(val) != -1:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sort_assets(assets, order_by='hostname', reverse=False):
|
|
|
|
|
if order_by == 'ip':
|
|
|
|
|
assets = sorted(assets, key=lambda asset: [int(d) for d in asset.ip.split('.') if d.isdigit()], reverse=reverse)
|
|
|
|
|
else:
|
|
|
|
|
assets = sorted(assets, key=lambda asset: getattr(asset, order_by), reverse=reverse)
|
|
|
|
|
return assets
|
2019-02-21 09:35:28 +00:00
|
|
|
|
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
class ParserNode:
|
|
|
|
|
nodes_only_fields = ("key", "value", "id")
|
|
|
|
|
assets_only_fields = ("platform", "hostname", "id", "ip", "protocols")
|
|
|
|
|
system_users_only_fields = (
|
|
|
|
|
"id", "name", "username", "protocol", "priority", "login_mode",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_node_to_tree_node(node):
|
|
|
|
|
name = '{} ({})'.format(node.value, node.assets_amount)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
# name = node.value
|
2019-07-11 10:12:14 +00:00
|
|
|
|
data = {
|
|
|
|
|
'id': node.key,
|
|
|
|
|
'name': name,
|
|
|
|
|
'title': name,
|
|
|
|
|
'pId': node.parent_key,
|
|
|
|
|
'isParent': True,
|
2019-08-23 10:23:07 +00:00
|
|
|
|
'open': node.is_org_root(),
|
2019-07-11 10:12:14 +00:00
|
|
|
|
'meta': {
|
|
|
|
|
'node': {
|
|
|
|
|
"id": node.id,
|
|
|
|
|
"key": node.key,
|
|
|
|
|
"value": node.value,
|
|
|
|
|
},
|
|
|
|
|
'type': 'node'
|
|
|
|
|
}
|
2019-02-21 09:35:28 +00:00
|
|
|
|
}
|
2019-07-11 10:12:14 +00:00
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
|
return tree_node
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def parse_asset_to_tree_node(node, asset):
|
2019-07-11 10:12:14 +00:00
|
|
|
|
icon_skin = 'file'
|
2019-08-31 04:07:03 +00:00
|
|
|
|
platform = asset.platform.lower()
|
|
|
|
|
if platform == 'windows':
|
2019-07-11 10:12:14 +00:00
|
|
|
|
icon_skin = 'windows'
|
2019-08-31 04:07:03 +00:00
|
|
|
|
elif platform == 'linux':
|
2019-07-11 10:12:14 +00:00
|
|
|
|
icon_skin = 'linux'
|
2019-08-31 04:07:03 +00:00
|
|
|
|
parent_id = node.key if node else ''
|
2019-07-11 10:12:14 +00:00
|
|
|
|
data = {
|
|
|
|
|
'id': str(asset.id),
|
|
|
|
|
'name': asset.hostname,
|
|
|
|
|
'title': asset.ip,
|
2019-08-31 04:07:03 +00:00
|
|
|
|
'pId': parent_id,
|
2019-07-11 10:12:14 +00:00
|
|
|
|
'isParent': False,
|
|
|
|
|
'open': False,
|
|
|
|
|
'iconSkin': icon_skin,
|
|
|
|
|
'meta': {
|
|
|
|
|
'type': 'asset',
|
|
|
|
|
'asset': {
|
|
|
|
|
'id': asset.id,
|
|
|
|
|
'hostname': asset.hostname,
|
|
|
|
|
'ip': asset.ip,
|
|
|
|
|
'protocols': asset.protocols_as_list,
|
|
|
|
|
'platform': asset.platform,
|
|
|
|
|
},
|
|
|
|
|
}
|
2019-02-21 09:35:28 +00:00
|
|
|
|
}
|
2019-07-11 10:12:14 +00:00
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
|
return tree_node
|