diff --git a/apps/assets/models/base.py b/apps/assets/models/base.py index 895e0134d..5d0f3cef1 100644 --- a/apps/assets/models/base.py +++ b/apps/assets/models/base.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # +import io import os import uuid from hashlib import md5 @@ -76,6 +77,14 @@ class AssetUser(OrgModelMixin): i = '-'.join(str(self.id).split('-')[:3]) return i + def get_private_key(self): + if not self.private_key_obj: + return None + string_io = io.StringIO() + self.private_key_obj.write_private_key(string_io) + private_key = string_io.getvalue() + return private_key + def get_related_assets(self): assets = self.assets.all() return assets diff --git a/apps/assets/serializers/system_user.py b/apps/assets/serializers/system_user.py index 5295d74b2..e16a7a271 100644 --- a/apps/assets/serializers/system_user.py +++ b/apps/assets/serializers/system_user.py @@ -123,6 +123,7 @@ class SystemUserAuthSerializer(AuthSerializer): """ 系统用户认证信息 """ + private_key = serializers.SerializerMethodField() class Meta: model = SystemUser @@ -131,6 +132,10 @@ class SystemUserAuthSerializer(AuthSerializer): "login_mode", "password", "private_key", ] + @staticmethod + def get_private_key(obj): + return obj.get_private_key() + class SystemUserSimpleSerializer(serializers.ModelSerializer): """