mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-01-20 17:44:26 +00:00
* [Update] 添加org * [Update] 修改url * [Update] 完成基本框架 * [Update] 修改一些逻辑 * [Update] 修改用户view * [Update] 修改资产 * [Update] 修改asset api * [Update] 修改协议小问题 * [Update] stash it * [Update] 修改约束 * [Update] 修改外键为org_id * [Update] 删掉Premiddleware * [Update] 修改Node * [Update] 修改get_current_org 为 proxy对象 current_org * [Bugfix] 解决Node.root() 死循环,移动AdminRequired到permission中 (#1571) * [Update] 修改permission (#1574) * Tmp org (#1579) * [Update] 添加org api, 升级到django 2.0 * [Update] fix some bug * [Update] 修改一些bug * [Update] 添加授权规则org (#1580) * [Update] 修复创建授权规则,显示org_name不是有效UUID的bug * [Update] 更新org之间隔离授权规则,解决QuerySet与Manager问题;修复创建用户,显示org_name不是有效UUID之bug; * Tmp org (#1583) * [Update] 修改一些内容 * [Update] 修改datatable 支持process * [Bugfix] 修复asset queryset 没有valid方法的bug * [Update] 在线/历史/命令model添加org;修复命令记录保存org失败bug (#1584) * [Update] 修复创建授权规则,显示org_name不是有效UUID的bug * [Update] 更新org之间隔离授权规则,解决QuerySet与Manager问题;修复创建用户,显示org_name不是有效UUID之bug; * [Update] 在线/历史/命令model添加org * [Bugfix] 修复命令记录,保存org不成功bug * [Update] Org功能修改 * [Bugfix] 修复merge带来的问题 * [Update] org admin显示资产详情右侧选项卡;修复资产授权添加用户,会显示其他org用户的bug (#1594) * [Bugfix] 修复资产授权添加用户,显示其他org的用户bug * [Update] org admin 显示资产详情右侧选项卡 * Tmp org (#1596) * [Update] 修改index view * [Update] 修改nav * [Update] 修改profile * [Bugfix] 修复org下普通用户打开web终端看不到已被授权的资产和节点bug * [Update] 修改get_all_assets * [Bugfix] 修复节点前面有个空目录 * [Bugfix] 修复merge引起的bug * [Update] Add init * [Update] Node get_all_assets 过滤游离资产,条件nodes_key=None -> nodes=None * [Update] 恢复原来的api地址 * [Update] 修改api * [Bugfix] 修复org下用户查看我的资产不显示已授权节点/资产的bug * [Bugfix] Fix perm name unique * [Bugfix] 修复校验失败api * [Update] Merge with org * [Merge] 修改一下bug * [Update] 暂时修改一些url * [Update] 修改url 为django 2.0 path * [Update] 优化datatable 和显示组织优化 * [Update] 升级url * [Bugfix] 修复coco启动失败(load_config_from_server)、硬件刷新,测试连接,str 没有 decode(… (#1613) * [Bugfix] 修复coco启动失败(load_config_from_server)、硬件刷新,测试连接,str 没有 decode() method的bug * [Bugfix] (task任务系统)修复资产连接性测试、硬件刷新和系统用户连接性测试失败等bug * [Bugfix] 修复一些bug * [Bugfix] 修复一些bug * [Update] 更新org下普通用户的资产详情 (#1619) * [Update] 更新org下普通用户查看资产详情,只显示数据 * [Update] 优化org下普通用户查看资产详情前端代码 * [Update] 创建/更新用户的role选项;密码强度提示信息中英文; (#1623) * [Update] 修改 超级管理员/组织管理员 在 创建/更新 用户时role的选项 问题 * [Update] 用户密码强度提示信息支持中英文 * [Update] 修改token返回 * [Update] Asset返回org name * [Update] 修改支持xpack * [Update] 修改url * [Bugfix] 修复不登录就能查看资产的bug * [Update] 用户修改 * [Bugfix] ... * [Bugfix] 修复跳转错误的问题 * [Update] xpack/orgs组织添加删除功能-js; 修复Label继承Org后bug; (#1644) * [Update] 更新xpack下orgs的翻译信息 * [Update] 更新model Label,继承OrgModelMixin; * [Update] xpack/orgs组织添加删除功能-js; 修复Label继承Org后bug; * [Bugfix] 修复小bug * [Update] 优化一些api * [Update] 优化用户资产页面 * [Update] 更新 xpack/orgs 删除功能:限制在当前org下删除当前org (#1645) * [Update] 修改版本号
161 lines
4.7 KiB
Python
161 lines
4.7 KiB
Python
# coding: utf-8
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
from collections import defaultdict
|
|
from django.db.models import Q
|
|
|
|
from common.utils import get_logger
|
|
from .models import AssetPermission
|
|
from .hands import Node
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
class Tree:
|
|
def __init__(self):
|
|
self.__all_nodes = Node.objects.all().prefetch_related('assets')
|
|
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 = [a.id for a in node.assets.all()]
|
|
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:
|
|
return
|
|
else:
|
|
self.nodes[node] = defaultdict(set)
|
|
if node.key == self.root.key:
|
|
return
|
|
parent_key = ':'.join(node.key.split(':')[:-1])
|
|
for n in self.__all_nodes:
|
|
if n.key == parent_key:
|
|
self.add_node(n)
|
|
break
|
|
|
|
def add_nodes(self, nodes):
|
|
for node in nodes:
|
|
self.add_node(node)
|
|
|
|
|
|
def get_user_permissions(user, include_group=True):
|
|
if include_group:
|
|
groups = user.groups.all()
|
|
arg = Q(users=user) | Q(user_groups__in=groups)
|
|
else:
|
|
arg = Q(users=user)
|
|
return AssetPermission.objects.all().valid().filter(arg)
|
|
|
|
|
|
def get_user_group_permissions(user_group):
|
|
return AssetPermission.objects.all().valid().filter(
|
|
user_groups=user_group
|
|
)
|
|
|
|
|
|
def get_asset_permissions(asset, include_node=True):
|
|
if include_node:
|
|
nodes = asset.get_all_nodes(flat=True)
|
|
arg = Q(assets=asset) | Q(nodes__in=nodes)
|
|
else:
|
|
arg = Q(assets=asset)
|
|
return AssetPermission.objects.all().valid().filter(arg)
|
|
|
|
|
|
def get_node_permissions(node):
|
|
return AssetPermission.objects.all().valid().filter(nodes=node)
|
|
|
|
|
|
def get_system_user_permissions(system_user):
|
|
return AssetPermission.objects.valid().all().filter(
|
|
system_users=system_user
|
|
)
|
|
|
|
|
|
class AssetPermissionUtil:
|
|
get_permissions_map = {
|
|
"User": get_user_permissions,
|
|
"UserGroup": get_user_group_permissions,
|
|
"Asset": get_asset_permissions,
|
|
"Node": get_node_permissions,
|
|
"SystemUser": get_node_permissions,
|
|
}
|
|
|
|
def __init__(self, obj):
|
|
self.object = obj
|
|
self._permissions = None
|
|
self._assets = None
|
|
|
|
@property
|
|
def permissions(self):
|
|
if self._permissions:
|
|
return self._permissions
|
|
object_cls = self.object.__class__.__name__
|
|
func = self.get_permissions_map[object_cls]
|
|
permissions = func(self.object)
|
|
self._permissions = permissions
|
|
return permissions
|
|
|
|
def get_nodes_direct(self):
|
|
"""
|
|
返回用户/组授权规则直接关联的节点
|
|
:return: {node1: set(system_user1,)}
|
|
"""
|
|
nodes = defaultdict(set)
|
|
permissions = self.permissions.prefetch_related('nodes', 'system_users')
|
|
for perm in permissions:
|
|
for node in perm.nodes.all():
|
|
nodes[node].update(perm.system_users.all())
|
|
return nodes
|
|
|
|
def get_assets_direct(self):
|
|
"""
|
|
返回用户授权规则直接关联的资产
|
|
:return: {asset1: set(system_user1,)}
|
|
"""
|
|
assets = defaultdict(set)
|
|
permissions = self.permissions.prefetch_related('assets', 'system_users')
|
|
for perm in permissions:
|
|
for asset in perm.assets.all().valid().prefetch_related('nodes'):
|
|
assets[asset].update(perm.system_users.all())
|
|
return assets
|
|
|
|
def get_assets(self):
|
|
if self._assets:
|
|
return self._assets
|
|
assets = self.get_assets_direct()
|
|
nodes = self.get_nodes_direct()
|
|
for node, system_users in nodes.items():
|
|
_assets = node.get_all_assets().valid().prefetch_related('nodes')
|
|
for asset in _assets:
|
|
if isinstance(asset, Node):
|
|
print(_assets)
|
|
assets[asset].update(system_users)
|
|
self._assets = assets
|
|
return self._assets
|
|
|
|
def get_nodes_with_assets(self):
|
|
"""
|
|
返回节点并且包含资产
|
|
{"node": {"assets": set("system_user")}}
|
|
:return:
|
|
"""
|
|
assets = self.get_assets()
|
|
tree = Tree()
|
|
for asset, system_users in assets.items():
|
|
tree.add_asset(asset, system_users)
|
|
return tree.nodes
|
|
|
|
|