mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-10-23 00:38:39 +00:00
[Feature] 添加功能:数据库应用 (#3551)
* [Update] 添加数据库应用Model * [Update] 添加数据库应用ViewSet * [Update] 添加数据库应用HTML * [Update] 更新数据库应用迁移文件 * [Update] 添加数据库应用授权Model * [Update] 添加数据库应用授权ViewSet(待续) * [Update] 添加数据库应用授权ViewSet(完结) * [Update] 添加数据库应用授权View(待续) * [Update] 添加数据库应用授权View(待续2) * [Update] 修改远程应用授权View(小问题) * [Update] 添加数据库应用授权View(待续3) * [Update] 添加数据库应用授权View(完结) * [Update] 添加数据库应用授权相关API * [Update] 添加数据库应用View(用户页面) * [Update] 修改数据库应用授权Model/View/API(系统用户) * [Update] 修改系统用户Model/View(添加mysql协议) * [Update] 修改用户页面(我的应用) * [Update] 添加迁移文件 * [Update] 添加迁移文件2 * [Update] 续添加迁移文件2(Model更改) * [Update] 修改系统用户序列类(mysql协议自动生成密码问题) * [Update] 修改数据库应用/资产等授权序列类 * [Update] 修改命令列表/会话详情命令溢出 * [Update] 修改授权详情中添加系统用户的过滤 * [Update] 修改列表动作的宽度
This commit is contained in:
99
apps/perms/utils/database_app_permission.py
Normal file
99
apps/perms/utils/database_app_permission.py
Normal file
@@ -0,0 +1,99 @@
|
||||
# coding: utf-8
|
||||
#
|
||||
|
||||
from django.db.models import Q
|
||||
from orgs.utils import set_to_root_org
|
||||
|
||||
from ..models import DatabaseAppPermission
|
||||
from common.tree import TreeNode
|
||||
from applications.models import DatabaseApp
|
||||
from assets.models import SystemUser
|
||||
|
||||
|
||||
__all__ = [
|
||||
'DatabaseAppPermissionUtil',
|
||||
'construct_database_apps_tree_root',
|
||||
'parse_database_app_to_tree_node'
|
||||
]
|
||||
|
||||
|
||||
def get_user_database_app_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 DatabaseAppPermission.objects.all().valid().filter(arg)
|
||||
|
||||
|
||||
def get_user_group_database_app_permission(user_group):
|
||||
return DatabaseAppPermission.objects.all().valid().filter(
|
||||
user_group=user_group
|
||||
)
|
||||
|
||||
|
||||
class DatabaseAppPermissionUtil:
|
||||
get_permissions_map = {
|
||||
'User': get_user_database_app_permissions,
|
||||
'UserGroup': get_user_group_database_app_permission
|
||||
}
|
||||
|
||||
def __init__(self, obj):
|
||||
self.object = obj
|
||||
self.change_org_if_need()
|
||||
|
||||
@staticmethod
|
||||
def change_org_if_need():
|
||||
set_to_root_org()
|
||||
|
||||
@property
|
||||
def permissions(self):
|
||||
obj_class = self.object.__class__.__name__
|
||||
func = self.get_permissions_map[obj_class]
|
||||
_permissions = func(self.object)
|
||||
return _permissions
|
||||
|
||||
def get_database_apps(self):
|
||||
database_apps = DatabaseApp.objects.filter(
|
||||
granted_by_permissions__in=self.permissions
|
||||
)
|
||||
return database_apps
|
||||
|
||||
def get_database_app_system_users(self, database_app):
|
||||
queryset = self.permissions
|
||||
kwargs = {'database_apps': database_app}
|
||||
queryset = queryset.filter(**kwargs)
|
||||
system_users_ids = queryset.values_list('system_users', flat=True)
|
||||
system_users_ids = system_users_ids.distinct()
|
||||
system_users = SystemUser.objects.filter(id__in=system_users_ids)
|
||||
system_users = system_users.order_by('-priority')
|
||||
return system_users
|
||||
|
||||
|
||||
def construct_database_apps_tree_root():
|
||||
tree_root = {
|
||||
'id': 'ID_DATABASE_APP_ROOT',
|
||||
'name': 'DatabaseApp',
|
||||
'title': 'DatabaseApp',
|
||||
'pId': '',
|
||||
'open': False,
|
||||
'isParent': True,
|
||||
'iconSkin': '',
|
||||
'meta': {'type': 'database_app'}
|
||||
}
|
||||
return TreeNode(**tree_root)
|
||||
|
||||
|
||||
def parse_database_app_to_tree_node(parent, database_app):
|
||||
pid = parent.id if parent else ''
|
||||
tree_node = {
|
||||
'id': database_app.id,
|
||||
'name': database_app.name,
|
||||
'title': database_app.name,
|
||||
'pId': pid,
|
||||
'open': False,
|
||||
'isParent': False,
|
||||
'iconSkin': 'file',
|
||||
'meta': {'type': 'database_app'}
|
||||
}
|
||||
return TreeNode(**tree_node)
|
Reference in New Issue
Block a user