Skip to content

Commit

Permalink
Fix Default Key Entry persistent definition and related deep copy method
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Jan 10, 2024
1 parent 5508ffe commit 7ab5ebe
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
15 changes: 13 additions & 2 deletions KeyManager.Library.UI/Domain/KeyEntriesControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue, Ke
{
DataContext = model
};
await EditDefaultKeyEntry(dialog);
if (await EditDefaultKeyEntry(dialog))
{
OnDefaultKeyEntryUpdated();
}
});

EditKeyEntryCommand = new AsyncRelayCommand<SelectableKeyEntryId>(
Expand Down Expand Up @@ -177,6 +180,12 @@ public KeyEntriesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue, Ke
private readonly ICollectionView _identifiersView;
private string? _searchTerms;

public event EventHandler? DefaultKeyEntryUpdated;
protected void OnDefaultKeyEntryUpdated()
{
DefaultKeyEntryUpdated?.Invoke(this, new EventArgs());
}

public ObservableCollection<SelectableKeyEntryId> Identifiers { get; }

public ObservableCollection<WizardFactory> WizardFactories { get; }
Expand Down Expand Up @@ -274,16 +283,18 @@ private async Task GenerateKeyEntry(KeyEntryDialog dialog)

public AsyncRelayCommand EditDefaultKeyEntryCommand { get; }

private async Task EditDefaultKeyEntry(KeyEntryDialog dialog)
private async Task<bool> EditDefaultKeyEntry(KeyEntryDialog dialog)
{
var ret = await DialogHelper.ForceShow(dialog, "RootDialog");
if (ret != null && dialog.DataContext is KeyEntryDialogViewModel model)
{
if (KeyStore != null && model.KeyEntry != null)
{
KeyStore.DefaultKeyEntries[KeyEntryClass] = model.KeyEntry;
return true;
}
}
return false;
}

public AsyncRelayCommand<SelectableKeyEntryId> EditKeyEntryCommand { get; }
Expand Down
2 changes: 2 additions & 0 deletions KeyManager.Library/Key.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Leosac.KeyManager.Library.Policy;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Text;

Expand Down Expand Up @@ -86,6 +87,7 @@ public uint KeySize

public ObservableCollection<string> Tags { get; set; }

[JsonIgnore]
public ObservableCollection<IKeyPolicy> Policies { get; set; }

private KeyLink _link;
Expand Down
15 changes: 13 additions & 2 deletions KeyManager.Library/KeyStore/KeyEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,19 @@ protected KeyEntryVariant CreateVariantFromAlgo(string algo, uint keySize)

public KeyEntry? DeepCopy()
{
var serialized = JsonConvert.SerializeObject(this);
return serialized != null ? JsonConvert.DeserializeObject<KeyEntry>(serialized) : null;
var serialized = JsonConvert.SerializeObject(this, CreateJsonSerializerSettings());
return serialized != null ? JsonConvert.DeserializeObject(serialized, this.GetType(), CreateJsonSerializerSettings()) as KeyEntry : null;
}

public static JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace,
Formatting = Formatting.Indented
};
}
}
}
8 changes: 1 addition & 7 deletions KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ public abstract class KeyStore

protected KeyStore()
{
_jsonSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace,
Formatting = Formatting.Indented
};
_jsonSettings = KeyEntry.CreateJsonSerializerSettings();
DefaultKeyEntries = new Dictionary<KeyEntryClass, KeyEntry?>();
Attributes = new Dictionary<string, string>();
}
Expand Down
34 changes: 27 additions & 7 deletions KeyManager/Domain/EditKeyStoreControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public EditKeyStoreControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
protected ISnackbarMessageQueue _snackbarMessageQueue;

protected IList<KeyEntriesControlViewModel> _keModels;
protected void OnKeyStoreUpdated()
{
if (Favorite != null)
{
var favorites = Favorites.GetSingletonInstance();
if (favorites != null)
{
SaveToFavorite(favorites, Favorite);
}
}
}

private KeyStore? _keyStore;

Expand Down Expand Up @@ -123,6 +134,10 @@ public async Task OpenKeyStore()
foreach (var kclass in classes)
{
var model = new KeyEntriesControlViewModel(_snackbarMessageQueue, kclass) { KeyStore = KeyStore };
model.DefaultKeyEntryUpdated += (sender, e) =>
{
OnKeyStoreUpdated();
};
_keModels.Add(model);
Tabs.Add(new TabItem
{
Expand Down Expand Up @@ -180,7 +195,6 @@ public static async Task EditFavorite(Favorites? favorites, Favorite fav)
{
if (favorites != null)
{
int favindex = favorites.KeyStores.IndexOf(fav);
var model = new KeyStoreSelectorDialogViewModel
{
Message = "Edit the Key Store Favorite"
Expand All @@ -196,19 +210,25 @@ public static async Task EditFavorite(Favorites? favorites, Favorite fav)
object? ret = await DialogHost.Show(dialog, "RootDialog");
if (ret != null)
{
if (favindex > -1)
{
favorites.KeyStores.RemoveAt(favindex);
}
fav.Properties = model.SelectedFactoryItem.DataContext.Properties;
favorites.KeyStores.Add(fav);
favorites.SaveToFile();
SaveToFavorite(favorites, fav);
}
}
}
}
}

private static void SaveToFavorite(Favorites favorites, Favorite fav)
{
int favindex = favorites.KeyStores.IndexOf(fav);
if (favindex > -1)
{
favorites.KeyStores.RemoveAt(favindex);
}
favorites.KeyStores.Add(fav);
favorites.SaveToFile();
}

public async Task Publish()
{
if (KeyStore != null)
Expand Down

0 comments on commit 7ab5ebe

Please sign in to comment.