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
f8384973
Commit
f8384973
authored
May 23, 2018
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 调整Luna的树形结构
parent
dc1d228e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
7 deletions
+44
-7
node.py
apps/assets/models/node.py
+6
-2
api.py
apps/perms/api.py
+7
-2
utils.py
apps/perms/utils.py
+31
-3
No files found.
apps/assets/models/node.py
View file @
f8384973
...
@@ -61,10 +61,14 @@ class Node(models.Model):
...
@@ -61,10 +61,14 @@ class Node(models.Model):
return
child
return
child
def
get_children
(
self
):
def
get_children
(
self
):
return
self
.
__class__
.
objects
.
filter
(
key__regex
=
r'^{}:[0-9]+$'
.
format
(
self
.
key
))
return
self
.
__class__
.
objects
.
filter
(
key__regex
=
r'^{}:[0-9]+$'
.
format
(
self
.
key
)
)
def
get_all_children
(
self
):
def
get_all_children
(
self
):
return
self
.
__class__
.
objects
.
filter
(
key__startswith
=
'{}:'
.
format
(
self
.
key
))
return
self
.
__class__
.
objects
.
filter
(
key__startswith
=
'{}:'
.
format
(
self
.
key
)
)
def
get_family
(
self
):
def
get_family
(
self
):
children
=
list
(
self
.
get_all_children
())
children
=
list
(
self
.
get_all_children
())
...
...
apps/perms/api.py
View file @
f8384973
...
@@ -6,7 +6,7 @@ from rest_framework.views import APIView, Response
...
@@ -6,7 +6,7 @@ from rest_framework.views import APIView, Response
from
rest_framework.generics
import
ListAPIView
,
get_object_or_404
,
RetrieveUpdateAPIView
from
rest_framework.generics
import
ListAPIView
,
get_object_or_404
,
RetrieveUpdateAPIView
from
rest_framework
import
viewsets
from
rest_framework
import
viewsets
from
common.utils
import
set_or_append_attr_bulk
from
common.utils
import
set_or_append_attr_bulk
,
get_object_or_none
from
users.permissions
import
IsValidUser
,
IsSuperUser
,
IsSuperUserOrAppUser
from
users.permissions
import
IsValidUser
,
IsSuperUser
,
IsSuperUserOrAppUser
from
.utils
import
AssetPermissionUtil
from
.utils
import
AssetPermissionUtil
from
.models
import
AssetPermission
from
.models
import
AssetPermission
...
@@ -147,8 +147,13 @@ class UserGrantedNodeAssetsApi(ListAPIView):
...
@@ -147,8 +147,13 @@ class UserGrantedNodeAssetsApi(ListAPIView):
user
=
get_object_or_404
(
User
,
id
=
user_id
)
user
=
get_object_or_404
(
User
,
id
=
user_id
)
else
:
else
:
user
=
self
.
request
.
user
user
=
self
.
request
.
user
node
=
get_object_or_404
(
Node
,
id
=
node_id
)
nodes
=
AssetPermissionUtil
.
get_user_nodes_with_assets
(
user
)
nodes
=
AssetPermissionUtil
.
get_user_nodes_with_assets
(
user
)
node
=
get_object_or_none
(
Node
,
id
=
node_id
)
if
not
node
:
unnode
=
[
node
for
node
in
nodes
if
node
.
name
==
'Unnode'
]
node
=
unnode
[
0
]
if
unnode
else
None
assets
=
nodes
.
get
(
node
,
[])
assets
=
nodes
.
get
(
node
,
[])
for
asset
,
system_users
in
assets
.
items
():
for
asset
,
system_users
in
assets
.
items
():
asset
.
system_users_granted
=
system_users
asset
.
system_users_granted
=
system_users
...
...
apps/perms/utils.py
View file @
f8384973
...
@@ -13,7 +13,6 @@ logger = get_logger(__file__)
...
@@ -13,7 +13,6 @@ logger = get_logger(__file__)
class
AssetPermissionUtil
:
class
AssetPermissionUtil
:
@staticmethod
@staticmethod
def
get_user_permissions
(
user
):
def
get_user_permissions
(
user
):
return
AssetPermission
.
objects
.
all
()
.
valid
()
.
filter
(
users
=
user
)
return
AssetPermission
.
objects
.
all
()
.
valid
()
.
filter
(
users
=
user
)
...
@@ -122,6 +121,24 @@ class AssetPermissionUtil:
...
@@ -122,6 +121,24 @@ class AssetPermissionUtil:
nodes
[
node
]
.
update
(
set
(
_system_users
))
nodes
[
node
]
.
update
(
set
(
_system_users
))
return
nodes
return
nodes
@classmethod
def
get_user_nodes_inherit_group
(
cls
,
user
):
nodes
=
defaultdict
(
set
)
groups
=
user
.
groups
.
all
()
for
group
in
groups
:
_nodes
=
cls
.
get_user_group_nodes
(
group
)
for
node
,
system_users
in
_nodes
.
items
():
nodes
[
node
]
.
update
(
set
(
system_users
))
return
nodes
@classmethod
def
get_user_nodes
(
cls
,
user
):
nodes
=
cls
.
get_user_nodes_direct
(
user
)
nodes_inherit
=
cls
.
get_user_nodes_inherit_group
(
user
)
for
node
,
system_users
in
nodes_inherit
.
items
():
nodes
[
node
]
.
update
(
set
(
system_users
))
return
nodes
@classmethod
@classmethod
def
get_user_nodes_assets_direct
(
cls
,
user
):
def
get_user_nodes_assets_direct
(
cls
,
user
):
assets
=
defaultdict
(
set
)
assets
=
defaultdict
(
set
)
...
@@ -164,15 +181,26 @@ class AssetPermissionUtil:
...
@@ -164,15 +181,26 @@ class AssetPermissionUtil:
:param user:
:param user:
:return: {node: {asset: set(su1, su2)}}
:return: {node: {asset: set(su1, su2)}}
"""
"""
from
assets.models
import
Node
unnode
=
Node
(
value
=
'Unnode'
)
nodes
=
defaultdict
(
dict
)
nodes
=
defaultdict
(
dict
)
for
_node
in
cls
.
get_user_nodes
(
user
):
children
=
_node
.
get_family
()
for
node
in
children
:
nodes
[
node
]
=
defaultdict
(
set
)
_assets
=
cls
.
get_user_assets
(
user
)
_assets
=
cls
.
get_user_assets
(
user
)
for
asset
,
_system_users
in
_assets
.
items
():
for
asset
,
_system_users
in
_assets
.
items
():
_nodes
=
asset
.
get_nodes
()
_nodes
=
asset
.
get_nodes
()
in_node
=
False
for
node
in
_nodes
:
for
node
in
_nodes
:
if
asset
in
nodes
[
node
]:
if
node
in
nodes
:
in_node
=
True
nodes
[
node
][
asset
]
.
update
(
_system_users
)
nodes
[
node
][
asset
]
.
update
(
_system_users
)
if
not
in_node
:
if
unnode
in
nodes
:
nodes
[
unnode
][
asset
]
.
update
(
_system_users
)
else
:
else
:
nodes
[
node
][
asset
]
=
_system_users
nodes
[
un
node
][
asset
]
=
_system_users
return
nodes
return
nodes
@classmethod
@classmethod
...
...
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