mirror of https://github.com/jumpserver/jumpserver
[Update] 修改permistion utils优化返回的属性
parent
fc2a44621b
commit
0b4df78393
|
@ -117,7 +117,8 @@ class Asset(models.Model):
|
||||||
|
|
||||||
def get_nodes(self):
|
def get_nodes(self):
|
||||||
from .node import Node
|
from .node import Node
|
||||||
return self.nodes.all() or [Node.root()]
|
nodes = self.nodes.all() or [Node.root()]
|
||||||
|
return nodes
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hardware_info(self):
|
def hardware_info(self):
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,7 +88,12 @@ class Node(models.Model):
|
||||||
|
|
||||||
def get_assets(self):
|
def get_assets(self):
|
||||||
from .asset import Asset
|
from .asset import Asset
|
||||||
assets = Asset.objects.filter(nodes__id=self.id)
|
if self.is_root():
|
||||||
|
assets = Asset.objects.filter(
|
||||||
|
Q(nodes__id=self.id) | Q(nodes__isnull=True)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
assets = Asset.objects.filter(nodes__id=self.id)
|
||||||
return assets
|
return assets
|
||||||
|
|
||||||
def get_valid_assets(self):
|
def get_valid_assets(self):
|
||||||
|
|
|
@ -61,13 +61,13 @@ class AssetGrantedSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
|
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
|
||||||
system_users_join = serializers.SerializerMethodField()
|
system_users_join = serializers.SerializerMethodField()
|
||||||
nodes = NodeTMPSerializer(many=True, read_only=True)
|
# nodes = NodeTMPSerializer(many=True, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Asset
|
model = Asset
|
||||||
fields = (
|
fields = (
|
||||||
"id", "hostname", "ip", "port", "system_users_granted",
|
"id", "hostname", "ip", "port", "system_users_granted",
|
||||||
"is_active", "system_users_join", "os", 'domain', "nodes",
|
"is_active", "system_users_join", "os", 'domain',
|
||||||
"platform", "comment"
|
"platform", "comment"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,8 @@ class AssetPermission(models.Model):
|
||||||
nodes = models.ManyToManyField('assets.Node', related_name='granted_by_permissions', blank=True, verbose_name=_("Nodes"))
|
nodes = models.ManyToManyField('assets.Node', related_name='granted_by_permissions', blank=True, verbose_name=_("Nodes"))
|
||||||
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_permissions', verbose_name=_("System user"))
|
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_permissions', verbose_name=_("System user"))
|
||||||
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
||||||
date_start = models.DateTimeField(default=timezone.now, verbose_name=_("Date start"))
|
date_start = models.DateTimeField(default=timezone.now, db_index=True, verbose_name=_("Date start"))
|
||||||
date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired'))
|
date_expired = models.DateTimeField(default=date_expired_default, db_index=True, verbose_name=_('Date expired'))
|
||||||
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
|
created_by = models.CharField(max_length=128, blank=True, verbose_name=_('Created by'))
|
||||||
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
||||||
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
||||||
|
|
|
@ -16,8 +16,22 @@ logger = get_logger(__file__)
|
||||||
class Tree:
|
class Tree:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__all_nodes = list(Node.objects.all())
|
self.__all_nodes = list(Node.objects.all())
|
||||||
|
self.__node_asset_map = defaultdict(set)
|
||||||
self.nodes = defaultdict(dict)
|
self.nodes = defaultdict(dict)
|
||||||
self.root = Node.root()
|
self.root = Node.root()
|
||||||
|
self.init_node_asset_map()
|
||||||
|
|
||||||
|
def init_node_asset_map(self):
|
||||||
|
for node in self.__all_nodes:
|
||||||
|
assets = node.get_assets().values_list('id', flat=True)
|
||||||
|
for asset in assets:
|
||||||
|
self.__node_asset_map[str(asset)].add(node)
|
||||||
|
|
||||||
|
def add_asset(self, asset, system_users):
|
||||||
|
nodes = self.__node_asset_map.get(str(asset.id), [])
|
||||||
|
self.add_nodes(nodes)
|
||||||
|
for node in nodes:
|
||||||
|
self.nodes[node][asset].update(system_users)
|
||||||
|
|
||||||
def add_node(self, node):
|
def add_node(self, node):
|
||||||
if node in self.nodes:
|
if node in self.nodes:
|
||||||
|
@ -44,11 +58,15 @@ class AssetPermissionUtil:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_group_permissions(user_group):
|
def get_user_group_permissions(user_group):
|
||||||
return AssetPermission.objects.all().valid().filter(user_groups=user_group)
|
return AssetPermission.objects.all().valid().filter(
|
||||||
|
user_groups=user_group
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_asset_permissions(asset):
|
def get_asset_permissions(asset):
|
||||||
return AssetPermission.objects.all().valid().filter(assets=asset)
|
return AssetPermission.objects.all().valid().filter(
|
||||||
|
assets=asset
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_node_permissions(node):
|
def get_node_permissions(node):
|
||||||
|
@ -56,7 +74,9 @@ class AssetPermissionUtil:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_system_user_permissions(system_user):
|
def get_system_user_permissions(system_user):
|
||||||
return AssetPermission.objects.valid().all().filter(system_users=system_user)
|
return AssetPermission.objects.valid().all().filter(
|
||||||
|
system_users=system_user
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_user_group_nodes(cls, group):
|
def get_user_group_nodes(cls, group):
|
||||||
|
@ -209,11 +229,11 @@ class AssetPermissionUtil:
|
||||||
tree = Tree()
|
tree = Tree()
|
||||||
_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()
|
tree.add_asset(asset, _system_users)
|
||||||
tree.add_nodes(_nodes)
|
# _nodes = asset.get_nodes()
|
||||||
|
# tree.add_nodes(_nodes)
|
||||||
for node in _nodes:
|
# for node in _nodes:
|
||||||
tree.nodes[node][asset].update(_system_users)
|
# tree.nodes[node][asset].update(_system_users)
|
||||||
return tree.nodes
|
return tree.nodes
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -77,7 +77,7 @@ class User(AbstractUser):
|
||||||
is_first_login = models.BooleanField(default=True)
|
is_first_login = models.BooleanField(default=True)
|
||||||
date_expired = models.DateTimeField(
|
date_expired = models.DateTimeField(
|
||||||
default=date_expired_default, blank=True, null=True,
|
default=date_expired_default, blank=True, null=True,
|
||||||
verbose_name=_('Date expired')
|
db_index=True, verbose_name=_('Date expired')
|
||||||
)
|
)
|
||||||
created_by = models.CharField(
|
created_by = models.CharField(
|
||||||
max_length=30, default='', verbose_name=_('Created by')
|
max_length=30, default='', verbose_name=_('Created by')
|
||||||
|
|
Loading…
Reference in New Issue