-
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
Closed
Changes from 1 commit
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
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 |
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 |
---|---|---|
|
@@ -29,14 +29,22 @@ | |
TenantDeptUserRelationBatchPatchInputSLZ, | ||
TenantDeptUserRelationBatchUpdateInputSLZ, | ||
) | ||
from bkuser.apis.web.organization.views.mixins import CurrentUserTenantDataSourceMixin | ||
from bkuser.apis.web.organization.views.mixins import ( | ||
CurrentUserDepartmentRelationMixin, | ||
CurrentUserTenantDataSourceMixin, | ||
) | ||
from bkuser.apps.audit.constants import ObjectTypeEnum, OperationEnum | ||
from bkuser.apps.audit.data_model import AuditObject | ||
from bkuser.apps.audit.recorder import batch_add_audit_records | ||
from bkuser.apps.data_source.models import DataSourceDepartmentUserRelation | ||
from bkuser.apps.permission.constants import PermAction | ||
from bkuser.apps.permission.permissions import perm_class | ||
from bkuser.apps.tenant.models import TenantDepartment, TenantUser | ||
|
||
|
||
class TenantDeptUserRelationBatchCreateApi(CurrentUserTenantDataSourceMixin, generics.CreateAPIView): | ||
class TenantDeptUserRelationBatchCreateApi( | ||
CurrentUserTenantDataSourceMixin, CurrentUserDepartmentRelationMixin, generics.CreateAPIView | ||
): | ||
"""批量添加 / 拉取租户用户(添加部门 - 用户关系)""" | ||
|
||
permission_classes = [IsAuthenticated, perm_class(PermAction.MANAGE_TENANT)] | ||
|
@@ -66,6 +74,9 @@ def post(self, request, *args, **kwargs): | |
id__in=data["user_ids"], | ||
).values_list("data_source_user_id", flat=True) | ||
|
||
# 【审计】记录变更前用户-部门映射 | ||
user_department_map_before = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 复制操作:为数据源部门 & 用户添加关联边,但是不会影响存量的关联边 | ||
relations = [ | ||
DataSourceDepartmentUserRelation(user_id=user_id, department_id=dept_id, data_source=data_source) | ||
|
@@ -74,10 +85,39 @@ def post(self, request, *args, **kwargs): | |
# 由于复制操作不会影响存量的关联边,所以需要忽略冲突,避免出现用户复选的情况 | ||
DataSourceDepartmentUserRelation.objects.bulk_create(relations, ignore_conflicts=True) | ||
|
||
# 【审计】记录变更后用户-部门映射 | ||
user_department_map = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 【审计】创建用户-部门审计对象 | ||
audit_objects = [ | ||
AuditObject( | ||
id=tenant_user.data_source_user_id, | ||
name=tenant_user.data_source_user.username, | ||
type=ObjectTypeEnum.DATA_SOURCE_USER, | ||
operation=OperationEnum.MODIFY_USER_DEPARTMENT_RELATIONS, | ||
data_before={"department_ids": user_department_map_before[tenant_user.data_source_user_id]}, | ||
data_after={"department_ids": user_department_map[tenant_user.data_source_user_id]}, | ||
extras={"department_ids": list(data_source_dept_ids)}, | ||
) | ||
for tenant_user in TenantUser.objects.filter( | ||
tenant_id=cur_tenant_id, | ||
id__in=data["user_ids"], | ||
).select_related("data_source_user") | ||
] | ||
|
||
# 【审计】批量添加审计记录 | ||
batch_add_audit_records( | ||
operator=request.user.username, | ||
tenant_id=cur_tenant_id, | ||
objects=audit_objects, | ||
) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class TenantDeptUserRelationBatchUpdateApi(CurrentUserTenantDataSourceMixin, generics.UpdateAPIView): | ||
class TenantDeptUserRelationBatchUpdateApi( | ||
CurrentUserTenantDataSourceMixin, CurrentUserDepartmentRelationMixin, generics.UpdateAPIView | ||
): | ||
"""批量移动租户用户(更新部门 - 用户关系)""" | ||
|
||
permission_classes = [IsAuthenticated, perm_class(PermAction.MANAGE_TENANT)] | ||
|
@@ -107,6 +147,9 @@ def put(self, request, *args, **kwargs): | |
id__in=data["user_ids"], | ||
).values_list("data_source_user_id", flat=True) | ||
|
||
# 【审计】记录变更前用户-部门映射 | ||
user_department_map_before = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 移动操作:为数据源部门 & 用户添加关联边,但是会删除这批用户所有的存量关联边 | ||
with transaction.atomic(): | ||
# 先删除 | ||
|
@@ -118,6 +161,33 @@ def put(self, request, *args, **kwargs): | |
] | ||
DataSourceDepartmentUserRelation.objects.bulk_create(relations) | ||
|
||
# 【审计】记录变更后用户-部门映射 | ||
user_department_map = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 【审计】创建用户-部门审计对象 | ||
audit_objects = [ | ||
AuditObject( | ||
id=tenant_user.data_source_user_id, | ||
name=tenant_user.data_source_user.username, | ||
type=ObjectTypeEnum.DATA_SOURCE_USER, | ||
operation=OperationEnum.MODIFY_USER_DEPARTMENT_RELATIONS, | ||
data_before={"department_ids": user_department_map_before[tenant_user.data_source_user_id]}, | ||
data_after={"department_ids": user_department_map[tenant_user.data_source_user_id]}, | ||
extras={"department_ids": list(data_source_dept_ids)}, | ||
) | ||
for tenant_user in TenantUser.objects.filter( | ||
tenant_id=cur_tenant_id, | ||
id__in=data["user_ids"], | ||
).select_related("data_source_user") | ||
] | ||
|
||
# 【审计】批量添加审计记录 | ||
batch_add_audit_records( | ||
operator=request.user.username, | ||
tenant_id=cur_tenant_id, | ||
objects=audit_objects, | ||
) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
@swagger_auto_schema( | ||
|
@@ -147,6 +217,9 @@ def patch(self, request, *args, **kwargs): | |
id__in=data["user_ids"], | ||
).values_list("data_source_user_id", flat=True) | ||
|
||
# 【审计】记录变更前用户-部门映射 | ||
user_department_map_before = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 移动操作:为数据源部门 & 用户添加关联边,但是会删除这批用户在当前部门的存量关联边 | ||
with transaction.atomic(): | ||
# 先删除(仅限于指定部门) | ||
|
@@ -160,10 +233,39 @@ def patch(self, request, *args, **kwargs): | |
] | ||
DataSourceDepartmentUserRelation.objects.bulk_create(relations, ignore_conflicts=True) | ||
|
||
# 【审计】记录变更后用户-部门映射 | ||
user_department_map = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 【审计】创建用户-部门审计对象 | ||
audit_objects = [ | ||
AuditObject( | ||
id=tenant_user.data_source_user_id, | ||
name=tenant_user.data_source_user.username, | ||
type=ObjectTypeEnum.DATA_SOURCE_USER, | ||
operation=OperationEnum.MODIFY_USER_DEPARTMENT_RELATIONS, | ||
data_before={"department_ids": user_department_map_before[tenant_user.data_source_user_id]}, | ||
data_after={"department_ids": user_department_map[tenant_user.data_source_user_id]}, | ||
extras={}, | ||
) | ||
for tenant_user in TenantUser.objects.filter( | ||
tenant_id=cur_tenant_id, | ||
id__in=data["user_ids"], | ||
).select_related("data_source_user") | ||
] | ||
|
||
# 【审计】批量添加审计记录 | ||
batch_add_audit_records( | ||
operator=request.user.username, | ||
tenant_id=cur_tenant_id, | ||
objects=audit_objects, | ||
) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class TenantDeptUserRelationBatchDeleteApi(CurrentUserTenantDataSourceMixin, generics.DestroyAPIView): | ||
class TenantDeptUserRelationBatchDeleteApi( | ||
CurrentUserTenantDataSourceMixin, CurrentUserDepartmentRelationMixin, generics.DestroyAPIView | ||
): | ||
"""批量删除指定部门 & 用户的部门 - 用户关系""" | ||
|
||
permission_classes = [IsAuthenticated, perm_class(PermAction.MANAGE_TENANT)] | ||
|
@@ -191,8 +293,38 @@ def delete(self, request, *args, **kwargs): | |
id__in=data["user_ids"], | ||
).values_list("data_source_user_id", flat=True) | ||
|
||
# 【审计】记录变更前用户-部门映射 | ||
user_department_map_before = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
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. 为审计预留的数据, 统一命名:
|
||
|
||
DataSourceDepartmentUserRelation.objects.filter( | ||
user_id__in=data_source_user_ids, department=source_data_source_dept | ||
).delete() | ||
|
||
# 【审计】记录变更后用户-部门映射 | ||
user_department_map = self.get_user_department_map(data_source_user_ids=data_source_user_ids) | ||
|
||
# 【审计】创建用户-部门审计对象 | ||
audit_objects = [ | ||
AuditObject( | ||
id=tenant_user.data_source_user_id, | ||
name=tenant_user.data_source_user.username, | ||
type=ObjectTypeEnum.DATA_SOURCE_USER, | ||
operation=OperationEnum.MODIFY_USER_DEPARTMENT_RELATIONS, | ||
data_before={"department_ids": user_department_map_before[tenant_user.data_source_user_id]}, | ||
data_after={"department_ids": user_department_map[tenant_user.data_source_user_id]}, | ||
extras={"department_id": source_data_source_dept.id}, | ||
) | ||
for tenant_user in TenantUser.objects.filter( | ||
tenant_id=cur_tenant_id, | ||
id__in=data["user_ids"], | ||
).select_related("data_source_user") | ||
] | ||
|
||
# 【审计】批量添加审计记录 | ||
batch_add_audit_records( | ||
operator=request.user.username, | ||
tenant_id=cur_tenant_id, | ||
objects=audit_objects, | ||
) | ||
|
||
return Response(status=status.HTTP_204_NO_CONTENT) |
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 都做成可选参数