Skip to content
Open
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
93 changes: 93 additions & 0 deletions src/Common/Helpers/GPOHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Win32;
using Serilog;

namespace WindowsAdvancedSettings.Common.Helpers;

public class GPOHelper
{
private static readonly ILogger _log = Log.ForContext("SourceContext", nameof(GPOHelper));

private enum GpoRuleConfigured
{
WrongValue = -3, // The policy is set to an unrecognized value
Unavailable = -2, // Couldn't access registry
NotConfigured = -1, // Policy is not configured
Disabled = 0, // Policy is disabled
Enabled = 1, // Policy is enabled
}

// Registry path where gpo policy values are stored
private const string PoliciesScopeMachine = "HKEY_LOCAL_MACHINE";
private const string PoliciesPath = @"\SOFTWARE\Policies\Microsoft\Windows\WindowsAdvancedSettings";

// Registry value names
private const string PolicyConfigureEnabledWindowsAdvancedSettings = "ConfigureEnabledWindowsAdvancedSettings";

private static GpoRuleConfigured GetConfiguredValue(string registryValueName)
{
try
{
var rawValue = Registry.GetValue(
keyName: PoliciesScopeMachine + PoliciesPath,
valueName: registryValueName,
defaultValue: GpoRuleConfigured.NotConfigured);

_log.Debug($"Registry value {registryValueName} set to {rawValue}");

// Value will be null if the subkey specified by keyName does not exist.
if (rawValue == null)
{
return GpoRuleConfigured.NotConfigured;
}
else if (rawValue is not int && rawValue is not GpoRuleConfigured)
{
return GpoRuleConfigured.WrongValue;
}
else
{
return (GpoRuleConfigured)rawValue;
}
}
catch (System.Security.SecurityException)
{
// The user does not have the permissions required to read from the registry key.
return GpoRuleConfigured.Unavailable;
}
catch (System.IO.IOException)
{
// The RegistryKey that contains the specified value has been marked for deletion.
return GpoRuleConfigured.Unavailable;
}
catch (System.ArgumentException)
{
// keyName does not begin with a valid registry root.
return GpoRuleConfigured.NotConfigured;
}
}

private static bool EvaluateConfiguredValue(string registryValueName, GpoRuleConfigured defaultValue)
{
var configuredValue = GetConfiguredValue(registryValueName);
if (configuredValue < 0)
{
if (configuredValue != GpoRuleConfigured.NotConfigured)
{
// Only log an error if a configuration was attempted but was incorrect. NotConfigured is expected state.
_log.Error($"Registry value {registryValueName} set to {configuredValue}, using default {defaultValue} instead.");
}

configuredValue = defaultValue;
}

return configuredValue == GpoRuleConfigured.Enabled;
}

public static bool GetConfiguredEnabledWindowsAdvancedSettingsValue()
{
var defaultValue = GpoRuleConfigured.Enabled;
return EvaluateConfiguredValue(PolicyConfigureEnabledWindowsAdvancedSettings, defaultValue);
}
}
32 changes: 32 additions & 0 deletions src/Common/WindowsAdvancedSettings.admx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
<policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
<policyNamespaces>
<target prefix="devhome" namespace="Microsoft.Policies.DevHome" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still using the DevHome name here?

</policyNamespaces>
<resources minRequiredRevision="0.1900"/>
<supportedOn>
<definitions>
<definition name="SUPPORTED_DEVHOME_0_1900" displayName="$(string.SUPPORTED_DEVHOME_0_1900)"/>
</definitions>
</supportedOn>
<categories>
<category name="DevHome" displayName="$(string.DevHome)" />
</categories>

<policies>

<!--The name (id) of the policy is different from the valueName to sort it as first policy in edit dialog. The order is sorted alphabetically based on the "name" property.-->
<policy name="ConfigureAllDevHomeEnabledState" class="Both" displayName="$(string.ConfigureAllDevHomeEnabledState)" explainText="$(string.ConfigureAllDevHomeEnabledStateDescription)" key="Software\Policies\DevHome" valueName="ConfigureEnabledDevHome">
<parentCategory ref="DevHome" />
<supportedOn ref="SUPPORTED_DEVHOME_0_1901" />
<enabledValue>
<decimal value="1" />
</enabledValue>
<disabledValue>
<decimal value="0" />
</disabledValue>
</policy>
</policies>
</policyDefinitions>
32 changes: 32 additions & 0 deletions src/Common/gpo/assets/WindowsAdvancedSettings.admx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two copies of this file?

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
<policyDefinitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
<policyNamespaces>
<target prefix="windowsadvancedsettings" namespace="Microsoft.Policies.WindowsAdvancedSettings" />
</policyNamespaces>
<resources minRequiredRevision="0.1900"/>
<supportedOn>
<definitions>
<definition name="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900" displayName="$(string.SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900)"/>
</definitions>
</supportedOn>
<categories>
<category name="WindowsAdvancedSettings" displayName="$(string.WindowsAdvancedSettings)" />
</categories>

<policies>

<!--The name (id) of the policy is different from the valueName to sort it as first policy in edit dialog. The order is sorted alphabetically based on the "name" property.-->
<policy name="ConfigureAllWindowsAdvancedSettingsEnabledState" class="Both" displayName="$(string.ConfigureAllWindowsAdvancedSettingsEnabledState)" explainText="$(string.ConfigureAllWindowsAdvancedSettingsEnabledStateDescription)" key="Software\Policies\Microsoft\Windows\WindowsAdvancedSettings" valueName="ConfigureEnabledWindowsAdvancedSettings">
<parentCategory ref="DevHome" />
<supportedOn ref="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900" />
<enabledValue>
<decimal value="1" />
</enabledValue>
<disabledValue>
<decimal value="0" />
</disabledValue>
</policy>
</policies>
</policyDefinitions>
30 changes: 30 additions & 0 deletions src/Common/gpo/assets/en-US/WindowsAdvancedSettings.adml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation.
Licensed under the MIT License. -->
<policyDefinitionResources xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" revision="0.1900" schemaVersion="1.0" xmlns="http://schemas.microsoft.com/GroupPolicy/2006/07/PolicyDefinitions">
<displayName>Windows Advanced Settings</displayName>
<description>Windows Advanced Settings</description>
<resources>
<stringTable>
<string id="WindowsAdvancedSettings">Microsoft Windows Advanced Settings</string>

<string id="SUPPORTED_WINDOWSADVANCEDSETTINGS_0_1900">Windows Advanced Settings version 0.1900.* or later</string>

<string id="ConfigureAllWindowsAdvancedSettingsEnabledStateDescription">
This policy configures the enabled state for Windows Advanced Settings.

If you enable this setting, Windows Advanced Settings will be always enabled and the user won't be able to disable it.

If you disable this setting, Windows Advanced Settings will be always disabled and the user won't be able to enable it.

If you don't configure this setting, users are able to enable or disable Windows Advanced Settings.

This policy will override any enabled state policies for individual Windows Advanced Settings features.
</string>

<string id="ConfigureAllWindowsAdvancedSettingsEnabledState">Configure Windows Advanced Settings enabled state</string>
</stringTable>

</resources>
</policyDefinitionResources>

7 changes: 7 additions & 0 deletions src/FileExplorerGitIntegration/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ private static void AppActivationRedirected(object? sender, Microsoft.Windows.Ap

private static void HandleCOMServerActivation()
{
var gpoPolicyEnabled = GPOHelper.GetConfiguredEnabledWindowsAdvancedSettingsValue();
if (!gpoPolicyEnabled)
{
Log.Information($"Windows Advanced Settings is disabled by policy, exiting.");
return;
}

Log.Information($"Activating COM Server");

RepositoryCache cache = new RepositoryCache();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using FileExplorerSourceControlIntegration;
using Microsoft.Internal.Windows.DevHome.Helpers.FileExplorer;
using Serilog;
using Windows.Storage;

namespace AdvancedSettings.Tester;
namespace FileExplorerSourceControlIntegration;

public enum ProviderType
{
Dev,
Release,
}

internal class ConfigureFolderPath
public class ConfigureFolderPath
{
private static readonly string _releaseProviderGuidString = "1212F95B-257E-414e-B44F-F26634BD2627";
private static readonly string _devProviderGuidString = "40FE4D6E-C9A0-48B4-A83E-AAA1D002C0D5";
private static readonly Guid _releaseProvider = new(_releaseProviderGuidString);
private static readonly Guid _devProvider = new(_devProviderGuidString);

public static void DisplayStatus()
{
foreach (var folderInfo in ExtraFolderPropertiesWrapper.GetRegisteredFolderInfos())
{
var providerName = GetProviderName(folderInfo.HandlerClsid);
Console.WriteLine($"{providerName}: {folderInfo.RootFolderPath} {{{folderInfo.HandlerClsid}}} {folderInfo.AppId}");
}
}
public static readonly Guid ReleaseProvider = new(_releaseProviderGuidString);
public static readonly Guid DevProvider = new(_devProviderGuidString);
public static readonly Guid CurrentProvider = typeof(SourceControlProvider).GUID;

public static void AddPath(string provider, string path)
{
try
{
var providerType = (ProviderType)Enum.Parse(typeof(ProviderType), provider, true);
AddPath(providerType, path);
var providerGuid = GetProvider(providerType);
AddPath(providerGuid, path);
}
catch (Exception ex)
{
Log.Error(ex, $"Invalid provider: {provider}");
}
}

public static void AddPath(ProviderType providerType, string path)
public static void AddPath(Guid provider, string path)
{
var provider = GetProvider(providerType);
Console.WriteLine($"Registering source folder: {path} for provider {providerType}");
try
{
if (!Directory.Exists(path))
Expand Down Expand Up @@ -79,28 +68,50 @@ public static void RemovePath(string path)
}
}

private static Guid GetProvider(ProviderType providerType)
public static Guid GetProvider(ProviderType providerType)
{
return providerType switch
{
ProviderType.Release => _releaseProvider,
_ => _devProvider,
ProviderType.Release => ReleaseProvider,
_ => DevProvider,
};
}

private static string GetProviderName(Guid guid)
public static void RemoveAllForProvider(string provider)
{
if (guid.Equals(_devProvider))
try
{
return "DEV";
var providerType = (ProviderType)Enum.Parse(typeof(ProviderType), provider, true);
var providerGuid = GetProvider(providerType);
RemoveAllForProvider(providerGuid);
}
else if (guid.Equals(_releaseProvider))
catch (Exception ex)
{
return "REL";
Log.Error(ex, $"Invalid provider: {provider}");
}
else
}

public static void RemoveAllForProvider(Guid provider)
{
Log.Information($"Removing all registered folders for provider: {provider}");
try
{
foreach (var folderInfo in ExtraFolderPropertiesWrapper.GetRegisteredFolderInfos())
{
if (folderInfo.HandlerClsid.Equals(provider))
{
RemovePath(folderInfo.RootFolderPath);
}
}
}
catch (Exception ex)
{
return "UNK";
Log.Error(ex, $"An exception occurred while enumerating the folder properties.");
}
}

public static void RemoveAllForCurrentProvider()
{
RemoveAllForProvider(typeof(SourceControlProvider).GUID);
}
}
8 changes: 8 additions & 0 deletions src/FileExplorerSourceControlIntegration/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public static async Task Main([System.Runtime.InteropServices.WindowsRuntime.Rea

private static void HandleCOMServerActivation()
{
var gpoPolicyEnabled = GPOHelper.GetConfiguredEnabledWindowsAdvancedSettingsValue();
if (!gpoPolicyEnabled)
{
Log.Information($"Windows Advanced Settings is disabled by policy, removing all registered entries for this provider and exiting.");
ConfigureFolderPath.RemoveAllForCurrentProvider();
return;
}

Log.Information($"Activating COM Server");
using var sourceControlProviderServer = new SourceControlProviderServer();
var sourceControlProviderInstance = new SourceControlProvider();
Expand Down
Loading