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}}
- {{currentProfile.last_login_time}} + {{currentProfile.last_login_time | convertIsoTime}}