mirror of https://github.com/jumpserver/jumpserver
Bugfix (#3216)
* [Update] 添加 asset permission默认排序,去掉node permission model * [Update] 修复刷新节点的bugpull/3217/head^2
parent
c1d274028f
commit
9bcc325c1e
|
@ -35,7 +35,7 @@ __all__ = [
|
|||
'NodeAddAssetsApi', 'NodeRemoveAssetsApi', 'NodeReplaceAssetsApi',
|
||||
'NodeAddChildrenApi', 'RefreshNodeHardwareInfoApi',
|
||||
'TestNodeConnectiveApi', 'NodeListAsTreeApi',
|
||||
'NodeChildrenAsTreeApi', 'RefreshAssetsAmount',
|
||||
'NodeChildrenAsTreeApi', 'RefreshNodesCacheApi',
|
||||
]
|
||||
|
||||
|
||||
|
@ -282,9 +282,13 @@ class TestNodeConnectiveApi(APIView):
|
|||
return Response({"task": task.id})
|
||||
|
||||
|
||||
class RefreshAssetsAmount(APIView):
|
||||
class RefreshNodesCacheApi(APIView):
|
||||
permission_classes = (IsOrgAdmin,)
|
||||
model = Node
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
Node.refresh_nodes()
|
||||
return Response("Ok")
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
self.get(*args, **kwargs)
|
||||
return Response(status=204)
|
||||
|
|
|
@ -10,12 +10,14 @@ from django.utils.translation import ugettext_lazy as _
|
|||
from django.utils.translation import ugettext
|
||||
from django.core.cache import cache
|
||||
|
||||
from common.utils import get_logger
|
||||
from orgs.mixins.models import OrgModelMixin, OrgManager
|
||||
from orgs.utils import set_current_org, get_current_org, tmp_to_org
|
||||
from orgs.models import Organization
|
||||
|
||||
|
||||
__all__ = ['Node']
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class NodeQuerySet(models.QuerySet):
|
||||
|
@ -39,6 +41,7 @@ class TreeMixin:
|
|||
tree_updated_time > cls.tree_created_time:
|
||||
tree = TreeService.new()
|
||||
cls.tree_created_time = time.time()
|
||||
cls.refresh_tree(cls.tree_assets_created_time)
|
||||
cls.tree_assets_created_time = time.time()
|
||||
cls._tree_service = tree
|
||||
return tree
|
||||
|
@ -46,26 +49,41 @@ class TreeMixin:
|
|||
if not cls.tree_assets_created_time or \
|
||||
node_assets_updated_time > cls.tree_assets_created_time:
|
||||
cls._tree_service.init_assets_async()
|
||||
cls.tree_assets_created_time = time.time()
|
||||
cls.refresh_node_assets(cls.tree_assets_created_time)
|
||||
return cls._tree_service
|
||||
|
||||
@classmethod
|
||||
def refresh_tree(cls):
|
||||
def refresh_tree(cls, t=None):
|
||||
logger.debug("Refresh node tree")
|
||||
key = cls.tree_updated_time_cache_key
|
||||
ttl = cls.tree_cache_time
|
||||
value = time.time()
|
||||
cache.set(key, value, ttl)
|
||||
if not t:
|
||||
t = time.time()
|
||||
cache.set(key, t, ttl)
|
||||
|
||||
@classmethod
|
||||
def refresh_node_assets(cls):
|
||||
def refresh_node_assets(cls, t=None):
|
||||
logger.debug("Refresh node tree assets")
|
||||
key = cls.tree_assets_cache_key
|
||||
ttl = cls.tree_cache_time
|
||||
value = time.time()
|
||||
cache.set(key, value, ttl)
|
||||
if not t:
|
||||
t = time.time()
|
||||
cache.set(key, t, ttl)
|
||||
|
||||
@property
|
||||
def _tree(self):
|
||||
return self.__class__.tree()
|
||||
|
||||
@staticmethod
|
||||
def refresh_user_tree_cache():
|
||||
"""
|
||||
当节点-节点关系,节点-资产关系发生变化时,应该刷新用户授权树缓存
|
||||
:return:
|
||||
"""
|
||||
from perms.utils.asset_permission import AssetPermissionUtilV2
|
||||
AssetPermissionUtilV2.expire_all_user_tree_cache()
|
||||
|
||||
|
||||
class FamilyMixin:
|
||||
__parents = None
|
||||
|
@ -113,11 +131,8 @@ class FamilyMixin:
|
|||
return self.get_ancestor(with_self=False)
|
||||
|
||||
def get_ancestor(self, with_self=False):
|
||||
parents = self.parents
|
||||
if with_self:
|
||||
parents = list(parents)
|
||||
parents.append(self)
|
||||
return parents
|
||||
ancestor_keys = self.get_ancestor_keys(with_self=with_self)
|
||||
return self.__class__.objects.filter(key__in=ancestor_keys)
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
|
@ -155,9 +170,10 @@ class FamilyMixin:
|
|||
children = self.get_all_children()
|
||||
return [*tuple(ancestor), self, *tuple(children)]
|
||||
|
||||
def get_ancestor_keys(self, with_self=False):
|
||||
@classmethod
|
||||
def get_nodes_ancestor_keys_by_key(cls, key, with_self=False):
|
||||
parent_keys = []
|
||||
key_list = self.key.split(":")
|
||||
key_list = key.split(":")
|
||||
if not with_self:
|
||||
key_list.pop()
|
||||
for i in range(len(key_list)):
|
||||
|
@ -165,6 +181,11 @@ class FamilyMixin:
|
|||
key_list.pop()
|
||||
return parent_keys
|
||||
|
||||
def get_ancestor_keys(self, with_self=False):
|
||||
return self.__class__.get_nodes_ancestor_keys_by_key(
|
||||
self.key, with_self=with_self
|
||||
)
|
||||
|
||||
def is_children(self, other):
|
||||
pattern = r'^{0}:[0-9]+$'.format(self.key)
|
||||
return re.match(pattern, other.key)
|
||||
|
@ -398,24 +419,13 @@ class Node(OrgModelMixin, SomeNodesMixin, TreeMixin, FamilyMixin, FullValueMixin
|
|||
def level(self):
|
||||
return len(self.key.split(':'))
|
||||
|
||||
@staticmethod
|
||||
def refresh_user_tree_cache():
|
||||
"""
|
||||
当节点-节点关系,节点-资产关系发生变化时,应该刷新用户授权树缓存
|
||||
:return:
|
||||
"""
|
||||
from perms.utils.asset_permission import AssetPermissionUtilV2
|
||||
AssetPermissionUtilV2.expire_all_user_tree_cache()
|
||||
|
||||
@classmethod
|
||||
def refresh_nodes(cls):
|
||||
cls.refresh_tree()
|
||||
cls.refresh_user_tree_cache()
|
||||
|
||||
@classmethod
|
||||
def refresh_assets(cls):
|
||||
cls.refresh_node_assets()
|
||||
cls.refresh_user_tree_cache()
|
||||
|
||||
def as_tree_node(self):
|
||||
from common.tree import TreeNode
|
||||
|
|
|
@ -74,7 +74,6 @@ function initNodeTree(options) {
|
|||
if (options.showAssets) {
|
||||
treeUrl = setUrlParam(treeUrl, 'assets', '1')
|
||||
}
|
||||
var asyncTreeUrl = setUrlParam(treeUrl, 'refresh', '0');
|
||||
var setting = {
|
||||
view: {
|
||||
dblClickExpand: false,
|
||||
|
@ -87,7 +86,7 @@ function initNodeTree(options) {
|
|||
},
|
||||
async: {
|
||||
enable: true,
|
||||
url: asyncTreeUrl,
|
||||
url: treeUrl,
|
||||
autoParam: ["id=key", "name=n", "level=lv"],
|
||||
type: 'get'
|
||||
},
|
||||
|
@ -115,9 +114,15 @@ function initNodeTree(options) {
|
|||
$.get(treeUrl, function (data, status) {
|
||||
zTree = $.fn.zTree.init($("#nodeTree"), setting, data);
|
||||
rootNodeAddDom(zTree, function () {
|
||||
treeUrl = setUrlParam(treeUrl, 'refresh', '1');
|
||||
initNodeTree(options);
|
||||
treeUrl = setUrlParam(treeUrl, 'refresh', '0');
|
||||
const url = '{% url 'api-assets:refresh-nodes-cache' %}';
|
||||
requestApi({
|
||||
url: url,
|
||||
method: 'GET',
|
||||
flash_message: false,
|
||||
success: function () {
|
||||
initNodeTree(options);
|
||||
}
|
||||
});
|
||||
});
|
||||
inited = true;
|
||||
});
|
||||
|
|
|
@ -85,8 +85,8 @@ urlpatterns = [
|
|||
api.RefreshNodeHardwareInfoApi.as_view(), name='node-refresh-hardware-info'),
|
||||
path('nodes/<uuid:pk>/test-connective/',
|
||||
api.TestNodeConnectiveApi.as_view(), name='node-test-connective'),
|
||||
path('nodes/refresh-assets-amount/',
|
||||
api.RefreshAssetsAmount.as_view(), name='refresh-assets-amount'),
|
||||
|
||||
path('nodes/cache/', api.RefreshNodesCacheApi.as_view(), name='refresh-nodes-cache'),
|
||||
|
||||
path('gateways/<uuid:pk>/test-connective/',
|
||||
api.GatewayTestConnectionApi.as_view(), name='test-gateway-connective'),
|
||||
|
|
|
@ -70,7 +70,6 @@ class TreeService(Tree):
|
|||
@classmethod
|
||||
@timeit
|
||||
def new(cls):
|
||||
print("Call new")
|
||||
from .models import Node
|
||||
from orgs.utils import tmp_to_root_org
|
||||
|
||||
|
@ -91,17 +90,14 @@ class TreeService(Tree):
|
|||
t.start()
|
||||
|
||||
def init_assets(self):
|
||||
from orgs.utils import get_current_org, set_to_root_org
|
||||
origin_org = get_current_org()
|
||||
set_to_root_org()
|
||||
queryset = Asset.objects.all().valid().values_list('id', 'nodes__key')
|
||||
|
||||
if origin_org:
|
||||
origin_org.change_to()
|
||||
for asset_id, key in queryset:
|
||||
if not key:
|
||||
continue
|
||||
self.nodes_assets_map[key].add(asset_id)
|
||||
from orgs.utils import tmp_to_root_org
|
||||
self.all_nodes_assets_map = {}
|
||||
with tmp_to_root_org():
|
||||
queryset = Asset.objects.all().values_list('id', 'nodes__key')
|
||||
for asset_id, key in queryset:
|
||||
if not key:
|
||||
continue
|
||||
self.nodes_assets_map[key].add(asset_id)
|
||||
|
||||
def all_children(self, nid, with_self=True, deep=False):
|
||||
children_ids = self.expand_tree(nid)
|
||||
|
|
Loading…
Reference in New Issue