From 3dfd37e74e241541cda92a251b1a9163e3c0bc3a Mon Sep 17 00:00:00 2001 From: jesusalvino <96534278+jesusalvino@users.noreply.github.com> Date: Mon, 3 Oct 2022 10:22:01 -0500 Subject: [PATCH] Fix Trusted Message behavior (#13355) * Fix Trusted Message behavior * Extract Business Logic as a function * Moving API to Preferences * Adding unit test --- .../Configuration/PreferenceSettings.cs | 50 ++++++++++++++++++- .../ViewModels/Core/DynamoViewModel.cs | 20 +++++++- .../ViewModels/Menu/TrustedPathViewModel.cs | 25 +++++++++- .../Views/FileTrust/FileTrustWarning.xaml.cs | 4 +- .../Views/Menu/PreferencesView.xaml.cs | 24 ++++++++- .../Configuration/PreferenceSettingsTests.cs | 25 +++++++++- 6 files changed, 137 insertions(+), 11 deletions(-) diff --git a/src/DynamoCore/Configuration/PreferenceSettings.cs b/src/DynamoCore/Configuration/PreferenceSettings.cs index 9b691d54086..eae3736d1d6 100644 --- a/src/DynamoCore/Configuration/PreferenceSettings.cs +++ b/src/DynamoCore/Configuration/PreferenceSettings.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -924,6 +924,52 @@ public bool IsTrustedLocation(string location) } } + + /// + /// Different ways to ask the user about display the Trusted location + /// + public enum AskForTrustedLocationResult + { + /// + /// Ask for the Trusted location + /// + Ask, + /// + /// Don't ask about the Trusted location + /// + DontAsk, + /// + /// Unable to ask about the Trusted location + /// + UnableToAsk + } + + /// + /// AskForTrustedLocation function + /// + /// + /// + /// + /// + /// + /// + public static AskForTrustedLocationResult AskForTrustedLocation(bool isOpenedFile, bool isFileInTrustedLocation, bool isHomeSpace, bool isShowStartPage, bool isDisableTrustWarnings) + { + AskForTrustedLocationResult result = AskForTrustedLocationResult.UnableToAsk; + if (isOpenedFile) + { + if (isHomeSpace && !isShowStartPage && !isDisableTrustWarnings && !isFileInTrustedLocation) + { + result = AskForTrustedLocationResult.Ask; + } + else + { + result = AskForTrustedLocationResult.DontAsk; + } + } + return result; + } + #endregion #region ILogSource @@ -948,4 +994,4 @@ public List StaticFields() return typeof(PreferenceSettings).GetMembers(BindingFlags.Static | BindingFlags.NonPublic).OfType().Select(field => field.Name).ToList(); } } -} \ No newline at end of file +} diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index 32a9e3e0ba2..8cedce63e72 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -3095,7 +3095,25 @@ private bool AskUserToSaveWorkspacesOrCancel(bool allowCancel = true) return false; } return true; - } + } + + /// + /// Check if the current file is located in a Trusted Location in order to display to the User the proper message + /// + public void CheckCurrentFileInTrustedLocation() + { + PreferenceSettings.AskForTrustedLocationResult askToTheUser = + PreferenceSettings.AskForTrustedLocation(CurrentSpaceViewModel.FileName.Length > 0, + CurrentSpaceViewModel.FileName.Length > 0 ? PreferenceSettings.IsTrustedLocation(Path.GetDirectoryName(CurrentSpaceViewModel.FileName)) : false, + (currentWorkspaceViewModel?.IsHomeSpace ?? false), + ShowStartPage, + model.PreferenceSettings.DisableTrustWarnings); + + if (askToTheUser == PreferenceSettings.AskForTrustedLocationResult.Ask) { + + FileTrustViewModel.AllowOneTimeTrust = false; + } + } #endregion } diff --git a/src/DynamoCoreWpf/ViewModels/Menu/TrustedPathViewModel.cs b/src/DynamoCoreWpf/ViewModels/Menu/TrustedPathViewModel.cs index e41ec21a9a0..ef0f4b943cd 100644 --- a/src/DynamoCoreWpf/ViewModels/Menu/TrustedPathViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Menu/TrustedPathViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; @@ -119,9 +119,10 @@ private void InsertPath() return; } - TrustedLocations.Insert(TrustedLocations.Count, args.Path); + TrustedLocations.Insert(TrustedLocations.Count, args.Path); CommitChanges(null); RaiseCanExecuteChanged(); + RaisePropertyChanged(nameof(TrustedPathViewModel.Action.Insert)); } private void ShowFileDialog(TrustedPathEventArgs e) @@ -154,6 +155,7 @@ private void UpdatePathAt(int index) TrustedLocations[index] = args.Path; CommitChanges(null); + RaisePropertyChanged(nameof(TrustedPathViewModel.Action.Update)); } private void RemovePathAt(int index) @@ -161,6 +163,7 @@ private void RemovePathAt(int index) TrustedLocations.RemoveAt(index); CommitChanges(null); RaiseCanExecuteChanged(); + RaisePropertyChanged(nameof(TrustedPathViewModel.Action.Remove)); } private void CommitChanges(object param) @@ -173,5 +176,23 @@ internal void InitializeTrustedLocations() TrustedLocations = new ObservableCollection(settings?.TrustedLocations ?? new List()); RaisePropertyChanged(string.Empty); } + /// + /// Actions the user can do to the model since it's a List + /// + public struct Action + { + /// + /// Insert a new item + /// + public string Insert; + /// + /// Update an existing Item + /// + public string Update; + /// + /// Remove an Item + /// + public string Remove; + } } } diff --git a/src/DynamoCoreWpf/Views/FileTrust/FileTrustWarning.xaml.cs b/src/DynamoCoreWpf/Views/FileTrust/FileTrustWarning.xaml.cs index cb41ff75d12..cf936893df1 100644 --- a/src/DynamoCoreWpf/Views/FileTrust/FileTrustWarning.xaml.cs +++ b/src/DynamoCoreWpf/Views/FileTrust/FileTrustWarning.xaml.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Threading; @@ -162,8 +162,6 @@ private void YesButton_Click(object sender, RoutedEventArgs e) (dynViewModel.HomeSpaceViewModel as HomeWorkspaceViewModel).CurrentNotificationMessage = Properties.Resources.RunReady; (dynViewModel.HomeSpaceViewModel as HomeWorkspaceViewModel).CurrentNotificationLevel = NotificationLevel.Mild; } - - fileTrustWarningViewModel.DynFileDirectoryName = string.Empty; } /// diff --git a/src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs b/src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs index 6b275c0dc3a..9aadc30f2f2 100644 --- a/src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs +++ b/src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows; @@ -80,6 +81,23 @@ private void SetupPreferencesViewModel(DynamoViewModel dynamoViewModel) // Init all package filters dynamoViewModel.PreferencesViewModel.InitPackageListFilters(); + + dynamoViewModel.PreferencesViewModel.TrustedPathsViewModel.PropertyChanged += TrustedPathsViewModel_PropertyChanged; + } + + /// + /// Evaluates if the user interacts over the Trusted Locations + /// + /// + /// + private void TrustedPathsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + { + List actions = typeof(TrustedPathViewModel.Action).GetFields().Select(a => a.Name).ToList(); + + if (actions.Contains(e.PropertyName)) + { + dynViewModel.CheckCurrentFileInTrustedLocation(); + } } /// @@ -116,6 +134,8 @@ private void CloseButton_Click(object sender, RoutedEventArgs e) RunGraphWhenScaleFactorUpdated(); + dynViewModel.PreferencesViewModel.TrustedPathsViewModel.PropertyChanged -= TrustedPathsViewModel_PropertyChanged; + Close(); } @@ -435,4 +455,4 @@ private void exportTextBlock_MouseLeftButtonDown(object sender, MouseButtonEvent } } } -} \ No newline at end of file +} diff --git a/test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs b/test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs index 161e841e7cc..235b9adac21 100644 --- a/test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs +++ b/test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using Dynamo.Configuration; using Dynamo.Models; @@ -341,5 +341,28 @@ public void TestImportCopySettings() var checkEquality = comparePrefenceSettings(defaultSettings, newSettings); Assert.IsTrue(checkEquality.SamePropertyValues.Count == checkEquality.Properties.Count); } + + [Test] + [Category("UnitTests")] + public void TestAskForTrustedLocation() + { + //Settings + bool isOpenedFile = true; + bool isHomeSpace = true; + bool isShowStartPage = false; + bool isFileInTrustedLocation = false; + bool isDisableTrustWarnings = false; + + // getting result + PreferenceSettings.AskForTrustedLocationResult result = PreferenceSettings.AskForTrustedLocation( + isOpenedFile, + isFileInTrustedLocation, + isHomeSpace, + isShowStartPage, + isDisableTrustWarnings); + + // checking the result + Assert.IsTrue(result == PreferenceSettings.AskForTrustedLocationResult.Ask, $"Conditions info: is opened file : {isOpenedFile} | is file in trusted location : {isFileInTrustedLocation} | is home space : {isHomeSpace} | is show Start page : {isShowStartPage} | is disable trust warnings : {isDisableTrustWarnings}"); + } } }