Skip to content

Commit

Permalink
Fix Trusted Message behavior (#13355)
Browse files Browse the repository at this point in the history
* Fix Trusted Message behavior

* Extract Business Logic as a function

* Moving API to Preferences

* Adding unit test
  • Loading branch information
jesusalvino authored and QilongTang committed Jul 12, 2023
1 parent 339cced commit 6bc26de
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 10 deletions.
50 changes: 48 additions & 2 deletions src/DynamoCore/Configuration/PreferenceSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -900,6 +900,52 @@ public bool IsTrustedLocation(string location)
}

}

/// <summary>
/// Different ways to ask the user about display the Trusted location
/// </summary>
public enum AskForTrustedLocationResult
{
/// <summary>
/// Ask for the Trusted location
/// </summary>
Ask,
/// <summary>
/// Don't ask about the Trusted location
/// </summary>
DontAsk,
/// <summary>
/// Unable to ask about the Trusted location
/// </summary>
UnableToAsk
}

/// <summary>
/// AskForTrustedLocation function
/// </summary>
/// <param name="isOpenedFile"></param>
/// <param name="isHomeSpace"></param>
/// <param name="isShowStartPage"></param>
/// <param name="isDisableTrustWarnings"></param>
/// <param name="isFileInTrustedLocation"></param>
/// <returns></returns>
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
Expand All @@ -915,4 +961,4 @@ internal void OnMessageLogged(ILogMessage msg)
}
#endregion
}
}
}
18 changes: 18 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3097,6 +3097,24 @@ private bool AskUserToSaveWorkspacesOrCancel(bool allowCancel = true)
return true;
}

/// <summary>
/// Check if the current file is located in a Trusted Location in order to display to the User the proper message
/// </summary>
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
}
}
25 changes: 23 additions & 2 deletions src/DynamoCoreWpf/ViewModels/Menu/TrustedPathViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -154,13 +155,15 @@ private void UpdatePathAt(int index)

TrustedLocations[index] = args.Path;
CommitChanges(null);
RaisePropertyChanged(nameof(TrustedPathViewModel.Action.Update));
}

private void RemovePathAt(int index)
{
TrustedLocations.RemoveAt(index);
CommitChanges(null);
RaiseCanExecuteChanged();
RaisePropertyChanged(nameof(TrustedPathViewModel.Action.Remove));
}

private void CommitChanges(object param)
Expand All @@ -172,5 +175,23 @@ internal void InitializeTrustedLocations()
{
TrustedLocations = new ObservableCollection<string>(settings?.TrustedLocations ?? new List<string>());
}
/// <summary>
/// Actions the user can do to the model since it's a List
/// </summary>
public struct Action
{
/// <summary>
/// Insert a new item
/// </summary>
public string Insert;
/// <summary>
/// Update an existing Item
/// </summary>
public string Update;
/// <summary>
/// Remove an Item
/// </summary>
public string Remove;
}
}
}
4 changes: 1 addition & 3 deletions src/DynamoCoreWpf/Views/FileTrust/FileTrustWarning.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down
24 changes: 22 additions & 2 deletions src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -76,6 +77,23 @@ private void SetupPreferencesViewModel(DynamoViewModel dynamoViewModel)

// Init all package filters
dynamoViewModel.PreferencesViewModel.InitPackageListFilters();

dynamoViewModel.PreferencesViewModel.TrustedPathsViewModel.PropertyChanged += TrustedPathsViewModel_PropertyChanged;
}

/// <summary>
/// Evaluates if the user interacts over the Trusted Locations
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TrustedPathsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
List<string> actions = typeof(TrustedPathViewModel.Action).GetFields().Select(a => a.Name).ToList();

if (actions.Contains(e.PropertyName))
{
dynViewModel.CheckCurrentFileInTrustedLocation();
}
}

/// <summary>
Expand Down Expand Up @@ -112,6 +130,8 @@ private void CloseButton_Click(object sender, RoutedEventArgs e)

RunGraphWhenScaleFactorUpdated();

dynViewModel.PreferencesViewModel.TrustedPathsViewModel.PropertyChanged -= TrustedPathsViewModel_PropertyChanged;

Close();
}

Expand Down Expand Up @@ -370,4 +390,4 @@ private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e
e.Handled = true;
}
}
}
}
25 changes: 24 additions & 1 deletion test/DynamoCoreTests/Configuration/PreferenceSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using Dynamo.Configuration;
using Dynamo.Models;
Expand Down Expand Up @@ -199,5 +199,28 @@ public void TestSerializationTrustedLocations()

Assert.IsTrue(settingsLoaded.IsTrustedLocation(Path.GetTempPath()));
}

[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}");
}
}
}

0 comments on commit 6bc26de

Please sign in to comment.