Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,18 @@ private void OpenFromJson(object parameters)
this.ShowStartPage = false; // Hide start page if there's one.
}

/// <summary>
/// Resets trust warning state when a preemptively shown popup should be dismissed.
/// </summary>
/// <param name="trustWarningWasShown">True when this open/insert flow displayed the trust warning before command execution.</param>
private void ResetTrustWarningStateIfNeeded(bool trustWarningWasShown)
{
if (!trustWarningWasShown || FileTrustViewModel == null) return;

FileTrustViewModel.ShowWarningPopup = false;
RunSettings.ForceBlockRun = false;
}

/// <summary>
/// Open a definition or workspace.
/// For most cases, parameters variable refers to the file path to open
Expand All @@ -2203,6 +2215,7 @@ private void Open(object parameters)
fileContents = string.Empty;
bool forceManualMode = false;
bool isTemplate = false;
bool trustWarningWasShown = false;
try
{
if (parameters is Tuple<string, bool> packedParams)
Expand All @@ -2229,25 +2242,33 @@ private void Open(object parameters)
&& !DynamoModel.IsTestMode
&& !PreferenceSettings.DisableTrustWarnings
&& FileTrustViewModel != null;
RunSettings.ForceBlockRun = displayTrustWarning;
// Execute graph open command
ExecuteCommand(new DynamoModel.OpenFileCommand(filePath, forceManualMode, isTemplate));

// Apply annotation updates based on the preference setting
RefreshAnnotationDescriptions();

// Only show trust warning popop when current opened workspace is homeworkspace and not custom node workspace
if (displayTrustWarning && (currentWorkspaceViewModel?.IsHomeSpace ?? false))
if (displayTrustWarning)
{
// Skip these when opening dyf
FileTrustViewModel.AllowOneTimeTrust = false;
FileTrustViewModel.DynFileDirectoryName = directoryName;
FileTrustViewModel.ShowWarningPopup = true;
(HomeSpaceViewModel as HomeWorkspaceViewModel)?.UpdateRunStatusMsgBasedOnStates();
trustWarningWasShown = true;
}

RunSettings.ForceBlockRun = displayTrustWarning;
// Execute graph open command
ExecuteCommand(new DynamoModel.OpenFileCommand(filePath, forceManualMode, isTemplate));

if (trustWarningWasShown)
{
// Hide the preemptively shown popup if trust is now satisfied (trusted path or warnings disabled).
ShowHideFileTrustWarningIfCurrentWorkspaceTrusted();
}

// Apply annotation updates based on the preference setting
RefreshAnnotationDescriptions();
}
catch (Exception e)
{
ResetTrustWarningStateIfNeeded(trustWarningWasShown);

if (!DynamoModel.IsTestMode)
{
string commandString = String.Format(Resources.MessageErrorOpeningFileGeneral);
Expand Down Expand Up @@ -2301,6 +2322,7 @@ private void Insert(object parameters)
filePath = string.Empty;
fileContents = string.Empty;
bool forceManualMode = true;
bool trustWarningWasShown = false;
try
{
if (parameters is Tuple<string, bool> packedParams)
Expand All @@ -2321,24 +2343,30 @@ private void Insert(object parameters)
&& !DynamoModel.IsTestMode
&& !PreferenceSettings.DisableTrustWarnings
&& FileTrustViewModel != null;
RunSettings.ForceBlockRun = displayTrustWarning;
// Execute graph open command
ExecuteCommand(new DynamoModel.InsertFileCommand(filePath, forceManualMode));

this.FitViewCommand.Execute(null);

// Only show trust warning popup when current opened workspace is homeworkspace and not custom node workspace
if (displayTrustWarning && (currentWorkspaceViewModel?.IsHomeSpace ?? false))
if (displayTrustWarning)
{
// Skip these when opening dyf
FileTrustViewModel.AllowOneTimeTrust = false;
FileTrustViewModel.DynFileDirectoryName = directoryName;
FileTrustViewModel.ShowWarningPopup = true;
(HomeSpaceViewModel as HomeWorkspaceViewModel)?.UpdateRunStatusMsgBasedOnStates();
trustWarningWasShown = true;
}

RunSettings.ForceBlockRun = displayTrustWarning;
// Execute graph open command
ExecuteCommand(new DynamoModel.InsertFileCommand(filePath, forceManualMode));

this.FitViewCommand.Execute(null);
if (trustWarningWasShown)
{
ShowHideFileTrustWarningIfCurrentWorkspaceTrusted();
}
}
catch (Exception e)
{
ResetTrustWarningStateIfNeeded(trustWarningWasShown);

if (!DynamoModel.IsTestMode)
{
string commandString = String.Format(Resources.MessageErrorOpeningFileGeneral);
Expand Down
60 changes: 60 additions & 0 deletions test/DynamoCoreWpfTests/DynamoViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,66 @@ public void OpeningWorkspaceWithTclsrustWarning()
DynamoModel.IsTestMode = true;
}

[Test]
public void WhenOpeningWorkspaceThenTrustWarningIsShownBeforeCommand()
{
DynamoModel.IsTestMode = false;
bool? popupStateWhenOpenStarts = null;

void OnCommandStarting(DynamoModel.RecordableCommand command)
{
if (command is DynamoModel.OpenFileCommand)
{
popupStateWhenOpenStarts = ViewModel.FileTrustViewModel.ShowWarningPopup;
}
}

Model.CommandStarting += OnCommandStarting;
try
{
Open(@"core\CustomNodes\TestAdd.dyn");
}
finally
{
Model.CommandStarting -= OnCommandStarting;
DynamoModel.IsTestMode = true;
}

Assert.IsTrue(popupStateWhenOpenStarts.GetValueOrDefault(false));
}

[Test]
public void WhenInsertingWorkspaceThenTrustWarningIsShownBeforeCommand()
{
DynamoModel.IsTestMode = false;
bool? popupStateWhenInsertStarts = null;

void OnCommandStarting(DynamoModel.RecordableCommand command)
{
if (command is DynamoModel.InsertFileCommand)
{
popupStateWhenInsertStarts = ViewModel.FileTrustViewModel.ShowWarningPopup;
}
}

Model.CommandStarting += OnCommandStarting;
try
{
var insertMethod = typeof(DynamoViewModel).GetMethod("Insert", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.NotNull(insertMethod);

var insertPath = Path.Combine(GetTestDirectory(ExecutingDirectory), @"core\CustomNodes\TestAdd.dyn");
insertMethod.Invoke(ViewModel, new object[] { insertPath });
}
finally
{
Model.CommandStarting -= OnCommandStarting;
DynamoModel.IsTestMode = true;
}

Assert.IsTrue(popupStateWhenInsertStarts.GetValueOrDefault(false));
}

[Test]
public void TestHomeWorkspaceClosedBeforeCustomNode()
{
Expand Down