diff --git a/KeyVaultExplorer/ViewModels/CreateNewKeyVersionViewModel .cs b/KeyVaultExplorer/ViewModels/CreateNewKeyVersionViewModel .cs new file mode 100644 index 00000000..9c761373 --- /dev/null +++ b/KeyVaultExplorer/ViewModels/CreateNewKeyVersionViewModel .cs @@ -0,0 +1,90 @@ +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using KeyVaultExplorer.Views; +using KeyVaultExplorer.Services; +using System.Threading.Tasks; +using System.Threading; +using System.Linq; +using Azure.Security.KeyVault.Secrets; +using System; +using Azure.Security.KeyVault.Keys; + +namespace KeyVaultExplorer.ViewModels; + +public partial class CreateNewKeyVersionViewModel : ViewModelBase +{ + [ObservableProperty] + private bool isBusy = false; + + [ObservableProperty] + private bool isEdit = false; + + public bool HasActivationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.NotBefore.HasValue; + public bool HasExpirationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.ExpiresOn.HasValue; + + [ObservableProperty] + private string value; + + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(Location))] + [NotifyPropertyChangedFor(nameof(HasActivationDate))] + [NotifyPropertyChangedFor(nameof(HasExpirationDate))] + private KeyProperties keyVaultKeyModel; + + [ObservableProperty] + private TimeSpan? expiresOnTimespan; + + [ObservableProperty] + private TimeSpan? notBeforeTimespan; + + public string? Location => KeyVaultKeyModel?.VaultUri.ToString(); + public string? Identifier => KeyVaultKeyModel?.Id.ToString(); + + private readonly AuthService _authService; + private readonly VaultService _vaultService; + private NotificationViewModel _notificationViewModel; + + public CreateNewKeyVersionViewModel() + { + _authService = Defaults.Locator.GetRequiredService(); + _vaultService = Defaults.Locator.GetRequiredService(); + _notificationViewModel = Defaults.Locator.GetRequiredService(); + } + + [RelayCommand] + public async Task EditDetails() + { + if (KeyVaultKeyModel.NotBefore.HasValue) + KeyVaultKeyModel.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero); + + if (KeyVaultKeyModel.ExpiresOn.HasValue) + KeyVaultKeyModel.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero); + + //var updatedProps = await _vaultService.UpdateSecret(KeyVaultKeyModel, KeyVaultKeyModel.VaultUri); + //KeyVaultKeyModel = updatedProps; + } + + [RelayCommand] + public async Task NewVersion() + { + var newSecret = new KeyVaultSecret(KeyVaultKeyModel.Name, Value); + if (KeyVaultKeyModel.NotBefore.HasValue) + newSecret.Properties.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero); + + if (KeyVaultKeyModel.ExpiresOn.HasValue) + newSecret.Properties.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero); + + + var newVersion = await _vaultService.CreateSecret(newSecret, KeyVaultKeyModel.VaultUri); + var properties = (await _vaultService.GetSecretProperties(newVersion.Properties.VaultUri, newVersion.Name)).First(); + //KeyVaultKeyModel = properties; + } + + partial void OnKeyVaultKeyModelChanging(KeyProperties model) + { + ExpiresOnTimespan = model is not null && model.ExpiresOn.HasValue ? model?.ExpiresOn.Value.LocalDateTime.TimeOfDay : null; + NotBeforeTimespan = model is not null && model.NotBefore.HasValue ? model?.NotBefore.Value.LocalDateTime.TimeOfDay : null; + } +} \ No newline at end of file diff --git a/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml b/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml new file mode 100644 index 00000000..1426e7d0 --- /dev/null +++ b/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Set Activation Date + + + + + + + Set Expiration Date + + + + + + + + + + + + + + + + + + + + + diff --git a/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml.cs b/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml.cs new file mode 100644 index 00000000..67c23cbe --- /dev/null +++ b/KeyVaultExplorer/Views/Pages/PropertiesDialogs/CreateNewKeyVersion.axaml.cs @@ -0,0 +1,31 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Markup.Xaml; +using Avalonia.Threading; + + +using KeyVaultExplorer.ViewModels; + +namespace KeyVaultExplorer; + +public partial class CreateNewKeyVersion : UserControl +{ + public CreateNewKeyVersion() + { + InitializeComponent(); + DataContext = new CreateNewKeyVersionViewModel(); + } + + private void InputField_OnAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e) + { + // We will set the focus into our input field just after it got attached to the visual tree. + if (sender is InputElement inputElement) + { + Dispatcher.UIThread.InvokeAsync(() => + { + inputElement.Focus(NavigationMethod.Unspecified, KeyModifiers.None); + }); + } + } +} \ No newline at end of file