Skip to content

Commit

Permalink
sprintfix: 修复查询指定业务范围显示范围外任务(fixed #1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
neko12583 authored and ZhuoZhuoCrayon committed Jul 31, 2023
1 parent e7b4e99 commit 39d0805
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
14 changes: 9 additions & 5 deletions apps/node_man/handlers/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def list(self, params: dict, username: str):
all_biz_info = CmdbHandler().biz_id_name_without_permission()
biz_info = CmdbHandler().biz_id_name({"action": constants.IamActionType.task_history_view})
biz_permission = list(biz_info.keys())
if not biz_permission:
return {"total": 0, "list": []}

if params.get("job_id"):
job_ids = set()
for job_id_var in params["job_id"]:
Expand All @@ -174,20 +177,21 @@ def list(self, params: dict, username: str):

if search_biz_ids:
# 字典的 in 比列表性能更高
search_biz_ids = [bk_biz_id for bk_biz_id in search_biz_ids if bk_biz_id in biz_info]
biz_scope = [bk_biz_id for bk_biz_id in search_biz_ids if bk_biz_id in biz_info]
else:
search_biz_ids = biz_permission
biz_scope = biz_permission

if set(search_biz_ids) & all_biz_ids == all_biz_ids:
if set(biz_scope) & all_biz_ids == all_biz_ids:
biz_scope_query_q = Q()
else:
biz_scope_query_q = reduce(
operator.or_,
[Q(bk_biz_scope__contains=bk_biz_id) for bk_biz_id in search_biz_ids],
Q()
)
# 自身创建的 job 可见
biz_scope_query_q |= Q(created_by=username)
# 仅查询所有业务时,自身创建的 job 可见
if not search_biz_ids:
biz_scope_query_q |= Q(created_by=username)

# ip 搜索
inner_ip_query_q = Q()
Expand Down
16 changes: 8 additions & 8 deletions apps/node_man/tests/test_handlers/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ def test_job_list_without_permission(self):
bk_biz_scope=[SEARCH_BUSINESS[0]["bk_biz_id"]],
created_by="blueking"
)
create_job(
number,
id=998,
bk_biz_scope=[999],
created_by="blueking"
)
create_job(
number,
id=999,
Expand All @@ -141,12 +135,17 @@ def test_job_list_without_permission(self):
result = JobHandler().list({
"page": 1,
"pagesize": 10,
"bk_biz_id": [SEARCH_BUSINESS[0]["bk_biz_id"], 999]
"bk_biz_id": [SEARCH_BUSINESS[0]["bk_biz_id"]],
}, "admin")
self.assertEqual(result["total"], 1)

result = JobHandler().list({
"page": 1,
"pagesize": 10,
}, "admin")
self.assertEqual(result["total"], 2)
self.assertEqual(result["list"][0]["id"], 999)

@patch("apps.node_man.handlers.cmdb.client_v2", MockClient)
def test_job_list_with_ip(self):
"""测试 单/多ip 搜索"""
create_host(1, bk_cloud_id=0, ip="127.0.0.1")
Expand Down Expand Up @@ -191,6 +190,7 @@ def test_job_list_with_ip(self):
)
self.assertEqual(multiple_ip_result["total"], 2)

@patch("apps.node_man.handlers.cmdb.client_v2", MockClient)
def test_job_list_spend_time(self):
"""测试查询时间"""
create_host(1, bk_cloud_id=0, ip="127.0.0.1")
Expand Down

0 comments on commit 39d0805

Please sign in to comment.