|
|
|
@ -3,12 +3,13 @@
|
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
|
import collections
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from django.db.models import Q
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
import copy
|
|
|
|
|
|
|
|
|
|
from common.utils import set_or_append_attr_bulk, get_logger
|
|
|
|
|
from .models import AssetPermission
|
|
|
|
|
from .hands import Node
|
|
|
|
|
from .hands import Node, User, UserGroup, Asset, SystemUser
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
@ -254,106 +255,47 @@ class AssetPermissionUtil:
|
|
|
|
|
return system_users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Abandon
|
|
|
|
|
class NodePermissionUtil:
|
|
|
|
|
"""
|
|
|
|
|
class AssetPermissionUtilsV2:
|
|
|
|
|
def __init__(self, obj):
|
|
|
|
|
self.object = obj
|
|
|
|
|
self._permissions = None
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_user_permissions(user):
|
|
|
|
|
groups = user.groups.all()
|
|
|
|
|
return AssetPermission.objects.all().valid().filter(
|
|
|
|
|
Q(users=user) | Q(user_groups=groups)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_user_group_permissions(user_group):
|
|
|
|
|
return user_group.nodepermission_set.all() \
|
|
|
|
|
.filter(is_active=True) \
|
|
|
|
|
.filter(date_expired__gt=timezone.now())
|
|
|
|
|
return AssetPermission.objects.all().valid().filter(
|
|
|
|
|
user_groups=user_group
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_system_user_permissions(system_user):
|
|
|
|
|
return system_user.nodepermission_set.all() \
|
|
|
|
|
.filter(is_active=True) \
|
|
|
|
|
.filter(date_expired__gt=timezone.now())
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_group_nodes(cls, user_group):
|
|
|
|
|
"""
|
|
|
|
|
获取用户组授权的node和系统用户
|
|
|
|
|
:param user_group:
|
|
|
|
|
:return: {"node": set(systemuser1, systemuser2), ..}
|
|
|
|
|
"""
|
|
|
|
|
permissions = cls.get_user_group_permissions(user_group)
|
|
|
|
|
nodes_directed = collections.defaultdict(set)
|
|
|
|
|
|
|
|
|
|
for perm in permissions:
|
|
|
|
|
nodes_directed[perm.node].add(perm.system_user)
|
|
|
|
|
|
|
|
|
|
nodes = copy.deepcopy(nodes_directed)
|
|
|
|
|
for node, system_users in nodes_directed.items():
|
|
|
|
|
for child in node.get_all_children_with_self():
|
|
|
|
|
nodes[child].update(system_users)
|
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_group_nodes_with_assets(cls, user_group):
|
|
|
|
|
"""
|
|
|
|
|
获取用户组授权的节点和系统用户,节点下带有资产
|
|
|
|
|
:param user_group:
|
|
|
|
|
:return: {"node": {"assets": "", "system_user": ""}, {}}
|
|
|
|
|
"""
|
|
|
|
|
nodes = cls.get_user_group_nodes(user_group)
|
|
|
|
|
nodes_with_assets = dict()
|
|
|
|
|
for node, system_users in nodes.items():
|
|
|
|
|
nodes_with_assets[node] = {
|
|
|
|
|
'assets': node.get_valid_assets(),
|
|
|
|
|
'system_users': system_users
|
|
|
|
|
}
|
|
|
|
|
return nodes_with_assets
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_group_assets(cls, user_group):
|
|
|
|
|
assets = collections.defaultdict(set)
|
|
|
|
|
permissions = cls.get_user_group_permissions(user_group)
|
|
|
|
|
|
|
|
|
|
for perm in permissions:
|
|
|
|
|
for asset in perm.node.get_all_assets():
|
|
|
|
|
assets[asset].add(perm.system_user)
|
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_nodes(cls, user):
|
|
|
|
|
nodes = collections.defaultdict(set)
|
|
|
|
|
groups = user.groups.all()
|
|
|
|
|
for group in groups:
|
|
|
|
|
group_nodes = cls.get_user_group_nodes(group)
|
|
|
|
|
for node, system_users in group_nodes.items():
|
|
|
|
|
nodes[node].update(system_users)
|
|
|
|
|
return nodes
|
|
|
|
|
def get_asset_permissions(asset):
|
|
|
|
|
direct_nodes = asset.get_nodes_or_cache()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_nodes_with_assets(cls, user):
|
|
|
|
|
nodes = cls.get_user_nodes(user)
|
|
|
|
|
nodes_with_assets = dict()
|
|
|
|
|
for node, system_users in nodes.items():
|
|
|
|
|
nodes_with_assets[node] = {
|
|
|
|
|
'assets': node.get_valid_assets(),
|
|
|
|
|
'system_users': system_users
|
|
|
|
|
}
|
|
|
|
|
return nodes_with_assets
|
|
|
|
|
return AssetPermission.objects.all().valid().filter(
|
|
|
|
|
Q(assets=asset) | Q(nodes=direct_nodes)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_user_assets(cls, user):
|
|
|
|
|
assets = collections.defaultdict(set)
|
|
|
|
|
nodes_with_assets = cls.get_user_nodes_with_assets(user)
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_node_permissions(node):
|
|
|
|
|
return AssetPermission.objects.all().valid().filter(nodes=node)
|
|
|
|
|
|
|
|
|
|
for v in nodes_with_assets.values():
|
|
|
|
|
for asset in v['assets']:
|
|
|
|
|
assets[asset].update(v['system_users'])
|
|
|
|
|
return assets
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_system_user_permissions(system_user):
|
|
|
|
|
return AssetPermission.objects.valid().all().filter(
|
|
|
|
|
system_users=system_user
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_system_user_assets(cls, system_user):
|
|
|
|
|
assets = set()
|
|
|
|
|
permissions = cls.get_system_user_permissions(system_user)
|
|
|
|
|
@property
|
|
|
|
|
def permissions(self):
|
|
|
|
|
if self._permissions:
|
|
|
|
|
return self._permissions
|
|
|
|
|
if isinstance(self.object, User):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
for perm in permissions:
|
|
|
|
|
assets.update(perm.node.get_all_assets())
|
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|