From 415d4475c8c0b1cef4640f3b12681a567d1179f3 Mon Sep 17 00:00:00 2001 From: IMBlues Date: Thu, 25 Nov 2021 14:51:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E7=BB=84=E7=BB=87?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E5=AF=BC=E5=85=A5=E6=A8=A1=E7=89=88=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=90=8D=20&=20=E5=8C=BA=E5=88=86=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AF=BC=E5=87=BA=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=20&=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=8E=E6=81=A2=E5=A4=8D=E9=80=BB=E8=BE=91=20&=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=88=9B=E5=BB=BA=E6=97=B6=E9=97=B4=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/bkuser_core/common/viewset.py | 2 +- src/api/bkuser_core/profiles/models.py | 16 +++++++++----- src/api/bkuser_core/profiles/serializers.py | 20 ++---------------- .../organization/details/UserMaterial.vue | 16 ++++++++++++-- src/pages/src/views/organization/index.vue | 7 ------ src/saas/bkuser_shell/audit/views.py | 2 +- src/saas/bkuser_shell/categories/views.py | 5 +++-- src/saas/media/excel/export_org_tmpl.xlsx | Bin 10629 -> 10631 bytes 8 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/api/bkuser_core/common/viewset.py b/src/api/bkuser_core/common/viewset.py index db8f1139a..c4e353950 100644 --- a/src/api/bkuser_core/common/viewset.py +++ b/src/api/bkuser_core/common/viewset.py @@ -245,7 +245,7 @@ def check_object_permissions(self, request, obj): if not permission.has_object_permission(request, self, obj): self.permission_denied(request, message=getattr(permission, "message", None), obj=obj) - def permission_denied(self, request, message=None, obj=None): + def permission_denied(self, request, message=None, obj=None, **kwargs): """针对 IAM 注入相关信息""" raise IAMPermissionDenied( detail=message, diff --git a/src/api/bkuser_core/profiles/models.py b/src/api/bkuser_core/profiles/models.py index b7a558688..d57f9724b 100644 --- a/src/api/bkuser_core/profiles/models.py +++ b/src/api/bkuser_core/profiles/models.py @@ -9,6 +9,7 @@ specific language governing permissions and limitations under the License. """ import datetime +from typing import Optional import jsonfield from bkuser_core.audit.constants import LogInFailReason @@ -181,6 +182,15 @@ def latest_password_update_time(self) -> datetime.datetime: """最近一次更新密码时间""" return self.password_update_time or self.create_time + @property + def last_login_time(self) -> Optional[datetime.datetime]: + """获取用户最近一次登录时间""" + latest_logins = self.login_set.filter(is_success=True) + if latest_logins: + return latest_logins.latest().create_time + + return None + def enable(self): self.enabled = True self.status = ProfileStatus.NORMAL.value @@ -188,13 +198,9 @@ def enable(self): def delete(self, using=None, keep_parents=False): """软删除""" + # 为了保证用户恢复时拥有原来所有关系,这里只修改状态字段 self.enabled = False self.status = ProfileStatus.DELETED.value - - # 解除与其他模型的绑定关系 - self.departments.clear() - self.leader.clear() - self.save(update_fields=["enabled", "status", "update_time"]) return diff --git a/src/api/bkuser_core/profiles/serializers.py b/src/api/bkuser_core/profiles/serializers.py index d88d7ebdc..342c080f0 100644 --- a/src/api/bkuser_core/profiles/serializers.py +++ b/src/api/bkuser_core/profiles/serializers.py @@ -71,15 +71,7 @@ class ProfileSerializer(CustomFieldsModelSerializer): departments = SimpleDepartmentSerializer(many=True, required=False) extras = serializers.SerializerMethodField(required=False) leader = LeaderSerializer(many=True, required=False) - last_login_time = serializers.SerializerMethodField(required=False, read_only=True) - - def get_last_login_time(self, obj: "Profile") -> Optional[str]: - """获取用户最近一次登录时间""" - latest_logins = obj.login_set.filter(is_success=True) - if latest_logins: - return latest_logins.latest().create_time - - return None + last_login_time = serializers.DateTimeField(required=False, read_only=True) def get_extras(self, obj) -> dict: """尝试从 context 中获取默认字段值""" @@ -106,7 +98,7 @@ class RapidProfileSerializer(CustomFieldsMixin, serializers.Serializer): departments = SimpleDepartmentSerializer(many=True, required=False) leader = LeaderSerializer(many=True, required=False) - last_login_time = serializers.SerializerMethodField(required=False, read_only=True) + last_login_time = serializers.DateTimeField(required=False, read_only=True) create_time = serializers.DateTimeField(required=False, read_only=True) update_time = serializers.DateTimeField(required=False, read_only=True) @@ -130,14 +122,6 @@ class RapidProfileSerializer(CustomFieldsMixin, serializers.Serializer): status = serializers.CharField(read_only=True) logo = serializers.CharField(read_only=True, allow_blank=True) - def get_last_login_time(self, obj: "Profile") -> Optional[str]: - """获取用户最近一次登录时间""" - latest_logins = obj.login_set.filter(is_success=True) - if latest_logins: - return latest_logins.latest().create_time - - return None - def get_extras(self, obj: "Profile") -> dict: """尝试从 context 中获取默认字段值""" return get_extras(obj.extras, self.context.get("extra_defaults", {}).copy()) diff --git a/src/pages/src/views/organization/details/UserMaterial.vue b/src/pages/src/views/organization/details/UserMaterial.vue index f14efa09e..5df64ae86 100644 --- a/src/pages/src/views/organization/details/UserMaterial.vue +++ b/src/pages/src/views/organization/details/UserMaterial.vue @@ -129,14 +129,14 @@ {{$t('创建时间')}}

- {{currentProfile.create_time}} + {{currentProfile.create_time | convertIsoTime}}

{{$t('最近一次登录时间')}}

- {{currentProfile.last_login_time}} + {{currentProfile.last_login_time | convertIsoTime}}

@@ -147,6 +147,18 @@