From b34bdcd4bb80b7e49311d51c46ec5cf657fdbfb2 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Wed, 26 Oct 2022 16:19:56 +0100 Subject: [PATCH] Add GPO --- doc/gpo/assets/PowerToys.admx | 10 +++++++ doc/gpo/assets/en-US/PowerToys.adml | 1 + installer/PowerToysSetup/Product.wxs | 2 +- src/common/GPOWrapper/GPOWrapper.cpp | 4 +++ src/common/GPOWrapper/GPOWrapper.h | 1 + src/common/GPOWrapper/GPOWrapper.idl | 1 + src/common/utils/gpo.h | 6 ++++ .../FileLocksmithExt/PowerToysModule.cpp | 6 ++++ .../FileLocksmith/FileLocksmithExt/Settings.h | 6 ++++ .../FileLocksmith/FileLocksmithUI/App.xaml.cs | 8 +++++ .../FileLocksmithUI/FileLocksmithUI.csproj | 9 ++++++ .../ViewModels/FileLocksmithViewModel.cs | 29 +++++++++++++++++-- .../Settings.UI/Views/FileLocksmithPage.xaml | 12 +++++++- .../Views/FileLocksmithPage.xaml.cs | 2 +- .../BugReportTool/ReportGPOValues.cpp | 1 + 15 files changed, 93 insertions(+), 5 deletions(-) rename src/settings-ui/{Settings.UI.Library => Settings.UI}/ViewModels/FileLocksmithViewModel.cs (61%) diff --git a/doc/gpo/assets/PowerToys.admx b/doc/gpo/assets/PowerToys.admx index 1ae32fcb79..03446af73d 100644 --- a/doc/gpo/assets/PowerToys.admx +++ b/doc/gpo/assets/PowerToys.admx @@ -57,6 +57,16 @@ + + + + + + + + + + diff --git a/doc/gpo/assets/en-US/PowerToys.adml b/doc/gpo/assets/en-US/PowerToys.adml index d53ee7c8e6..9a07cd42f9 100644 --- a/doc/gpo/assets/en-US/PowerToys.adml +++ b/doc/gpo/assets/en-US/PowerToys.adml @@ -32,6 +32,7 @@ If you don't configure this setting, users are able to disable or enable the uti Awake: Configure enabled state Color Picker: Configure enabled state FancyZones: Configure enabled state + File Locksmith: Configure enabled state SVG file preview: Configure enabled state Markdown file preview: Configure enabled state Source code file preview: Configure enabled state diff --git a/installer/PowerToysSetup/Product.wxs b/installer/PowerToysSetup/Product.wxs index f993852df9..c1198d101b 100644 --- a/installer/PowerToysSetup/Product.wxs +++ b/installer/PowerToysSetup/Product.wxs @@ -117,7 +117,7 @@ - + diff --git a/src/common/GPOWrapper/GPOWrapper.cpp b/src/common/GPOWrapper/GPOWrapper.cpp index b3cd0be71e..4489c4ef2c 100644 --- a/src/common/GPOWrapper/GPOWrapper.cpp +++ b/src/common/GPOWrapper/GPOWrapper.cpp @@ -20,6 +20,10 @@ namespace winrt::PowerToys::GPOWrapper::implementation { return (GpoRuleConfigured)powertoys_gpo::getConfiguredFancyZonesEnabledValue(); } + GpoRuleConfigured GPOWrapper::GetConfiguredFileLocksmithEnabledValue() + { + return (GpoRuleConfigured)powertoys_gpo::getConfiguredFileLocksmithEnabledValue(); + } GpoRuleConfigured GPOWrapper::GetConfiguredSvgPreviewEnabledValue() { return (GpoRuleConfigured)powertoys_gpo::getConfiguredSvgPreviewEnabledValue(); diff --git a/src/common/GPOWrapper/GPOWrapper.h b/src/common/GPOWrapper/GPOWrapper.h index 47079ee781..25b8d87445 100644 --- a/src/common/GPOWrapper/GPOWrapper.h +++ b/src/common/GPOWrapper/GPOWrapper.h @@ -11,6 +11,7 @@ namespace winrt::PowerToys::GPOWrapper::implementation static GpoRuleConfigured GetConfiguredAwakeEnabledValue(); static GpoRuleConfigured GetConfiguredColorPickerEnabledValue(); static GpoRuleConfigured GetConfiguredFancyZonesEnabledValue(); + static GpoRuleConfigured GetConfiguredFileLocksmithEnabledValue(); static GpoRuleConfigured GetConfiguredSvgPreviewEnabledValue(); static GpoRuleConfigured GetConfiguredMarkdownPreviewEnabledValue(); static GpoRuleConfigured GetConfiguredMonacoPreviewEnabledValue(); diff --git a/src/common/GPOWrapper/GPOWrapper.idl b/src/common/GPOWrapper/GPOWrapper.idl index 2b25ba772f..f316b63fe1 100644 --- a/src/common/GPOWrapper/GPOWrapper.idl +++ b/src/common/GPOWrapper/GPOWrapper.idl @@ -15,6 +15,7 @@ namespace PowerToys static GpoRuleConfigured GetConfiguredAwakeEnabledValue(); static GpoRuleConfigured GetConfiguredColorPickerEnabledValue(); static GpoRuleConfigured GetConfiguredFancyZonesEnabledValue(); + static GpoRuleConfigured GetConfiguredFileLocksmithEnabledValue(); static GpoRuleConfigured GetConfiguredSvgPreviewEnabledValue(); static GpoRuleConfigured GetConfiguredMarkdownPreviewEnabledValue(); static GpoRuleConfigured GetConfiguredMonacoPreviewEnabledValue(); diff --git a/src/common/utils/gpo.h b/src/common/utils/gpo.h index 3bac6bb856..19b772d550 100644 --- a/src/common/utils/gpo.h +++ b/src/common/utils/gpo.h @@ -23,6 +23,7 @@ namespace powertoys_gpo { const std::wstring POLICY_CONFIGURE_ENABLED_AWAKE = L"ConfigureEnabledUtilityAwake"; const std::wstring POLICY_CONFIGURE_ENABLED_COLOR_PICKER = L"ConfigureEnabledUtilityColorPicker"; const std::wstring POLICY_CONFIGURE_ENABLED_FANCYZONES = L"ConfigureEnabledUtilityFancyZones"; + const std::wstring POLICY_CONFIGURE_ENABLED_FILE_LOCKSMITH = L"ConfigureEnabledUtilityFileLocksmith"; const std::wstring POLICY_CONFIGURE_ENABLED_SVG_PREVIEW = L"ConfigureEnabledUtilityFileExplorerSVGPreview"; const std::wstring POLICY_CONFIGURE_ENABLED_MARKDOWN_PREVIEW = L"ConfigureEnabledUtilityFileExplorerMarkdownPreview"; const std::wstring POLICY_CONFIGURE_ENABLED_MONACO_PREVIEW = L"ConfigureEnabledUtilityFileExplorerMonacoPreview"; @@ -119,6 +120,11 @@ namespace powertoys_gpo { return getConfiguredValue(POLICY_CONFIGURE_ENABLED_FANCYZONES); } + inline gpo_rule_configured_t getConfiguredFileLocksmithEnabledValue() + { + return getConfiguredValue(POLICY_CONFIGURE_ENABLED_FILE_LOCKSMITH); + } + inline gpo_rule_configured_t getConfiguredSvgPreviewEnabledValue() { return getConfiguredValue(POLICY_CONFIGURE_ENABLED_SVG_PREVIEW); diff --git a/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp b/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp index 5cb1093c4c..707c79e868 100644 --- a/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp +++ b/src/modules/FileLocksmith/FileLocksmithExt/PowerToysModule.cpp @@ -34,6 +34,12 @@ public: return constants::nonlocalizable::PowerToyKey; } + // Return the configured status for the gpo policy for the module + virtual powertoys_gpo::gpo_rule_configured_t gpo_policy_enabled_configuration() override + { + return powertoys_gpo::getConfiguredFileLocksmithEnabledValue(); + } + // Return JSON with the configuration options. // These are the settings shown on the settings page along with their current values. virtual bool get_config(_Out_ PWSTR buffer, _Out_ int* buffer_size) override diff --git a/src/modules/FileLocksmith/FileLocksmithExt/Settings.h b/src/modules/FileLocksmith/FileLocksmithExt/Settings.h index 40f6134e62..7dfd28fe14 100644 --- a/src/modules/FileLocksmith/FileLocksmithExt/Settings.h +++ b/src/modules/FileLocksmith/FileLocksmithExt/Settings.h @@ -1,6 +1,7 @@ #pragma once #include "pch.h" +#include class FileLocksmithSettings { @@ -9,6 +10,11 @@ public: inline bool GetEnabled() { + auto gpoSetting = powertoys_gpo::getConfiguredFileLocksmithEnabledValue(); + if (gpoSetting == powertoys_gpo::gpo_rule_configured_enabled) + return true; + if (gpoSetting == powertoys_gpo::gpo_rule_configured_disabled) + return false; Reload(); return settings.enabled; } diff --git a/src/modules/FileLocksmith/FileLocksmithUI/App.xaml.cs b/src/modules/FileLocksmith/FileLocksmithUI/App.xaml.cs index 1ee14e03cd..0df16f085b 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/App.xaml.cs +++ b/src/modules/FileLocksmith/FileLocksmithUI/App.xaml.cs @@ -32,6 +32,14 @@ namespace FileLocksmithUI /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs args) { + if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredFileLocksmithEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled) + { + // TODO: Add logging. + // Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); + Environment.Exit(0); // Current.Exit won't work until there's a window opened. + return; + } + _window = new MainWindow(Environment.GetCommandLineArgs().Contains("--elevated")); _window.Activate(); } diff --git a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithUI.csproj b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithUI.csproj index 0add2a0c5c..d08e39c286 100644 --- a/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithUI.csproj +++ b/src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithUI.csproj @@ -31,7 +31,15 @@ + + + PowerToys.GPOWrapper + $(OutDir) + false + + + @@ -56,6 +64,7 @@ + diff --git a/src/settings-ui/Settings.UI.Library/ViewModels/FileLocksmithViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs similarity index 61% rename from src/settings-ui/Settings.UI.Library/ViewModels/FileLocksmithViewModel.cs rename to src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs index 03997074d2..dc598a3caa 100644 --- a/src/settings-ui/Settings.UI.Library/ViewModels/FileLocksmithViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs @@ -3,10 +3,12 @@ // See the LICENSE file in the project root for more information. using System; +using global::PowerToys.GPOWrapper; +using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Helpers; using Microsoft.PowerToys.Settings.UI.Library.Interfaces; -namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels +namespace Microsoft.PowerToys.Settings.UI.ViewModels { public class FileLocksmithViewModel : Observable { @@ -22,7 +24,17 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels GeneralSettingsConfig = settingsRepository.SettingsConfig; - _isFileLocksmithEnabled = GeneralSettingsConfig.Enabled.FileLocksmith; + _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredFileLocksmithEnabledValue(); + if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) + { + // Get the enabled state from GPO. + _enabledStateIsGPOConfigured = true; + _isFileLocksmithEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled; + } + else + { + _isFileLocksmithEnabled = GeneralSettingsConfig.Enabled.FileLocksmith; + } // set the callback functions value to hangle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; @@ -33,6 +45,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels get => _isFileLocksmithEnabled; set { + if (_enabledStateIsGPOConfigured) + { + // If it's GPO configured, shouldn't be able to change this state. + return; + } + if (_isFileLocksmithEnabled != value) { _isFileLocksmithEnabled = value; @@ -49,8 +67,15 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels } } + public bool IsEnabledGpoConfigured + { + get => _enabledStateIsGPOConfigured; + } + private Func SendConfigMSG { get; } + private GpoRuleConfigured _enabledGpoRuleConfiguration; + private bool _enabledStateIsGPOConfigured; private bool _isFileLocksmithEnabled; } } diff --git a/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml b/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml index e9c6b80e83..0019f03f18 100644 --- a/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml +++ b/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml @@ -7,14 +7,19 @@ mc:Ignorable="d" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" xmlns:controls="using:Microsoft.PowerToys.Settings.UI.Controls" + xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters" AutomationProperties.LandmarkType="Main"> + + + + - + @@ -22,6 +27,11 @@ + diff --git a/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml.cs b/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml.cs index 94a8de0903..5a191ce9ff 100644 --- a/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml.cs +++ b/src/settings-ui/Settings.UI/Views/FileLocksmithPage.xaml.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using Microsoft.PowerToys.Settings.UI.Library; -using Microsoft.PowerToys.Settings.UI.Library.ViewModels; +using Microsoft.PowerToys.Settings.UI.ViewModels; using Microsoft.UI.Xaml.Controls; namespace Microsoft.PowerToys.Settings.UI.Views diff --git a/tools/BugReportTool/BugReportTool/ReportGPOValues.cpp b/tools/BugReportTool/BugReportTool/ReportGPOValues.cpp index 256cb679d0..2950a289ab 100644 --- a/tools/BugReportTool/BugReportTool/ReportGPOValues.cpp +++ b/tools/BugReportTool/BugReportTool/ReportGPOValues.cpp @@ -32,6 +32,7 @@ void ReportGPOValues(const std::filesystem::path& tmpDir) report << "getConfiguredAwakeEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredAwakeEnabledValue()) << std::endl; report << "getConfiguredColorPickerEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredColorPickerEnabledValue()) << std::endl; report << "getConfiguredFancyZonesEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredFancyZonesEnabledValue()) << std::endl; + report << "getConfiguredFileLocksmithEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredFileLocksmithEnabledValue()) << std::endl; report << "getConfiguredSvgPreviewEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredSvgPreviewEnabledValue()) << std::endl; report << "getConfiguredMarkdownPreviewEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMarkdownPreviewEnabledValue()) << std::endl; report << "getConfiguredMonacoPreviewEnabledValue: " << gpo_rule_configured_to_string(powertoys_gpo::getConfiguredMonacoPreviewEnabledValue()) << std::endl;