mirror of https://github.com/jumpserver/jumpserver
[Update] 修改用户serializer
parent
b92137afd9
commit
6bb13a26f5
|
@ -1,5 +1,6 @@
|
||||||
# ~*~ coding: utf-8 ~*~
|
# ~*~ coding: utf-8 ~*~
|
||||||
#
|
#
|
||||||
|
import time
|
||||||
from treelib import Tree
|
from treelib import Tree
|
||||||
from treelib.exceptions import NodeIDAbsentError
|
from treelib.exceptions import NodeIDAbsentError
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
@ -77,8 +78,15 @@ class TreeService(Tree):
|
||||||
return ancestor_ids
|
return ancestor_ids
|
||||||
|
|
||||||
def ancestors(self, nid, with_self=False, deep=False, with_assets=True):
|
def ancestors(self, nid, with_self=False, deep=False, with_assets=True):
|
||||||
|
# now = time.time()
|
||||||
|
# print("Start get ancestor_ids")
|
||||||
ancestor_ids = self.ancestors_ids(nid, with_self=with_self)
|
ancestor_ids = self.ancestors_ids(nid, with_self=with_self)
|
||||||
|
# now2 = time.time()
|
||||||
|
# interval = (now2 - now) * 1000
|
||||||
|
# print("Start get ancestor_ids using: {}".format(interval))
|
||||||
ancestors = [self.get_node(i, deep=deep) for i in ancestor_ids]
|
ancestors = [self.get_node(i, deep=deep) for i in ancestor_ids]
|
||||||
|
# interval = (time.time() - now2) * 1000
|
||||||
|
# print("Get ancestors done: {}".format(interval))
|
||||||
if with_assets:
|
if with_assets:
|
||||||
return ancestors
|
return ancestors
|
||||||
for n in ancestors:
|
for n in ancestors:
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#
|
#
|
||||||
from common.utils import lazyproperty
|
from common.utils import lazyproperty
|
||||||
from common.tree import TreeNodeSerializer
|
from common.tree import TreeNodeSerializer
|
||||||
|
from django.db.models import QuerySet
|
||||||
from ..mixin import UserPermissionMixin
|
from ..mixin import UserPermissionMixin
|
||||||
from ...utils import AssetPermissionUtil, ParserNode
|
from ...utils import AssetPermissionUtil, ParserNode
|
||||||
from ...hands import Node, Asset
|
from ...hands import Node, Asset
|
||||||
|
@ -32,7 +33,8 @@ class UserNodeTreeMixin:
|
||||||
nodes_only_fields = ParserNode.nodes_only_fields
|
nodes_only_fields = ParserNode.nodes_only_fields
|
||||||
|
|
||||||
def parse_nodes_to_queryset(self, nodes):
|
def parse_nodes_to_queryset(self, nodes):
|
||||||
nodes = nodes.only(*self.nodes_only_fields)
|
if isinstance(nodes, QuerySet):
|
||||||
|
nodes = nodes.only(*self.nodes_only_fields)
|
||||||
_queryset = []
|
_queryset = []
|
||||||
|
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
|
|
|
@ -290,11 +290,12 @@ class AssetPermissionUtil(AssetPermissionUtilCacheMixin):
|
||||||
def parse_user_tree_to_full_tree(self, user_tree):
|
def parse_user_tree_to_full_tree(self, user_tree):
|
||||||
"""
|
"""
|
||||||
经过前面两个动作,用户授权的节点已放到树上,但是树不是完整的,
|
经过前面两个动作,用户授权的节点已放到树上,但是树不是完整的,
|
||||||
这里要讲树构造成一个完整的树
|
这里要将树构造成一个完整的树
|
||||||
"""
|
"""
|
||||||
# 开始修正user_tree,保证父节点都在树上
|
# 开始修正user_tree,保证父节点都在树上
|
||||||
root_children = user_tree.children('')
|
root_children = user_tree.children('')
|
||||||
for child in root_children:
|
for child in root_children:
|
||||||
|
# print("child: {}".format(child.identifier))
|
||||||
if child.identifier.isdigit():
|
if child.identifier.isdigit():
|
||||||
continue
|
continue
|
||||||
if child.identifier.startswith('-'):
|
if child.identifier.startswith('-'):
|
||||||
|
@ -303,6 +304,7 @@ class AssetPermissionUtil(AssetPermissionUtilCacheMixin):
|
||||||
child.identifier, with_self=False, deep=True,
|
child.identifier, with_self=False, deep=True,
|
||||||
with_assets=False,
|
with_assets=False,
|
||||||
)
|
)
|
||||||
|
# print("Get ancestors: {}".format(len(ancestors)))
|
||||||
if not ancestors:
|
if not ancestors:
|
||||||
continue
|
continue
|
||||||
user_tree.safe_add_ancestors(child, ancestors)
|
user_tree.safe_add_ancestors(child, ancestors)
|
||||||
|
|
|
@ -40,8 +40,9 @@ class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
||||||
self.set_fields_queryset()
|
self.set_fields_queryset()
|
||||||
|
|
||||||
def set_fields_queryset(self):
|
def set_fields_queryset(self):
|
||||||
users_field = self.fields['users']
|
users_field = self.fields.get('users')
|
||||||
users_field.child_relation.queryset = utils.get_current_org_members(exclude=('Auditor',))
|
if users_field:
|
||||||
|
users_field.child_relation.queryset = utils.get_current_org_members(exclude=('Auditor',))
|
||||||
|
|
||||||
def validate_users(self, users):
|
def validate_users(self, users):
|
||||||
for user in users:
|
for user in users:
|
||||||
|
|
|
@ -33,6 +33,7 @@ class UserSerializer(CommonSerializerMixin, serializers.ModelSerializer):
|
||||||
choices=PASSWORD_STRATEGY_CHOICES, required=False, initial=0,
|
choices=PASSWORD_STRATEGY_CHOICES, required=False, initial=0,
|
||||||
label=_('Password strategy'), write_only=True
|
label=_('Password strategy'), write_only=True
|
||||||
)
|
)
|
||||||
|
mfa_level_display = serializers.ReadOnlyField(source='get_mfa_level_display')
|
||||||
can_update = serializers.SerializerMethodField()
|
can_update = serializers.SerializerMethodField()
|
||||||
can_delete = serializers.SerializerMethodField()
|
can_delete = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
@ -44,10 +45,11 @@ class UserSerializer(CommonSerializerMixin, serializers.ModelSerializer):
|
||||||
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
||||||
fields_small = fields_mini + [
|
fields_small = fields_mini + [
|
||||||
'password', 'email', 'public_key', 'wechat', 'phone', 'mfa_level',
|
'password', 'email', 'public_key', 'wechat', 'phone', 'mfa_level',
|
||||||
|
'mfa_level_display',
|
||||||
'comment', 'source', 'is_valid', 'is_expired',
|
'comment', 'source', 'is_valid', 'is_expired',
|
||||||
'is_active', 'created_by', 'is_first_login',
|
'is_active', 'created_by', 'is_first_login',
|
||||||
'password_strategy', 'date_password_last_updated', 'date_expired',
|
'password_strategy', 'date_password_last_updated', 'date_expired',
|
||||||
'avatar_url', 'source_display',
|
'avatar_url', 'source_display', 'date_joined', 'last_login'
|
||||||
]
|
]
|
||||||
fields = fields_small + [
|
fields = fields_small + [
|
||||||
'groups', 'role', 'groups_display', 'role_display',
|
'groups', 'role', 'groups_display', 'role_display',
|
||||||
|
|
Loading…
Reference in New Issue