Skip to content

Commit

Permalink
fix(backend): 单据列表去除self_manage逻辑 TencentBlueKing#9051
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 29160
  • Loading branch information
ygcyao committed Jan 14, 2025
1 parent 7bbded4 commit 65fe035
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions dbm-ui/backend/ticket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import operator
from collections import Counter
from functools import reduce
from typing import Dict, List

from django.db import transaction
Expand All @@ -27,7 +25,6 @@
from backend.bk_web.pagination import AuditedLimitOffsetPagination
from backend.bk_web.swagger import PaginatedResponseSwaggerAutoSchema, common_swagger_auto_schema
from backend.configuration.constants import DBType
from backend.configuration.models import DBAdministrator
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
from backend.iam_app.dataclass import ResourceEnum
from backend.iam_app.dataclass.actions import ActionEnum
Expand Down Expand Up @@ -149,17 +146,12 @@ def _get_self_manage_tickets(cls, user):
# 超级管理员返回所有单据
if user.username in env.ADMIN_USERS or user.is_superuser:
return Ticket.objects.all()
# 获取user管理的单据合集
manage_filters = [
Q(group=manage.db_type) & Q(bk_biz_id=manage.bk_biz_id) if manage.bk_biz_id else Q(group=manage.db_type)
for manage in DBAdministrator.objects.filter(users__contains=user.username)
]
# 除了user管理的单据合集,处理人及协助人也能管理自己的单据
# 过滤user为处理人、协助人的单据
todo_filters = Q(
Q(todo_of_ticket__operators__contains=user.username) | Q(todo_of_ticket__helpers__contains=user.username)
)
ticket_filter = Q(creator=user.username) | todo_filters | reduce(operator.or_, manage_filters or [Q()])
return Ticket.objects.filter(ticket_filter).prefetch_related("todo_of_ticket")
ticket_filter = Q(creator=user.username) | todo_filters
return Ticket.objects.filter(ticket_filter).distinct()

def filter_queryset(self, queryset):
"""filter_class可能导致预取的todo失效,这里重新取一次"""
Expand All @@ -168,22 +160,11 @@ def filter_queryset(self, queryset):

def get_queryset(self):
"""
单据queryset规则--针对list:
1. self_manage=0 只返回自己管理的单据
2. self_manage=1,则返回自己管理组件的单据,如果是管理员则返回所有单据
获取ticket的queryset
"""
if self.action != "list" or "self_manage" not in self.request.query_params:
if self.action != "list":
return super().get_queryset()

username = self.request.user.username
self_manage = int(self.request.query_params["self_manage"])
# 只返回自己创建的单据
if self_manage == 0:
qs = Ticket.objects.filter(creator=username)
# 返回自己管理的组件单据
else:
qs = self._get_self_manage_tickets(self.request.user)
return qs
return self._get_self_manage_tickets(self.request.user)

def get_serializer_context(self):
context = super(TicketViewSet, self).get_serializer_context()
Expand Down Expand Up @@ -278,9 +259,6 @@ def list(self, request, *args, **kwargs):
resp = super().list(request, *args, **kwargs)
# 补充单据关联信息
resp.data["results"] = TicketHandler.add_related_object(resp.data["results"])
# 如果是查询自身单据(self_manage),则不进行鉴权
skip_iam = "self_manage" in request.query_params
resp.data["results"] = [{"skip_iam": skip_iam, **t} for t in resp.data["results"]]
return resp

@common_swagger_auto_schema(
Expand Down

0 comments on commit 65fe035

Please sign in to comment.