Skip to content

Commit

Permalink
feat: added audit operation for web api and data source
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Nov 14, 2024
1 parent 01c071d commit 477e5be
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/bk-user/bkuser/apis/web/audit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from rest_framework import serializers

from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum
from bkuser.apps.audit.constants import OBJECT_TYPE_MAP, OPERATION_MAP, ObjectTypeEnum, OperationEnum


class OperationAuditRecordListInputSerializer(serializers.Serializer):
Expand All @@ -40,8 +40,8 @@ class OperationAuditRecordListOutputSerializer(serializers.Serializer):

def get_operation(self, obj):
# 从 operation_map 中提取 operation 对应的中文标识
return self.context["operation_map"][obj.operation]
return OPERATION_MAP[obj.operation]

def get_object_type(self, obj):
# 从 object_type_map 中提取 object_type 对应的中文标识
return self.context["object_type_map"][obj.object_type]
return OBJECT_TYPE_MAP[obj.object_type]
7 changes: 0 additions & 7 deletions src/bk-user/bkuser/apis/web/audit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from rest_framework.permissions import IsAuthenticated

from bkuser.apis.web.mixins import CurrentUserTenantMixin
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
Expand Down Expand Up @@ -57,12 +56,6 @@ def get_queryset(self):

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="操作审计列表",
Expand Down
4 changes: 4 additions & 0 deletions src/bk-user/bkuser/apps/audit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ class OperationEnum(str, StructuredEnum):
CREATE_VIRTUAL_USER = EnumField("create_virtual_user", label=_("创建虚拟用户"))
MODIFY_VIRTUAL_USER = EnumField("modify_virtual_user", label=_("修改虚拟用户信息"))
DELETE_VIRTUAL_USER = EnumField("delete_virtual_user", label=_("删除虚拟用户"))


OPERATION_MAP = dict(OperationEnum.get_choices())
OBJECT_TYPE_MAP = dict(ObjectTypeEnum.get_choices())
7 changes: 4 additions & 3 deletions src/bk-user/bkuser/apps/audit/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ def add_audit_record(
"""
data_before = data_before or {}
data_after = data_after or {}
extras = extras or {}

# 若有数据变更,则添加记录
if data_before != data_after or extras != {}:
if data_before != data_after or extras:
OperationAuditRecord.objects.create(
creator=operator,
tenant_id=tenant_id,
Expand All @@ -62,7 +63,7 @@ def add_audit_record(
object_name=object_name,
data_before=data_before,
data_after=data_after,
extras=extras or {},
extras=extras,
)


Expand Down Expand Up @@ -96,7 +97,7 @@ def batch_add_audit_records(
)
for obj in objects
# 若有数据变更,则添加记录
if obj.data_before != obj.data_after or obj.extras != {}
if obj.data_before != obj.data_after or obj.extras
]

OperationAuditRecord.objects.bulk_create(records, batch_size=100)

0 comments on commit 477e5be

Please sign in to comment.