-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added audit operation for data source and user #1981
Closed
+1,025
−210
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - 用户管理 (bk-user) available. | ||
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
# Licensed under the MIT License (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# http://opensource.org/licenses/MIT | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on 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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - 用户管理 (bk-user) available. | ||
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
# Licensed under the MIT License (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# http://opensource.org/licenses/MIT | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on 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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
|
||
|
||
from rest_framework import serializers | ||
|
||
from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum | ||
|
||
|
||
class OperationAuditRecordListInputSerializer(serializers.Serializer): | ||
operator = serializers.CharField(help_text="操作人", required=False, allow_blank=True) | ||
operation = serializers.ChoiceField(help_text="操作行为", choices=OperationEnum.get_choices(), required=False) | ||
object_type = serializers.ChoiceField( | ||
help_text="操作对象类型", choices=ObjectTypeEnum.get_choices(), required=False | ||
) | ||
object_name = serializers.CharField(help_text="操作对象名称", required=False, allow_blank=True) | ||
created_at = serializers.DateTimeField(help_text="操作时间", required=False) | ||
|
||
|
||
class OperationAuditRecordListOutputSerializer(serializers.Serializer): | ||
operator = serializers.CharField(help_text="操作人", source="creator") | ||
operation = serializers.SerializerMethodField(help_text="操作行为") | ||
object_type = serializers.SerializerMethodField(help_text="操作对象类型") | ||
object_name = serializers.CharField(help_text="操作对象名称", required=False) | ||
created_at = serializers.DateTimeField(help_text="操作时间") | ||
|
||
def get_operation(self, obj): | ||
# 从 operation_map 中提取 operation 对应的中文标识 | ||
return self.context["operation_map"][obj.operation] | ||
|
||
def get_object_type(self, obj): | ||
# 从 object_type_map 中提取 object_type 对应的中文标识 | ||
return self.context["object_type_map"][obj.object_type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - 用户管理 (bk-user) available. | ||
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
# Licensed under the MIT License (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# http://opensource.org/licenses/MIT | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on 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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
|
||
from django.urls import path | ||
|
||
from bkuser.apis.web.audit import views | ||
|
||
urlpatterns = [ | ||
# 操作审计列表 | ||
path( | ||
"", | ||
views.AuditRecordListAPIView.as_view(), | ||
name="audit.list", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
# TencentBlueKing is pleased to support the open source community by making | ||
# 蓝鲸智云 - 用户管理 (bk-user) available. | ||
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
# Licensed under the MIT License (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# http://opensource.org/licenses/MIT | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on 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. | ||
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
|
||
from django.db.models import Q | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework import generics, status | ||
from rest_framework.permissions import IsAuthenticated | ||
|
||
from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum | ||
from bkuser.apps.audit.models import OperationAuditRecord | ||
from bkuser.apps.permission.constants import PermAction | ||
from bkuser.apps.permission.permissions import perm_class | ||
|
||
from .serializers import OperationAuditRecordListInputSerializer, OperationAuditRecordListOutputSerializer | ||
|
||
|
||
class AuditRecordListAPIView(generics.ListAPIView): | ||
"""操作审计记录列表""" | ||
|
||
permission_classes = [IsAuthenticated, perm_class(PermAction.MANAGE_TENANT)] | ||
|
||
serializer_class = OperationAuditRecordListOutputSerializer | ||
|
||
def get_queryset(self): | ||
slz = OperationAuditRecordListInputSerializer(data=self.request.query_params) | ||
slz.is_valid(raise_exception=True) | ||
params = slz.validated_data | ||
|
||
filters = Q() | ||
if params.get("operator"): | ||
filters &= Q(creator=params["operator"]) | ||
if params.get("operation"): | ||
filters &= Q(operation=params["operation"]) | ||
if params.get("object_type"): | ||
filters &= Q(object_type=params["object_type"]) | ||
if params.get("created_at"): | ||
start_time = params["created_at"].replace(microsecond=0) | ||
end_time = params["created_at"].replace(microsecond=999999) | ||
filters &= Q(created_at__range=(start_time, end_time)) | ||
if params.get("object_name"): | ||
filters &= Q(object_name__icontains=params["object_name"]) | ||
|
||
return OperationAuditRecord.objects.filter(filters) | ||
|
||
def get_serializer_context(self): | ||
context = super().get_serializer_context() | ||
context["operation_map"] = dict(OperationEnum.get_choices()) | ||
context["object_type_map"] = dict(ObjectTypeEnum.get_choices()) | ||
return context | ||
|
||
@swagger_auto_schema( | ||
tags=["audit"], | ||
operation_description="操作审计列表", | ||
query_serializer=OperationAuditRecordListInputSerializer(), | ||
responses={status.HTTP_200_OK: OperationAuditRecordListOutputSerializer(many=True)}, | ||
) | ||
def get(self, request, *args, **kwargs): | ||
return self.list(request, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,14 @@ | |
# | ||
# We undertake not to change the open source license (MIT license) applicable | ||
# to the current version of the project delivered to anyone in the future. | ||
from collections import defaultdict | ||
from typing import Dict, List | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from bkuser.apis.web.mixins import CurrentUserTenantMixin | ||
from bkuser.apps.data_source.constants import DataSourceTypeEnum | ||
from bkuser.apps.data_source.models import DataSource | ||
from bkuser.apps.data_source.models import DataSource, DataSourceDepartmentUserRelation, DataSourceUserLeaderRelation | ||
from bkuser.common.error_codes import error_codes | ||
|
||
|
||
|
@@ -40,3 +43,37 @@ def get_current_tenant_local_real_data_source(self) -> DataSource: | |
raise error_codes.DATA_SOURCE_NOT_EXIST.f(_("当前租户不存在本地实名用户数据源")) | ||
|
||
return real_data_source | ||
|
||
|
||
class CurrentUserDepartmentRelationMixin: | ||
"""获取用户与部门之间的映射关系""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要讨论:加这个 mixin 来算数据,是否合适? |
||
|
||
def get_user_department_map(self, data_source_user_ids: List[int]) -> Dict: | ||
# 记录用户与部门之间的映射关系 | ||
user_department_relations = DataSourceDepartmentUserRelation.objects.filter( | ||
user_id__in=data_source_user_ids | ||
).values("department_id", "user_id") | ||
user_department_map = defaultdict(list) | ||
|
||
# 将用户的所有部门存储在列表中 | ||
for relation in user_department_relations: | ||
user_department_map[relation["user_id"]].append(relation["department_id"]) | ||
|
||
return user_department_map | ||
|
||
|
||
class CurrentUserLeaderRelationMixin: | ||
"""获取用户与上级之间的映射关系""" | ||
|
||
def get_user_leader_map(self, data_source_user_ids: List[int]) -> Dict: | ||
# 记录用户与上级之间的映射关系 | ||
user_leader_relations = DataSourceUserLeaderRelation.objects.filter(user_id__in=data_source_user_ids).values( | ||
"leader_id", "user_id" | ||
) | ||
user_leader_map = defaultdict(list) | ||
|
||
# 将用户的所有上级存储在列表中 | ||
for relation in user_leader_relations: | ||
user_leader_map[relation["user_id"]].append(relation["leader_id"]) | ||
|
||
return user_leader_map |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议 data_before, data_after, extras 都做成可选参数