[Update] 系统用户资产管理页面获取相关的所有资产 (#3038)

* [Update] 系统用户资产管理页面获取相关的所有资产

* [Update] 系统用户资产管理页面获取相关的所有资产2
pull/3045/head
BaiJiangJie 2019-07-26 16:16:06 +08:00 committed by GitHub
parent 28f0302e4d
commit 3cdabaf883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 15 deletions

View File

@ -81,7 +81,7 @@ class AssetUserViewSet(IDInCacheFilterMixin, BulkModelViewSet):
manager = AssetUserManager()
if system_user_id:
system_user = get_object_or_404(SystemUser, id=system_user_id)
assets = system_user.assets.all()
assets = system_user.get_all_assets()
username = system_user.username
elif admin_user_id:
admin_user = get_object_or_404(AdminUser, id=admin_user_id)

View File

@ -12,7 +12,6 @@ from django.core.cache import cache
from django.db import models
from django.utils.translation import ugettext_lazy as _
from .user import AdminUser, SystemUser
from .utils import Connectivity
from orgs.mixins import OrgModelMixin, OrgManager
@ -320,6 +319,7 @@ class Asset(ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
@classmethod
def generate_fake(cls, count=100):
from .user import AdminUser, SystemUser
from random import seed, choice
from django.db import IntegrityError
from .node import Node

View File

@ -4,12 +4,15 @@
import logging
from functools import reduce
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator, MaxValueValidator
from common.utils import get_signer
from .base import AssetUser
from .asset import Asset
__all__ = ['AdminUser', 'SystemUser']
@ -144,6 +147,19 @@ class SystemUser(AssetUser):
return False, matched_cmd
return True, None
def get_all_assets(self):
args = [Q(systemuser=self)]
pattern = set()
nodes_keys = self.nodes.all().values_list('key', flat=True)
for key in nodes_keys:
pattern.add(r'^{0}$|^{0}:'.format(key))
pattern = '|'.join(list(pattern))
if pattern:
args.append(Q(nodes__key__regex=pattern))
args = reduce(lambda x, y: x | y, args)
assets = Asset.objects.filter(args).distinct()
return assets
class Meta:
ordering = ['name']
unique_together = [('name', 'org_id')]

View File

@ -57,16 +57,16 @@ def on_system_user_update(sender, instance=None, created=True, **kwargs):
push_system_user_to_assets.delay(instance, assets)
@receiver(m2m_changed, sender=SystemUser.nodes.through)
def on_system_user_nodes_change(sender, instance=None, **kwargs):
if instance and kwargs["action"] == "post_add":
logger.info("System user `{}` nodes update signal received".format(instance))
assets = set()
nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
for node in nodes:
assets.update(set(node.get_all_assets()))
instance.assets.add(*tuple(assets))
# @receiver(m2m_changed, sender=SystemUser.nodes.through)
# def on_system_user_nodes_change(sender, instance=None, **kwargs):
# if instance and kwargs["action"] == "post_add":
# logger.info("System user `{}` nodes update signal received".format(instance))
# assets = set()
# nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
# for node in nodes:
# assets.update(set(node.get_all_assets()))
# instance.assets.add(*tuple(assets))
#
@receiver(m2m_changed, sender=SystemUser.assets.through)
def on_system_user_assets_change(sender, instance=None, **kwargs):

View File

@ -347,7 +347,7 @@ def test_system_user_connectivity_util(system_user, assets, task_name):
@shared_task
def test_system_user_connectivity_manual(system_user):
task_name = _("Test system user connectivity: {}").format(system_user)
assets = system_user.get_related_assets()
assets = system_user.get_all_assets()
return test_system_user_connectivity_util(system_user, assets, task_name)
@ -367,7 +367,7 @@ def test_system_user_connectivity_period():
system_users = SystemUser.objects.all()
for system_user in system_users:
task_name = _("Test system user connectivity period: {}").format(system_user)
assets = system_user.get_related_assets()
assets = system_user.get_all_assets()
test_system_user_connectivity_util(system_user, assets, task_name)
@ -513,7 +513,7 @@ def push_system_user_util(system_user, assets, task_name):
@shared_task
def push_system_user_to_assets_manual(system_user):
assets = system_user.get_related_assets()
assets = system_user.get_all_assets()
task_name = _("Push system users to assets: {}").format(system_user.name)
return push_system_user_util(system_user, assets, task_name=task_name)