fix: 修复用户提交应用发布申请时可以发布到该用户无权访问的主机中的问题

原来的逻辑会将该应用发布配置所关联的所有的主机返回给前端,但是会导致将用户无权访问的主机也返回回去,用户无法在页面上看到具体的ip,只能看到空白的选项条,但是用户其实可以选中并发布,造成应用更新发布到用户无权访问的主机中的问题。
pull/626/head
韩家旭 2023-08-17 19:49:41 +08:00
parent 173e14d5f1
commit 246e602e88
1 changed files with 16 additions and 0 deletions

View File

@ -5,9 +5,11 @@ from django.views.generic import View
from django.db.models import F
from libs import JsonParser, Argument, json_response, auth
from apps.app.models import App, Deploy, DeployExtend1, DeployExtend2
from apps.host.models import Host
from apps.config.models import Config, ConfigHistory, Service
from apps.app.utils import fetch_versions, remove_repo
from apps.setting.utils import AppSetting
from apps.account.utils import get_host_perms
import json
import re
@ -117,6 +119,20 @@ class DeployView(View):
deploys = Deploy.objects.filter(**form) \
.annotate(app_name=F('app__name'), app_key=F('app__key')) \
.order_by('-app__sort_id')
# 获取用户有权访问的主机列表
hosts = Host.objects.select_related('hostextend')
if not request.user.is_supper:
hosts = hosts.filter(id__in=get_host_perms(request.user))
# 提取出所有的 host id 到列表中
allowed_host_ids = list(map(lambda x: x.id, hosts))
# 遍历每一个发布配置,并修改其中的 host_ids 字段
for deploy_item in deploys:
# 解析 host_ids 字符串
all_host_ids = json.loads(deploy_item.host_ids)
# 获取所有用户有权访问的主机
available_host_ids = list(filter(lambda x: x in allowed_host_ids, all_host_ids))
# 将该列表重新赋值给发布配置的 host_ids 字段
deploy_item.host_ids = json.dumps(available_host_ids)
return json_response(deploys)
@auth('deploy.app.edit')