Skip to content

Commit

Permalink
Fix refreshing of validation messages about missing PFX files (#112)
Browse files Browse the repository at this point in the history
Resolves #112
  • Loading branch information
marcinotorowski committed Apr 23, 2021
1 parent 7b7fd4a commit ca38766
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,10 @@ public CertificateSelectorViewModel(
}

this.Store = new ChangeableProperty<CertificateSource>(newStore);
this.Store.ValueChanged += StoreOnValueChanged;
this.PfxPath = new ChangeableFileProperty("Path to PFX file", interactionService, signConfig.PfxPath?.Resolved)
this.Store.ValueChanged += this.StoreOnValueChanged;
this.PfxPath = new ChangeableFileProperty("Path to PFX file", interactionService, signConfig.PfxPath?.Resolved, this.ValidatePfxPath)
{
IsValidated = false,
Filter = new DialogFilterBuilder("*.pfx").BuildFilter(),
Validators = new []
{
ChangeableFileProperty.ValidatePathAndPresence
}
Filter = new DialogFilterBuilder("*.pfx").BuildFilter()
};

SecureString initialSecurePassword = null;
Expand Down Expand Up @@ -114,29 +109,32 @@ public CertificateSelectorViewModel(
this.SelectedPersonalCertificate = new ValidatedChangeableProperty<CertificateViewModel>("Selected certificate", false, this.ValidateSelectedCertificate);
this.PersonalCertificates = new AsyncProperty<ObservableCollection<CertificateViewModel>>(this.LoadPersonalCertificates(signConfig.Thumbprint, !signConfig.ShowAllCertificates));
this.ShowAllCertificates = new ChangeableProperty<bool>(signConfig.ShowAllCertificates);

switch (this.Store.CurrentValue)
{
case CertificateSource.Pfx:
this.PfxPath.IsValidated = true;
break;
case CertificateSource.DeviceGuard:
this.DeviceGuard.IsValidated = true;
break;
case CertificateSource.Personal:
this.SelectedPersonalCertificate.IsValidated = true;
break;
}

this.AddChildren(this.SelectedPersonalCertificate, this.PfxPath, this.TimeStamp, this.Password, this.DeviceGuard, this.Store, this.ShowAllCertificates);

this.ShowAllCertificates.ValueChanged += async (sender, args) =>
this.ShowAllCertificates.ValueChanged += async (_, args) =>
{
await this.PersonalCertificates.Load(this.LoadPersonalCertificates(this.SelectedPersonalCertificate.CurrentValue?.Model.Thumbprint, !(bool)args.NewValue)).ConfigureAwait(false);
this.OnPropertyChanged(nameof(this.SelectedPersonalCertificate));
};
}


private void StoreOnValueChanged(object sender, EventArgs e)
{
this.PfxPath.Validate();
this.DeviceGuard.Validate();
this.SelectedPersonalCertificate.Validate();
}

public string ValidatePfxPath(string value)
{
if (this.Store.CurrentValue != CertificateSource.Pfx)
{
return null;
}

return ChangeableFileProperty.ValidatePathAndPresence(value);
}

public AsyncProperty<ObservableCollection<CertificateViewModel>> PersonalCertificates { get; }

public ValidatedChangeableProperty<CertificateViewModel> SelectedPersonalCertificate { get; }
Expand Down Expand Up @@ -292,33 +290,6 @@ private string ValidateTimestamp(string value)

return "The URL must have a protocol.";
}

private void StoreOnValueChanged(object sender, ValueChangedEventArgs e)
{
switch ((CertificateSource) e.NewValue)
{
case CertificateSource.Unknown:
this.PfxPath.IsValidated = false;
this.SelectedPersonalCertificate.IsValidated = false;
this.DeviceGuard.IsValidated = false;
break;
case CertificateSource.Personal:
this.PfxPath.IsValidated = false;
this.SelectedPersonalCertificate.IsValidated = true;
this.DeviceGuard.IsValidated = false;
break;
case CertificateSource.Pfx:
this.PfxPath.IsValidated = true;
this.SelectedPersonalCertificate.IsValidated = false;
this.DeviceGuard.IsValidated = false;
break;
case CertificateSource.DeviceGuard:
this.PfxPath.IsValidated = false;
this.SelectedPersonalCertificate.IsValidated = false;
this.DeviceGuard.IsValidated = true;
break;
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,7 @@ public string this[string columnName]
}
}

public event EventHandler<ValueChangedEventArgs<string>> ValidationStatusChanged;

protected override void PostSetValue()
{
base.PostSetValue();
this.Validate();
}

private void Validate()
public void Validate()
{
var oldValidationMessage = this.ValidationMessage;
if (!this.IsValidated || this.Validators == null || !this.Validators.Any())
Expand Down Expand Up @@ -206,20 +198,25 @@ private void Validate()
this.ValidationMessage = msg;
}
}

// ReSharper disable once InvertIf
if (oldValidationMessage != this.ValidationMessage)
{
var validationChanged = this.ValidationStatusChanged;
if (validationChanged != null)
{
validationChanged(this, new ValueChangedEventArgs<string>(this.ValidationMessage));
}
validationChanged?.Invoke(this, new ValueChangedEventArgs<string>(this.ValidationMessage));
}

this.OnPropertyChanged(nameof(this.CurrentValue));
this.OnPropertyChanged(nameof(this.Error));
this.OnPropertyChanged(nameof(this.IsValid));
}

public event EventHandler<ValueChangedEventArgs<string>> ValidationStatusChanged;

protected override void PostSetValue()
{
base.PostSetValue();
this.Validate();
}
}
}

0 comments on commit ca38766

Please sign in to comment.