diff --git a/apps/assets/apps.py b/apps/assets/apps.py index 712033455..e1bb43544 100644 --- a/apps/assets/apps.py +++ b/apps/assets/apps.py @@ -13,4 +13,4 @@ class AssetsConfig(AppConfig): def ready(self): super().ready() - # from . import signal_handlers + from . import signal_handlers diff --git a/apps/assets/serializers/account/common.py b/apps/assets/serializers/account/common.py index 1627bf8c3..43c5c193b 100644 --- a/apps/assets/serializers/account/common.py +++ b/apps/assets/serializers/account/common.py @@ -19,6 +19,8 @@ class AccountFieldsSerializerMixin(serializers.ModelSerializer): extra_kwargs = { 'private_key': {'write_only': True}, 'public_key': {'write_only': True}, + 'token': {'write_only': True}, + 'password': {'write_only': True}, } def validate_name(self, value): diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index f55a73c3d..64854b3f4 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -49,6 +49,17 @@ class AssetPlatformSerializer(serializers.ModelSerializer): } +class AssetAccountSerializer(AccountSerializer): + add_org_fields = False + + class Meta(AccountSerializer.Meta): + fields_mini = [ + 'id', 'name', 'username', 'privileged', 'version' + ] + fields_write_only = ['password', 'private_key', 'public_key', 'passphrase', 'token'] + fields = fields_mini + fields_write_only + + class AssetSerializer(JMSWritableNestedModelSerializer): category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category')) type = LabeledChoiceField(choices=AllTypes.choices, read_only=True, label=_('Type')) @@ -56,7 +67,7 @@ class AssetSerializer(JMSWritableNestedModelSerializer): platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform')) nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes')) labels = AssetLabelSerializer(many=True, required=False, label=_('Labels')) - accounts = AccountSerializer(many=True, required=False, label=_('Accounts')) + accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts')) protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols')) """ 资产的数据结构 diff --git a/apps/assets/signal_handlers/account.py b/apps/assets/signal_handlers/account.py index 8020e4087..26cf75d02 100644 --- a/apps/assets/signal_handlers/account.py +++ b/apps/assets/signal_handlers/account.py @@ -9,6 +9,8 @@ logger = get_logger(__name__) @receiver(pre_save, sender=Account) def on_account_pre_create(sender, instance, **kwargs): + # Todo: 是否只有更改密码的时候才有版本增加, bitwarden 只有再改密码的时候, + # 才会有版本,代表的是 password_version # 升级版本号 instance.version += 1 # 即使在 root 组织也不怕 diff --git a/apps/orgs/mixins/serializers.py b/apps/orgs/mixins/serializers.py index fed9d1713..5c1a561e3 100644 --- a/apps/orgs/mixins/serializers.py +++ b/apps/orgs/mixins/serializers.py @@ -24,6 +24,7 @@ class OrgResourceSerializerMixin(CommonSerializerMixin, serializers.Serializer): """ org_id = serializers.ReadOnlyField(default=get_current_org_id_for_serializer, label=_("Organization")) org_name = serializers.ReadOnlyField(label=_("Org name")) + add_org_fields = True def get_validators(self): _validators = super().get_validators() @@ -38,7 +39,8 @@ class OrgResourceSerializerMixin(CommonSerializerMixin, serializers.Serializer): def get_field_names(self, declared_fields, info): fields = super().get_field_names(declared_fields, info) - fields.extend(["org_id", "org_name"]) + if self.add_org_fields: + fields.extend(["org_id", "org_name"]) return fields