From 153ff82322cec4b83e71ce5a172456a5d0f495c7 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 19 Apr 2026 21:37:04 +0800 Subject: [PATCH] fix: password not saved correctly when editing asset --- server/api/asset.go | 12 +++++++++++- server/service/asset.go | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/server/api/asset.go b/server/api/asset.go index 29e3eb32f..7f5a6cf76 100644 --- a/server/api/asset.go +++ b/server/api/asset.go @@ -212,7 +212,7 @@ func (assetApi AssetApi) AssetGetEndpoint(c echo.Context) (err error) { id := c.Param("id") var item model.Asset - if item, err = service.AssetService.FindByIdAndDecrypt(context.TODO(), id); err != nil { + if item, err = repository.AssetRepository.FindById(context.TODO(), id); err != nil { return err } attributeMap, err := repository.AssetRepository.FindAssetAttrMapByAssetId(context.TODO(), id) @@ -224,6 +224,16 @@ func (assetApi AssetApi) AssetGetEndpoint(c echo.Context) (err error) { itemMap[key] = attributeMap[key] } + if item.Password != "" && item.Password != "-" { + itemMap["password"] = "" + } + if item.PrivateKey != "" && item.PrivateKey != "-" { + itemMap["privateKey"] = "" + } + if item.Passphrase != "" && item.Passphrase != "-" { + itemMap["passphrase"] = "" + } + return Success(c, itemMap) } diff --git a/server/service/asset.go b/server/service/asset.go index 3a3083888..f9bca57a9 100644 --- a/server/service/asset.go +++ b/server/service/asset.go @@ -248,6 +248,11 @@ func (s assetService) UpdateById(id string, m maps.Map) error { return err } + existingAsset, err := repository.AssetRepository.FindById(context.TODO(), id) + if err != nil { + return err + } + switch item.AccountType { case "credential": item.Username = "-" @@ -261,9 +266,9 @@ func (s assetService) UpdateById(id string, m maps.Map) error { item.Username = "-" } if len(item.Passphrase) == 0 { - item.Passphrase = "-" + item.Passphrase = existingAsset.Passphrase } - case "custom": + case "password": item.PrivateKey = "-" item.Passphrase = "-" item.CredentialId = "-" @@ -271,7 +276,8 @@ func (s assetService) UpdateById(id string, m maps.Map) error { item.Username = "-" } if len(item.Password) == 0 { - item.Password = "-" + item.Password = existingAsset.Password + item.Encrypted = existingAsset.Encrypted } } @@ -287,8 +293,10 @@ func (s assetService) UpdateById(id string, m maps.Map) error { item.AccessGatewayId = "-" } - if err := s.Encrypt(&item, config.GlobalCfg.EncryptionPassword); err != nil { - return err + if item.Password != "" && item.Password != "-" && !item.Encrypted { + if err := s.Encrypt(&item, config.GlobalCfg.EncryptionPassword); err != nil { + return err + } } return s.Transaction(context.Background(), func(ctx context.Context) error {