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 Oct 3, 2022
1 parent f50c788 commit 3dfd37e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 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 @@ -924,6 +924,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 @@ -948,4 +994,4 @@ public List<string> StaticFields()
return typeof(PreferenceSettings).GetMembers(BindingFlags.Static | BindingFlags.NonPublic).OfType<FieldInfo>().Select(field => field.Name).ToList();
}
}
}
}
20 changes: 19 additions & 1 deletion src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3095,7 +3095,25 @@ private bool AskUserToSaveWorkspacesOrCancel(bool allowCancel = true)
return false;
}
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
}
Expand Down
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 @@ -173,5 +176,23 @@ internal void InitializeTrustedLocations()
TrustedLocations = new ObservableCollection<string>(settings?.TrustedLocations ?? new List<string>());
RaisePropertyChanged(string.Empty);
}
/// <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.IO;
using System.Linq;
using System.Windows;
Expand Down Expand Up @@ -80,6 +81,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 @@ -116,6 +134,8 @@ private void CloseButton_Click(object sender, RoutedEventArgs e)

RunGraphWhenScaleFactorUpdated();

dynViewModel.PreferencesViewModel.TrustedPathsViewModel.PropertyChanged -= TrustedPathsViewModel_PropertyChanged;

Close();
}

Expand Down Expand Up @@ -435,4 +455,4 @@ private void exportTextBlock_MouseLeftButtonDown(object sender, MouseButtonEvent
}
}
}
}
}
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 @@ -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}");
}
}
}

0 comments on commit 3dfd37e

Please sign in to comment.