mirror of https://github.com/jumpserver/jumpserver
Merge branch 'large' into dev
commit
ffabef0040
|
@ -117,7 +117,8 @@ class Asset(models.Model):
|
|||
|
||||
def get_nodes(self):
|
||||
from .node import Node
|
||||
return self.nodes.all() or [Node.root()]
|
||||
nodes = self.nodes.all() or [Node.root()]
|
||||
return nodes
|
||||
|
||||
@property
|
||||
def hardware_info(self):
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models, transaction
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
|
@ -87,7 +88,12 @@ class Node(models.Model):
|
|||
|
||||
def get_assets(self):
|
||||
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
|
||||
|
||||
def get_valid_assets(self):
|
||||
|
|
|
@ -61,13 +61,13 @@ class AssetGrantedSerializer(serializers.ModelSerializer):
|
|||
"""
|
||||
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
|
||||
system_users_join = serializers.SerializerMethodField()
|
||||
nodes = NodeTMPSerializer(many=True, read_only=True)
|
||||
# nodes = NodeTMPSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Asset
|
||||
fields = (
|
||||
"id", "hostname", "ip", "port", "system_users_granted",
|
||||
"is_active", "system_users_join", "os", 'domain', "nodes",
|
||||
"is_active", "system_users_join", "os", 'domain',
|
||||
"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"))
|
||||
system_users = models.ManyToManyField('assets.SystemUser', related_name='granted_by_permissions', verbose_name=_("System user"))
|
||||
is_active = models.BooleanField(default=True, verbose_name=_('Active'))
|
||||
date_start = models.DateTimeField(default=timezone.now, verbose_name=_("Date start"))
|
||||
date_expired = models.DateTimeField(default=date_expired_default, verbose_name=_('Date expired'))
|
||||
date_start = models.DateTimeField(default=timezone.now, db_index=True, verbose_name=_("Date start"))
|
||||
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'))
|
||||
date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Date created'))
|
||||
comment = models.TextField(verbose_name=_('Comment'), blank=True)
|
||||
|
|
|
@ -16,8 +16,22 @@ logger = get_logger(__file__)
|
|||
class Tree:
|
||||
def __init__(self):
|
||||
self.__all_nodes = list(Node.objects.all())
|
||||
self.__node_asset_map = defaultdict(set)
|
||||
self.nodes = defaultdict(dict)
|
||||
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):
|
||||
if node in self.nodes:
|
||||
|
@ -44,11 +58,15 @@ class AssetPermissionUtil:
|
|||
|
||||
@staticmethod
|
||||
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
|
||||
def get_asset_permissions(asset):
|
||||
return AssetPermission.objects.all().valid().filter(assets=asset)
|
||||
return AssetPermission.objects.all().valid().filter(
|
||||
assets=asset
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_node_permissions(node):
|
||||
|
@ -56,7 +74,9 @@ class AssetPermissionUtil:
|
|||
|
||||
@staticmethod
|
||||
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
|
||||
def get_user_group_nodes(cls, group):
|
||||
|
@ -207,11 +227,11 @@ class AssetPermissionUtil:
|
|||
tree = Tree()
|
||||
_assets = cls.get_user_assets(user)
|
||||
for asset, _system_users in _assets.items():
|
||||
_nodes = asset.get_nodes()
|
||||
tree.add_nodes(_nodes)
|
||||
|
||||
for node in _nodes:
|
||||
tree.nodes[node][asset].update(_system_users)
|
||||
tree.add_asset(asset, _system_users)
|
||||
# _nodes = asset.get_nodes()
|
||||
# tree.add_nodes(_nodes)
|
||||
# for node in _nodes:
|
||||
# tree.nodes[node][asset].update(_system_users)
|
||||
return tree.nodes
|
||||
|
||||
@classmethod
|
||||
|
|
|
@ -77,7 +77,7 @@ class User(AbstractUser):
|
|||
is_first_login = models.BooleanField(default=True)
|
||||
date_expired = models.DateTimeField(
|
||||
default=date_expired_default, blank=True, null=True,
|
||||
verbose_name=_('Date expired')
|
||||
db_index=True, verbose_name=_('Date expired')
|
||||
)
|
||||
created_by = models.CharField(
|
||||
max_length=30, default='', verbose_name=_('Created by')
|
||||
|
|
Loading…
Reference in New Issue