Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
9bcc325c
Unverified
Commit
9bcc325c
authored
Sep 11, 2019
by
老广
Committed by
GitHub
Sep 11, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bugfix (#3216)
* [Update] 添加 asset permission默认排序,去掉node permission model * [Update] 修复刷新节点的bug
parent
c1d27402
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
41 deletions
+56
-41
node.py
apps/assets/api/node.py
+7
-3
node.py
apps/assets/models/node.py
+34
-24
_node_tree.html
apps/assets/templates/assets/_node_tree.html
+9
-4
api_urls.py
apps/assets/urls/api_urls.py
+2
-2
utils.py
apps/assets/utils.py
+4
-8
No files found.
apps/assets/api/node.py
View file @
9bcc325c
...
...
@@ -35,7 +35,7 @@ __all__ = [
'NodeAddAssetsApi'
,
'NodeRemoveAssetsApi'
,
'NodeReplaceAssetsApi'
,
'NodeAddChildrenApi'
,
'RefreshNodeHardwareInfoApi'
,
'TestNodeConnectiveApi'
,
'NodeListAsTreeApi'
,
'NodeChildrenAsTreeApi'
,
'Refresh
AssetsAmount
'
,
'NodeChildrenAsTreeApi'
,
'Refresh
NodesCacheApi
'
,
]
...
...
@@ -282,9 +282,13 @@ class TestNodeConnectiveApi(APIView):
return
Response
({
"task"
:
task
.
id
})
class
Refresh
AssetsAmount
(
APIView
):
class
Refresh
NodesCacheApi
(
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
)
apps/assets/models/node.py
View file @
9bcc325c
...
...
@@ -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
...
...
apps/assets/templates/assets/_node_tree.html
View file @
9bcc325c
...
...
@@ -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
:
asyncT
reeUrl
,
url
:
t
reeUrl
,
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'
);
const
url
=
'{% url '
api
-
assets
:
refresh
-
nodes
-
cache
' %}'
;
requestApi
({
url
:
url
,
method
:
'GET'
,
flash_message
:
false
,
success
:
function
()
{
initNodeTree
(
options
);
treeUrl
=
setUrlParam
(
treeUrl
,
'refresh'
,
'0'
);
}
});
});
inited
=
true
;
});
...
...
apps/assets/urls/api_urls.py
View file @
9bcc325c
...
...
@@ -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'
),
...
...
apps/assets/utils.py
View file @
9bcc325c
...
...
@@ -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,13 +90,10 @@ 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
()
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment