Skip to content

Commit

Permalink
Sync authentication with IDSDK login/logout events (#14653)
Browse files Browse the repository at this point in the history
* Fix PostDiff job

* auth sync

* comments

* Update SplashScreen.xaml.cs

* comments

* jesus PR changes
  • Loading branch information
zeusongit authored Nov 30, 2023
1 parent 45038c5 commit 626878d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 10 deletions.
17 changes: 15 additions & 2 deletions src/DynamoCore/Core/IDSDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Dynamo.Core
/// <summary>
/// The class to provide auth APIs for IDSDK related methods.
/// </summary>
public class IDSDKManager : IOAuth2AuthProvider, IOAuth2AccessTokenProvider
public class IDSDKManager : IOAuth2AuthProvider, IOAuth2AccessTokenProvider, IDisposable
{
/// <summary>
/// Used by the auth provider to request authentication.
Expand Down Expand Up @@ -228,8 +228,10 @@ private bool Initialize()
}

bool ret = GetClientIDAndServer(out idsdk_server server, out string client_id);
if (ret)
if (ret)
{
Client.LogoutCompleteEvent += AuthCompleteEventHandler;
Client.LoginCompleteEvent += AuthCompleteEventHandler;
ret = SetProductConfigs("Dynamo", server, client_id);
Client.SetServer(server);
return ret;
Expand All @@ -253,6 +255,11 @@ private bool Deinitialize()
}
return false;
}
public void Dispose()
{
Client.LoginCompleteEvent -= AuthCompleteEventHandler;
Client.LogoutCompleteEvent -= AuthCompleteEventHandler;
}
private bool GetClientIDAndServer(out idsdk_server server, out string client_id)
{
server = idsdk_server.IDSDK_PRODUCTION_SERVER;
Expand All @@ -273,6 +280,12 @@ private bool GetClientIDAndServer(out idsdk_server server, out string client_id)
}
return !string.IsNullOrEmpty(client_id);
}

// Event handler for LogoutCompleteEvent and LoginCompleteEvent that is thrown whenever the user's auth state changes.
private void AuthCompleteEventHandler(object sender, Client.TypedEventArgs e)
{
OnLoginStateChanged(LoginState);
}
#endregion
}
}
5 changes: 5 additions & 0 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,11 @@ public void Dispose()
{
SearchModel.ItemProduced -= SearchModel_ItemProduced;
}
//handle the disposal of IDSDK manager instance for sandbox
if (AuthenticationManager?.AuthProvider is IDSDKManager idsdkProvider)
{
idsdkProvider?.Dispose();
}
}

private void InitializeCustomNodeManager()
Expand Down
24 changes: 19 additions & 5 deletions src/DynamoCoreWpf/Controls/ShortcutToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
using System.Linq;
using System.Windows;
using System.Collections.Generic;
using System;

namespace Dynamo.UI.Controls
{
/// <summary>
/// An object which provides the data for the shortcut toolbar.
/// </summary>
public partial class ShortcutToolbar : UserControl
public partial class ShortcutToolbar : UserControl, IDisposable
{
private readonly ObservableCollection<ShortcutBarItem> shortcutBarItems;
private readonly ObservableCollection<ShortcutBarItem> shortcutBarRightSideItems;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ShortcutToolbar(DynamoViewModel dynamoViewModel)
authManager = dynamoViewModel.Model.AuthenticationManager;
if (authManager.IsLoggedInInitial())
{
authManager.LoginStateChanged += SignOutHandler;
authManager.LoginStateChanged += AuthChangeHandler;
}
else {
logoutOption.Visibility = Visibility.Collapsed;
Expand All @@ -73,14 +74,28 @@ private void ShortcutToolbar_Loaded(object sender, RoutedEventArgs e)
DynamoViewModel.OnRequestShorcutToolbarLoaded(RightMenu.ActualWidth);
}

private void SignOutHandler(LoginState status)
public void Dispose()
{
if(authManager != null)
{
authManager.LoginStateChanged -= AuthChangeHandler;
}
this.Loaded -= ShortcutToolbar_Loaded;
}

private void AuthChangeHandler(LoginState status)
{
if (status == LoginState.LoggedOut)
{
LoginButton.ToolTip = Wpf.Properties.Resources.SignInButtonContentToolTip;
txtSignIn.Text = Wpf.Properties.Resources.SignInButtonText;
logoutOption.Visibility = Visibility.Collapsed;
authManager.LoginStateChanged -= SignOutHandler;
}
else if (status == LoginState.LoggedIn)
{
txtSignIn.Text = authManager.Username;
logoutOption.Visibility = Visibility.Visible;
LoginButton.ToolTip = null;
}
}

Expand Down Expand Up @@ -115,7 +130,6 @@ private void LoginButton_OnClick(object sender, RoutedEventArgs e)
tb.Text = authManager.Username;
logoutOption.Visibility = Visibility.Visible;
LoginButton.ToolTip = null;
authManager.LoginStateChanged += SignOutHandler;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/Views/Core/DynamoView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2964,6 +2964,7 @@ public void Dispose()

// Removing the tab items list handler
dynamoViewModel.SideBarTabItems.CollectionChanged -= this.OnCollectionChanged;
shortcutBar?.Dispose();

if (fileTrustWarningPopup != null)
{
Expand Down
44 changes: 41 additions & 3 deletions src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Dynamo.Utilities;
using Dynamo.ViewModels;
using DynamoUtilities;
using Greg.AuthProviders;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

Expand Down Expand Up @@ -129,9 +130,15 @@ public void OnRequestStaticSplashScreen()
}

/// <summary>
/// Constructor
/// Stores the value that indicates if the SignIn Button will be enabled(default) or not
/// </summary>
public SplashScreen()
bool enableSignInButton;

/// <summary>
/// Splash Screen Constructor.
/// <paramref name="enableSignInButton"/> Indicates if the SignIn Button will be enabled(default) or not.
/// </summary>
public SplashScreen(bool enableSignInButton = true)
{
InitializeComponent();

Expand All @@ -149,6 +156,7 @@ public SplashScreen()
RequestImportSettings = ImportSettings;
RequestSignIn = SignIn;
RequestSignOut = SignOut;
this.enableSignInButton = enableSignInButton;
}

private void DynamoModel_LanguageDetected()
Expand Down Expand Up @@ -230,12 +238,16 @@ private void LaunchDynamo(bool isCheckboxChecked)
{
viewModel.PreferenceSettings.EnableStaticSplashScreen = !isCheckboxChecked;
}
StaticSplashScreenReady -= OnStaticScreenReady;
Close();
dynamoView?.Show();
dynamoView?.Activate();
}

private void OnLoginStateChanged(LoginState state)
{
HandleSignInStatusChange(authManager.IsLoggedIn());
}

/// <summary>
/// Once main window is initialized, Dynamic Splash screen should finish loading
/// </summary>
Expand All @@ -250,6 +262,7 @@ private void OnStaticScreenReady()
// If user is launching Dynamo for the first time or chose to always show splash screen, display it. Otherwise, display Dynamo view directly.
if (viewModel.PreferenceSettings.IsFirstRun || viewModel.PreferenceSettings.EnableStaticSplashScreen)
{
authManager.LoginStateChanged += OnLoginStateChanged;
SetSignInStatus(authManager.IsLoggedInInitial());
SetLoadingDone();
}
Expand Down Expand Up @@ -346,6 +359,7 @@ internal async void SetLoadingDone()
{
await webView.CoreWebView2.ExecuteScriptAsync($"window.setLoadingDone()");
await webView.CoreWebView2.ExecuteScriptAsync($"window.setTotalLoadingTime(\"{Wpf.Properties.Resources.SplashScreenTotalLoadingTimeLabel} {totalLoadingTime}ms\")");
SetSignInEnable(enableSignInButton);
}
}

Expand Down Expand Up @@ -393,6 +407,28 @@ await webView.CoreWebView2.ExecuteScriptAsync("window.setSignInStatus({" +
}
}

/// <summary>
/// Handle the login status changes on splash screen.
/// </summary>
internal async void HandleSignInStatusChange(bool status)
{
if (webView?.CoreWebView2 != null)
{
await webView.CoreWebView2.ExecuteScriptAsync(@$"window.handleSignInStateChange({{""status"": ""{status}""}})");
}
}

/// <summary>
/// Enable or disable the SignIn button on splash screen.
/// </summary>
/// <param name="enabled"></param>
internal async void SetSignInEnable(bool enabled)
{
if (webView?.CoreWebView2 != null)
{
await webView.CoreWebView2.ExecuteScriptAsync(@$"window.setEnableSignInButton({{""enable"": ""{enabled}""}})");
}
}
/// <summary>
/// Setup the values for all labels on splash screen using resources
/// </summary>
Expand Down Expand Up @@ -509,6 +545,8 @@ protected override void OnClosed(EventArgs e)

DynamoModel.RequestUpdateLoadBarStatus -= DynamoModel_RequestUpdateLoadBarStatus;
DynamoModel.LanguageDetected -= DynamoModel_LanguageDetected;
StaticSplashScreenReady -= OnStaticScreenReady;
authManager.LoginStateChanged -= OnLoginStateChanged;
webView.Dispose();
webView = null;

Expand Down

0 comments on commit 626878d

Please sign in to comment.