2017-02-06 15:13:27 +00:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
import time
|
2019-03-05 11:47:14 +00:00
|
|
|
|
import uuid
|
2018-04-07 16:16:37 +00:00
|
|
|
|
from collections import defaultdict
|
2019-03-26 11:46:04 +00:00
|
|
|
|
import json
|
|
|
|
|
from hashlib import md5
|
2019-06-28 14:07:22 +00:00
|
|
|
|
import itertools
|
2019-03-26 11:46:04 +00:00
|
|
|
|
|
2019-03-05 11:47:14 +00:00
|
|
|
|
from django.utils import timezone
|
2018-05-31 11:47:57 +00:00
|
|
|
|
from django.db.models import Q
|
2019-03-04 12:45:57 +00:00
|
|
|
|
from django.core.cache import cache
|
|
|
|
|
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-07-11 10:12:14 +00:00
|
|
|
|
from common.utils import get_logger, timeit
|
2019-02-21 09:35:28 +00:00
|
|
|
|
from common.tree import TreeNode
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from assets.utils import NodeUtil
|
2019-06-12 09:04:49 +00:00
|
|
|
|
from .. import const
|
2019-07-02 08:45:26 +00:00
|
|
|
|
from ..models import AssetPermission, Action
|
2019-07-01 10:22:40 +00:00
|
|
|
|
from ..hands import Node, Asset
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from .stack import PermSystemUserNodeUtil, PermAssetsAmountUtil
|
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__ = [
|
|
|
|
|
'AssetPermissionUtil', 'is_obj_attr_has', 'sort_assets',
|
2019-07-11 10:12:14 +00:00
|
|
|
|
'ParserNode',
|
2019-05-20 11:39:53 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
|
class GenerateTree:
|
2018-06-06 03:13:13 +00:00
|
|
|
|
def __init__(self):
|
2018-12-10 02:11:54 +00:00
|
|
|
|
"""
|
2019-07-01 10:22:40 +00:00
|
|
|
|
nodes = {
|
2019-07-11 10:12:14 +00:00
|
|
|
|
node.key: {
|
2019-07-01 10:22:40 +00:00
|
|
|
|
"system_users": {
|
2019-07-11 10:12:14 +00:00
|
|
|
|
system_user.id: actions,
|
2019-07-01 10:22:40 +00:00
|
|
|
|
},
|
2019-07-11 10:12:14 +00:00
|
|
|
|
"assets": set([asset.id,]),
|
|
|
|
|
},
|
2019-07-01 10:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
assets = {
|
2019-07-11 10:12:14 +00:00
|
|
|
|
asset.id: {
|
|
|
|
|
system_user.id: actions,
|
2019-07-01 10:22:40 +00:00
|
|
|
|
},
|
2018-12-10 02:11:54 +00:00
|
|
|
|
}
|
|
|
|
|
"""
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self._node_util = None
|
2019-07-11 10:12:14 +00:00
|
|
|
|
self.nodes = defaultdict(lambda: {
|
|
|
|
|
"system_users": defaultdict(int), "assets": set(),
|
|
|
|
|
"assets_amount": 0, "all_assets": set(),
|
|
|
|
|
})
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self.assets = defaultdict(lambda: defaultdict(int))
|
2019-06-12 09:04:49 +00:00
|
|
|
|
self._root_node = None
|
|
|
|
|
self._ungroup_node = None
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self._nodes_with_assets = None
|
2019-07-11 10:12:14 +00:00
|
|
|
|
self._all_assets_nodes_key = None
|
|
|
|
|
self._asset_counter = 0
|
|
|
|
|
self._system_user_counter = 0
|
|
|
|
|
self._nodes_assets_counter = 0
|
2019-07-01 10:22:40 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def node_util(self):
|
|
|
|
|
if not self._node_util:
|
|
|
|
|
self._node_util = NodeUtil()
|
|
|
|
|
return self._node_util
|
2019-06-12 09:04:49 +00:00
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def key_sort(key):
|
|
|
|
|
key_list = [int(i) for i in key.split(':')]
|
|
|
|
|
return len(key_list), key_list
|
|
|
|
|
|
2019-06-12 09:04:49 +00:00
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
|
def root_key(self):
|
2019-06-12 09:04:49 +00:00
|
|
|
|
if self._root_node:
|
|
|
|
|
return self._root_node
|
2019-07-11 10:12:14 +00:00
|
|
|
|
all_keys = self.nodes.keys()
|
2019-06-12 09:04:49 +00:00
|
|
|
|
# 如果没有授权节点,就放到默认的根节点下
|
2019-07-11 10:12:14 +00:00
|
|
|
|
if not all_keys:
|
2019-06-27 13:43:10 +00:00
|
|
|
|
return None
|
2019-07-11 10:12:14 +00:00
|
|
|
|
root_key = min(all_keys, key=self.key_sort)
|
|
|
|
|
self._root_key = root_key
|
|
|
|
|
return root_key
|
2019-06-12 09:04:49 +00:00
|
|
|
|
|
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
|
def all_assets_nodes_keys(self):
|
|
|
|
|
if not self._all_assets_nodes_key:
|
|
|
|
|
self._all_assets_nodes_key = Asset.get_all_nodes_keys()
|
|
|
|
|
return self._all_assets_nodes_key
|
2018-06-06 03:13:13 +00:00
|
|
|
|
|
2019-07-03 08:33:21 +00:00
|
|
|
|
@property
|
2019-07-11 10:12:14 +00:00
|
|
|
|
def ungrouped_key(self):
|
|
|
|
|
if self._ungroup_node:
|
|
|
|
|
return self._ungroup_node
|
|
|
|
|
if self.root_key:
|
|
|
|
|
node_key = "{}:{}".format(self.root_key, '-1')
|
2019-07-01 10:22:40 +00:00
|
|
|
|
else:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
node_key = '1:-1'
|
|
|
|
|
self._ungroup_node = node_key
|
|
|
|
|
return node_key
|
|
|
|
|
|
|
|
|
|
@timeit
|
|
|
|
|
def add_assets_without_system_users(self, assets_ids):
|
|
|
|
|
for asset_id in assets_ids:
|
|
|
|
|
self.add_asset(asset_id, {})
|
|
|
|
|
|
|
|
|
|
@timeit
|
|
|
|
|
def add_assets(self, assets_ids_with_system_users):
|
|
|
|
|
for asset_id, system_users_ids in assets_ids_with_system_users.items():
|
|
|
|
|
self.add_asset(asset_id, system_users_ids)
|
|
|
|
|
|
|
|
|
|
# @timeit
|
|
|
|
|
def add_asset(self, asset_id, system_users_ids=None):
|
|
|
|
|
"""
|
|
|
|
|
:param asset_id:
|
|
|
|
|
:param system_users_ids: {system_user.id: actions, }
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if not system_users_ids:
|
|
|
|
|
system_users_ids = defaultdict(int)
|
|
|
|
|
|
|
|
|
|
# 获取已有资产的系统用户和actions,并更新到最新系统用户信息中
|
|
|
|
|
old_system_users_ids = self.assets[asset_id]
|
|
|
|
|
for system_user_id, action in old_system_users_ids.items():
|
|
|
|
|
system_users_ids[system_user_id] |= action
|
|
|
|
|
|
|
|
|
|
asset_nodes_keys = self.all_assets_nodes_keys.get(asset_id, [])
|
|
|
|
|
# {asset.id: [node.key, ], }
|
|
|
|
|
# 获取用户在的节点
|
|
|
|
|
in_nodes = set(self.nodes.keys()) & set(asset_nodes_keys)
|
2019-07-01 10:22:40 +00:00
|
|
|
|
if not in_nodes:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
self.nodes[self.ungrouped_key]["assets"].add(asset_id)
|
|
|
|
|
self.assets[asset_id] = system_users_ids
|
2019-07-01 10:22:40 +00:00
|
|
|
|
return
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
# 遍历用户应该在的节点
|
|
|
|
|
for key in in_nodes:
|
|
|
|
|
# 把自己加入到树上的节点中
|
|
|
|
|
self.nodes[key]["assets"].add(asset_id)
|
|
|
|
|
# 获取自己所在节点的系统用户,并添加进去
|
|
|
|
|
node_system_users_ids = self.nodes[key]["system_users"]
|
|
|
|
|
for system_user_id, action in node_system_users_ids.items():
|
|
|
|
|
system_users_ids[system_user_id] |= action
|
|
|
|
|
self.assets[asset_id] = system_users_ids
|
|
|
|
|
|
|
|
|
|
def add_node(self, node_key, system_users_ids=None):
|
|
|
|
|
"""
|
|
|
|
|
:param node_key: node.key
|
|
|
|
|
:param system_users_ids: {system_user.id: actions,}
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
if not system_users_ids:
|
|
|
|
|
system_users_ids = defaultdict(int)
|
|
|
|
|
self.nodes[node_key]["system_users"] = system_users_ids
|
2018-06-06 03:13:13 +00:00
|
|
|
|
|
2019-06-11 05:30:45 +00:00
|
|
|
|
# 添加树节点
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
|
|
|
|
def add_nodes(self, nodes_keys_with_system_users_ids):
|
|
|
|
|
"""
|
|
|
|
|
:param nodes_keys_with_system_users_ids:
|
|
|
|
|
{node.key: {system_user.id: actions,}, }
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
util = PermSystemUserNodeUtil()
|
|
|
|
|
family = util.get_nodes_family_and_system_users(nodes_keys_with_system_users_ids)
|
|
|
|
|
for key, system_users in family.items():
|
|
|
|
|
self.add_node(key, system_users)
|
2019-07-01 10:22:40 +00:00
|
|
|
|
|
|
|
|
|
def get_assets(self):
|
2019-07-11 10:12:14 +00:00
|
|
|
|
"""
|
|
|
|
|
:return:
|
|
|
|
|
[
|
|
|
|
|
{"id": asset.id, "system_users": {system_user.id: actions, }},
|
|
|
|
|
]
|
|
|
|
|
"""
|
|
|
|
|
assets = []
|
|
|
|
|
for asset_id, system_users in self.assets.items():
|
|
|
|
|
assets.append({"id": asset_id, "system_users": system_users})
|
|
|
|
|
return assets
|
2019-07-01 10:22:40 +00:00
|
|
|
|
|
2019-08-08 05:03:03 +00:00
|
|
|
|
def set_ungrouped_assets_nodes_if_need(self):
|
|
|
|
|
if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE:
|
|
|
|
|
return
|
|
|
|
|
ungrouped_assets_ids = self.nodes[self.ungrouped_key]["assets"]
|
|
|
|
|
for asset_id in ungrouped_assets_ids:
|
|
|
|
|
in_nodes = self.all_assets_nodes_keys.get(asset_id, [])
|
|
|
|
|
for node_key in in_nodes:
|
|
|
|
|
parents_keys = self.node_util.get_nodes_parents_keys_by_key(node_key, with_self=False)
|
|
|
|
|
for parent_key in parents_keys:
|
|
|
|
|
n = self.nodes[parent_key]
|
|
|
|
|
self.nodes[node_key]["assets"].add(asset_id)
|
|
|
|
|
self.nodes.pop(self.ungrouped_key, None)
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-07-01 10:22:40 +00:00
|
|
|
|
def get_nodes_with_assets(self):
|
2019-07-11 10:12:14 +00:00
|
|
|
|
"""
|
|
|
|
|
:return:
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
'key': node.key,
|
|
|
|
|
'assets_amount': 10
|
|
|
|
|
'assets': {
|
|
|
|
|
asset.id: {
|
|
|
|
|
system_user.id: actions,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
"""
|
2019-07-01 10:22:40 +00:00
|
|
|
|
if self._nodes_with_assets:
|
|
|
|
|
return self._nodes_with_assets
|
2019-08-08 05:03:03 +00:00
|
|
|
|
self.set_ungrouped_assets_nodes_if_need()
|
2019-07-11 10:12:14 +00:00
|
|
|
|
util = PermAssetsAmountUtil()
|
|
|
|
|
nodes_with_assets_amount = util.compute_nodes_assets_amount(self.nodes)
|
|
|
|
|
nodes = []
|
|
|
|
|
for key, values in nodes_with_assets_amount.items():
|
|
|
|
|
assets = {asset_id: self.assets.get(asset_id) for asset_id in values["assets"]}
|
2019-07-22 03:12:38 +00:00
|
|
|
|
all_assets = {asset_id: self.assets.get(asset_id) for asset_id in values["all_assets"]}
|
2019-07-11 10:12:14 +00:00
|
|
|
|
nodes.append({
|
2019-07-22 03:12:38 +00:00
|
|
|
|
"key": key, "assets": assets, "all_assets": all_assets,
|
2019-07-11 10:12:14 +00:00
|
|
|
|
"assets_amount": values["assets_amount"]
|
|
|
|
|
})
|
2019-07-03 08:33:21 +00:00
|
|
|
|
# 如果返回空节点,页面构造授权资产树报错
|
|
|
|
|
if not nodes:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
nodes.append({
|
2019-07-22 03:12:38 +00:00
|
|
|
|
"key": const.EMPTY_NODE_KEY, "assets": {}, "assets_amount": 0,
|
|
|
|
|
"all_assets": {},
|
2019-07-11 10:12:14 +00:00
|
|
|
|
})
|
|
|
|
|
nodes.sort(key=lambda n: self.key_sort(n["key"]))
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self._nodes_with_assets = nodes
|
2019-07-11 10:12:14 +00:00
|
|
|
|
return nodes
|
2018-06-06 03:13:13 +00:00
|
|
|
|
|
2019-07-02 04:08:25 +00:00
|
|
|
|
def get_nodes(self):
|
2019-08-08 05:03:03 +00:00
|
|
|
|
self.set_ungrouped_assets_nodes_if_need()
|
2019-07-11 10:12:14 +00:00
|
|
|
|
nodes = list(self.nodes.keys())
|
|
|
|
|
if not nodes:
|
|
|
|
|
nodes.append(const.EMPTY_NODE_KEY)
|
|
|
|
|
return list(nodes)
|
2019-07-02 04:08:25 +00:00
|
|
|
|
|
2018-06-06 03:13:13 +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-06-27 13:43:10 +00:00
|
|
|
|
class AssetPermissionCacheMixin:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
CACHE_KEY_PREFIX = '_ASSET_PERM_CACHE_V2_'
|
|
|
|
|
CACHE_META_KEY_PREFIX = '_ASSET_PERM_META_KEY_V2_'
|
2019-03-04 12:45:57 +00:00
|
|
|
|
CACHE_TIME = settings.ASSETS_PERM_CACHE_TIME
|
|
|
|
|
CACHE_POLICY_MAP = (('0', 'never'), ('1', 'using'), ('2', 'refresh'))
|
2019-07-01 13:48:25 +00:00
|
|
|
|
cache_policy = '1'
|
2019-07-02 06:17:56 +00:00
|
|
|
|
obj_id = ''
|
|
|
|
|
_filter_id = None
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2019-03-26 11:46:04 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def is_not_using_cache(cls, cache_policy):
|
|
|
|
|
return cls.CACHE_TIME == 0 or cache_policy in cls.CACHE_POLICY_MAP[0]
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def is_using_cache(cls, cache_policy):
|
|
|
|
|
return cls.CACHE_TIME != 0 and cache_policy in cls.CACHE_POLICY_MAP[1]
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def is_refresh_cache(cls, cache_policy):
|
|
|
|
|
return cache_policy in cls.CACHE_POLICY_MAP[2]
|
|
|
|
|
|
|
|
|
|
def _is_not_using_cache(self):
|
|
|
|
|
return self.is_not_using_cache(self.cache_policy)
|
|
|
|
|
|
|
|
|
|
def _is_using_cache(self):
|
|
|
|
|
return self.is_using_cache(self.cache_policy)
|
|
|
|
|
|
|
|
|
|
def _is_refresh_cache(self):
|
|
|
|
|
return self.is_refresh_cache(self.cache_policy)
|
2018-04-07 16:16:37 +00:00
|
|
|
|
|
2019-07-01 13:48:25 +00:00
|
|
|
|
#@timeit
|
2019-03-26 11:46:04 +00:00
|
|
|
|
def get_cache_key(self, resource):
|
2019-03-27 03:12:34 +00:00
|
|
|
|
cache_key = self.CACHE_KEY_PREFIX + '{obj_id}_{filter_id}_{resource}'
|
|
|
|
|
return cache_key.format(
|
2019-03-26 11:46:04 +00:00
|
|
|
|
obj_id=self.obj_id, filter_id=self._filter_id,
|
|
|
|
|
resource=resource
|
|
|
|
|
)
|
|
|
|
|
|
2019-07-01 13:48:25 +00:00
|
|
|
|
@property
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def node_asset_key(self):
|
2019-03-26 11:46:04 +00:00
|
|
|
|
return self.get_cache_key('NODES_WITH_ASSETS')
|
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
@property
|
|
|
|
|
def node_key(self):
|
|
|
|
|
return self.get_cache_key('NODES')
|
|
|
|
|
|
2019-07-01 13:48:25 +00:00
|
|
|
|
@property
|
2019-03-26 11:46:04 +00:00
|
|
|
|
def asset_key(self):
|
2019-07-01 13:48:25 +00:00
|
|
|
|
key = self.get_cache_key('ASSETS')
|
|
|
|
|
return key
|
2019-03-26 11:46:04 +00:00
|
|
|
|
|
2019-07-01 13:48:25 +00:00
|
|
|
|
@property
|
2019-03-26 11:46:04 +00:00
|
|
|
|
def system_key(self):
|
|
|
|
|
return self.get_cache_key('SYSTEM_USER')
|
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def get_resource_from_cache(self, resource):
|
2019-07-11 10:12:14 +00:00
|
|
|
|
logger.debug("Try get resource from cache")
|
2019-07-02 14:08:50 +00:00
|
|
|
|
key_map = {
|
|
|
|
|
"assets": self.asset_key,
|
|
|
|
|
"nodes": self.node_key,
|
|
|
|
|
"nodes_with_assets": self.node_asset_key,
|
|
|
|
|
"system_users": self.system_key
|
|
|
|
|
}
|
|
|
|
|
key = key_map.get(resource)
|
|
|
|
|
if not key:
|
|
|
|
|
raise ValueError("Not a valid resource: {}".format(resource))
|
|
|
|
|
cached = cache.get(key)
|
2019-03-05 11:47:14 +00:00
|
|
|
|
if not cached:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
logger.debug("Not found resource cache, update it")
|
2019-03-05 11:47:14 +00:00
|
|
|
|
self.update_cache()
|
2019-07-02 14:08:50 +00:00
|
|
|
|
cached = cache.get(key)
|
2019-03-05 11:47:14 +00:00
|
|
|
|
return cached
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def get_resource(self, resource):
|
2019-03-26 11:46:04 +00:00
|
|
|
|
if self._is_using_cache():
|
2019-07-11 10:12:14 +00:00
|
|
|
|
logger.debug("Using cache to get resource")
|
2019-07-02 14:08:50 +00:00
|
|
|
|
return self.get_resource_from_cache(resource)
|
2019-03-26 11:46:04 +00:00
|
|
|
|
elif self._is_refresh_cache():
|
2019-07-11 10:12:14 +00:00
|
|
|
|
logger.debug("Need refresh cache")
|
2019-03-04 12:45:57 +00:00
|
|
|
|
self.expire_cache()
|
2019-07-02 14:08:50 +00:00
|
|
|
|
data = self.get_resource_from_cache(resource)
|
|
|
|
|
return data
|
2019-03-04 12:45:57 +00:00
|
|
|
|
else:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
logger.debug("Not using cache get source")
|
2019-07-02 14:08:50 +00:00
|
|
|
|
return self.get_resource_without_cache(resource)
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def get_resource_without_cache(self, resource):
|
|
|
|
|
attr = 'get_{}_without_cache'.format(resource)
|
|
|
|
|
return getattr(self, attr)()
|
|
|
|
|
|
|
|
|
|
def get_nodes_with_assets(self):
|
|
|
|
|
return self.get_resource("nodes_with_assets")
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2019-07-01 10:22:40 +00:00
|
|
|
|
def get_assets(self):
|
2019-07-02 14:08:50 +00:00
|
|
|
|
return self.get_resource("assets")
|
|
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
|
return self.get_resource("nodes")
|
2019-07-01 10:22:40 +00:00
|
|
|
|
|
2019-03-04 12:45:57 +00:00
|
|
|
|
def get_system_users(self):
|
2019-07-02 14:08:50 +00:00
|
|
|
|
return self.get_resource("system_users")
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2019-03-26 11:46:04 +00:00
|
|
|
|
def get_meta_cache_key(self):
|
2019-03-27 03:12:34 +00:00
|
|
|
|
cache_key = self.CACHE_META_KEY_PREFIX + '{obj_id}_{filter_id}'
|
|
|
|
|
key = cache_key.format(
|
2019-07-02 06:17:56 +00:00
|
|
|
|
obj_id=self.obj_id, filter_id=self._filter_id
|
2019-03-26 11:46:04 +00:00
|
|
|
|
)
|
|
|
|
|
return key
|
|
|
|
|
|
2019-03-05 11:47:14 +00:00
|
|
|
|
@property
|
|
|
|
|
def cache_meta(self):
|
2019-03-26 11:46:04 +00:00
|
|
|
|
key = self.get_meta_cache_key()
|
2019-07-01 13:48:25 +00:00
|
|
|
|
meta = cache.get(key) or {}
|
|
|
|
|
# print("Meta key: {}".format(key))
|
|
|
|
|
# print("Meta id: {}".format(meta["id"]))
|
|
|
|
|
return meta
|
2019-03-05 11:47:14 +00:00
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def update_cache(self):
|
|
|
|
|
assets = self.get_resource_without_cache("assets")
|
|
|
|
|
nodes_with_assets = self.get_resource_without_cache("nodes_with_assets")
|
|
|
|
|
system_users = self.get_resource_without_cache("system_users")
|
|
|
|
|
nodes = self.get_resource_without_cache("nodes")
|
|
|
|
|
cache.set(self.asset_key, assets, self.CACHE_TIME)
|
|
|
|
|
cache.set(self.node_asset_key, nodes_with_assets, self.CACHE_TIME)
|
|
|
|
|
cache.set(self.system_key, system_users, self.CACHE_TIME)
|
|
|
|
|
cache.set(self.node_key, nodes, self.CACHE_TIME)
|
|
|
|
|
self.set_meta_to_cache()
|
|
|
|
|
|
2019-03-26 11:46:04 +00:00
|
|
|
|
def set_meta_to_cache(self):
|
|
|
|
|
key = self.get_meta_cache_key()
|
2019-03-05 11:47:14 +00:00
|
|
|
|
meta = {
|
|
|
|
|
'id': str(uuid.uuid4()),
|
|
|
|
|
'datetime': timezone.now(),
|
|
|
|
|
'object': str(self.object)
|
|
|
|
|
}
|
2019-07-01 13:48:25 +00:00
|
|
|
|
# print("Set meta key: {}".format(key))
|
|
|
|
|
# print("set meta to cache: {}".format(meta["id"]))
|
2019-03-05 11:47:14 +00:00
|
|
|
|
cache.set(key, meta, self.CACHE_TIME)
|
|
|
|
|
|
|
|
|
|
def expire_cache_meta(self):
|
2019-03-27 03:12:34 +00:00
|
|
|
|
cache_key = self.CACHE_META_KEY_PREFIX + '{obj_id}_*'
|
2019-07-02 06:17:56 +00:00
|
|
|
|
key = cache_key.format(obj_id=self.obj_id)
|
2019-03-27 03:12:34 +00:00
|
|
|
|
cache.delete_pattern(key)
|
2019-03-05 11:47:14 +00:00
|
|
|
|
|
2019-03-04 12:45:57 +00:00
|
|
|
|
def expire_cache(self):
|
2019-03-05 11:47:14 +00:00
|
|
|
|
"""
|
|
|
|
|
因为 获取用户的节点,资产,系统用户等都能会缓存,这里会清理所有与该对象有关的
|
|
|
|
|
缓存,以免造成不统一的情况
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2019-03-27 03:12:34 +00:00
|
|
|
|
cache_key = self.CACHE_KEY_PREFIX + '{obj_id}_*'
|
|
|
|
|
key = cache_key.format(obj_id='*')
|
2019-03-05 11:47:14 +00:00
|
|
|
|
cache.delete_pattern(key)
|
|
|
|
|
self.expire_cache_meta()
|
|
|
|
|
|
2019-03-04 12:45:57 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def expire_all_cache(cls):
|
2019-03-27 03:12:34 +00:00
|
|
|
|
key = cls.CACHE_KEY_PREFIX + '*'
|
2019-03-05 11:47:14 +00:00
|
|
|
|
cache.delete_pattern(key)
|
2019-07-24 09:45:38 +00:00
|
|
|
|
meta_key = cls.CACHE_META_KEY_PREFIX + '*'
|
|
|
|
|
cache.delete_pattern(meta_key)
|
2019-03-04 12:45:57 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2019-06-27 13:43:10 +00:00
|
|
|
|
class AssetPermissionUtil(AssetPermissionCacheMixin):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
def __init__(self, obj, cache_policy='0'):
|
|
|
|
|
self.object = obj
|
|
|
|
|
self.obj_id = str(obj.id)
|
|
|
|
|
self._permissions = None
|
|
|
|
|
self._permissions_id = None # 标记_permission的唯一值
|
|
|
|
|
self._assets = None
|
|
|
|
|
self._filter_id = 'None' # 当通过filter更改 permission是标记
|
|
|
|
|
self.cache_policy = cache_policy
|
|
|
|
|
self.tree = GenerateTree()
|
|
|
|
|
self.change_org_if_need()
|
|
|
|
|
self.nodes = None
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self._nodes = None
|
2019-07-02 14:08:50 +00:00
|
|
|
|
self._assets_direct = None
|
|
|
|
|
self._nodes_direct = None
|
2019-07-30 09:46:27 +00:00
|
|
|
|
self.node_util = NodeUtil()
|
|
|
|
|
self.tree._node_util = self.node_util
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def change_org_if_need():
|
|
|
|
|
set_to_root_org()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def permissions(self):
|
|
|
|
|
if self._permissions:
|
|
|
|
|
return self._permissions
|
|
|
|
|
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):
|
|
|
|
|
filters_json = json.dumps(filters, sort_keys=True)
|
|
|
|
|
self._permissions = self.permissions.filter(**filters)
|
|
|
|
|
self._filter_id = md5(filters_json.encode()).hexdigest()
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-06-27 13:43:10 +00:00
|
|
|
|
def get_nodes_direct(self):
|
|
|
|
|
"""
|
2019-07-11 10:12:14 +00:00
|
|
|
|
返回直接授权的节点,
|
|
|
|
|
并将节点添加到tree.nodes中,并将节点下的资产添加到tree.assets中
|
|
|
|
|
:return:
|
|
|
|
|
{node.key: {system_user.id: actions,}, }
|
2019-06-27 13:43:10 +00:00
|
|
|
|
"""
|
2019-07-02 14:08:50 +00:00
|
|
|
|
if self._nodes_direct:
|
|
|
|
|
return self._nodes_direct
|
2019-07-11 10:12:14 +00:00
|
|
|
|
nodes_keys = defaultdict(lambda: defaultdict(int))
|
2019-06-30 12:10:34 +00:00
|
|
|
|
for perm in self.permissions:
|
2019-07-01 10:22:40 +00:00
|
|
|
|
actions = [perm.actions]
|
2019-07-11 10:12:14 +00:00
|
|
|
|
system_users_ids = [s.id for s in perm.system_users.all()]
|
|
|
|
|
_nodes_keys = [n.key for n in perm.nodes.all()]
|
|
|
|
|
iterable = itertools.product(_nodes_keys, system_users_ids, actions)
|
|
|
|
|
for node_key, sys_id, action in iterable:
|
|
|
|
|
nodes_keys[node_key][sys_id] |= action
|
|
|
|
|
|
|
|
|
|
self.tree.add_nodes(nodes_keys)
|
|
|
|
|
|
2019-07-30 09:46:27 +00:00
|
|
|
|
all_nodes_keys = set()
|
2019-07-11 10:12:14 +00:00
|
|
|
|
for key in nodes_keys:
|
2019-07-30 09:46:27 +00:00
|
|
|
|
children_keys = self.node_util.get_all_children_keys_by_key(key)
|
|
|
|
|
all_nodes_keys.update(set(children_keys))
|
|
|
|
|
|
|
|
|
|
if all_nodes_keys:
|
2019-07-11 10:12:14 +00:00
|
|
|
|
assets_ids = Asset.objects.filter(
|
2019-07-30 09:46:27 +00:00
|
|
|
|
nodes__key__in=all_nodes_keys
|
2019-07-17 04:10:14 +00:00
|
|
|
|
).valid().values_list("id", flat=True).distinct()
|
2019-07-11 10:12:14 +00:00
|
|
|
|
else:
|
|
|
|
|
assets_ids = []
|
|
|
|
|
self.tree.add_assets_without_system_users(assets_ids)
|
|
|
|
|
self._nodes_direct = nodes_keys
|
|
|
|
|
return nodes_keys
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def get_nodes_without_cache(self):
|
2019-07-11 10:12:14 +00:00
|
|
|
|
self.get_assets_without_cache()
|
2019-07-02 04:08:25 +00:00
|
|
|
|
return self.tree.get_nodes()
|
|
|
|
|
|
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-07-02 14:08:50 +00:00
|
|
|
|
if self._assets_direct:
|
|
|
|
|
return self._assets_direct
|
2019-07-11 10:12:14 +00:00
|
|
|
|
assets_ids = defaultdict(lambda: defaultdict(int))
|
2019-06-30 12:10:34 +00:00
|
|
|
|
for perm in self.permissions:
|
2019-07-01 10:22:40 +00:00
|
|
|
|
actions = [perm.actions]
|
2019-07-17 04:10:14 +00:00
|
|
|
|
_assets_ids = perm.assets.valid().values_list("id", flat=True)
|
|
|
|
|
system_users_ids = perm.system_users.values_list("id", flat=True)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
iterable = itertools.product(_assets_ids, system_users_ids, actions)
|
|
|
|
|
for asset_id, sys_id, action in iterable:
|
|
|
|
|
assets_ids[asset_id][sys_id] |= action
|
|
|
|
|
self.tree.add_assets(assets_ids)
|
|
|
|
|
self._assets_direct = assets_ids
|
|
|
|
|
return assets_ids
|
|
|
|
|
|
|
|
|
|
@timeit
|
2019-06-27 13:43:10 +00:00
|
|
|
|
def get_assets_without_cache(self):
|
|
|
|
|
"""
|
2019-07-11 10:12:14 +00:00
|
|
|
|
:return:
|
|
|
|
|
[
|
|
|
|
|
{"id": asset.id, "system_users": {system_user.id: actions, }},
|
|
|
|
|
]
|
2019-06-27 13:43:10 +00:00
|
|
|
|
"""
|
|
|
|
|
if self._assets:
|
|
|
|
|
return self._assets
|
2019-07-11 10:12:14 +00:00
|
|
|
|
self.get_nodes_direct()
|
2019-07-02 14:08:50 +00:00
|
|
|
|
self.get_assets_direct()
|
2019-07-01 10:22:40 +00:00
|
|
|
|
assets = self.tree.get_assets()
|
|
|
|
|
self._assets = assets
|
|
|
|
|
return assets
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
@timeit
|
2019-06-27 13:43:10 +00:00
|
|
|
|
def get_nodes_with_assets_without_cache(self):
|
2019-07-01 10:22:40 +00:00
|
|
|
|
self.get_assets_without_cache()
|
2019-07-02 14:08:50 +00:00
|
|
|
|
nodes_assets = self.tree.get_nodes_with_assets()
|
|
|
|
|
return nodes_assets
|
2019-06-27 13:43:10 +00:00
|
|
|
|
|
2019-07-02 14:08:50 +00:00
|
|
|
|
def get_system_users_without_cache(self):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
system_users = set()
|
|
|
|
|
permissions = self.permissions.prefetch_related('system_users')
|
|
|
|
|
for perm in permissions:
|
|
|
|
|
system_users.update(perm.system_users.all())
|
|
|
|
|
return system_users
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
data = {
|
|
|
|
|
'id': node.key,
|
|
|
|
|
'name': name,
|
|
|
|
|
'title': name,
|
|
|
|
|
'pId': node.parent_key,
|
|
|
|
|
'isParent': True,
|
|
|
|
|
'open': node.is_root(),
|
|
|
|
|
'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
|
|
|
|
|
def parse_asset_to_tree_node(node, asset, system_users):
|
|
|
|
|
icon_skin = 'file'
|
|
|
|
|
if asset.platform.lower() == 'windows':
|
|
|
|
|
icon_skin = 'windows'
|
|
|
|
|
elif asset.platform.lower() == 'linux':
|
|
|
|
|
icon_skin = 'linux'
|
|
|
|
|
_system_users = []
|
|
|
|
|
for system_user in system_users:
|
|
|
|
|
_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,
|
|
|
|
|
'actions': [Action.value_to_choices(system_user.actions)],
|
|
|
|
|
})
|
|
|
|
|
data = {
|
|
|
|
|
'id': str(asset.id),
|
|
|
|
|
'name': asset.hostname,
|
|
|
|
|
'title': asset.ip,
|
|
|
|
|
'pId': node.key,
|
|
|
|
|
'isParent': False,
|
|
|
|
|
'open': False,
|
|
|
|
|
'iconSkin': icon_skin,
|
|
|
|
|
'meta': {
|
|
|
|
|
'system_users': _system_users,
|
|
|
|
|
'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
|