TogglePolicy.cs (7666B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="TogglePolicy.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 namespace Microsoft.WinGet.SharedLib.PolicySettings 7 { 8 using System; 9 using System.Collections.Generic; 10 11 using Microsoft.Win32; 12 using Microsoft.WinGet.SharedLib.Resources; 13 14 /// <summary> 15 /// A policyType that acts as a toggle to enable or disable a feature. 16 /// They are backed by a DWORD policyValue with values 0 and 1. 17 /// </summary> 18 public class TogglePolicy 19 { 20 private readonly Policy policyType; 21 private readonly bool isEnabledByDefault; 22 private readonly string registryValueName; 23 private readonly string resourceString; 24 25 private PolicyState state; 26 private int? value; 27 28 /// <summary> 29 /// Initializes a new instance of the <see cref="TogglePolicy"/> class. 30 /// </summary> 31 /// <param name="policy">PolicyType.</param> 32 /// <param name="registryValueName">RegistryValue Name.</param> 33 /// <param name="policyResourceString">PolicyType ResourceString.</param> 34 /// <param name="isEnabledByDefault">Is EnabledByDefault.</param> 35 internal TogglePolicy(Policy policy, string registryValueName, string policyResourceString, bool isEnabledByDefault = true) 36 { 37 this.policyType = policy; 38 this.registryValueName = registryValueName; 39 this.resourceString = policyResourceString; 40 this.isEnabledByDefault = isEnabledByDefault; 41 this.state = PolicyState.NotConfigured; 42 } 43 44 /// <summary> 45 /// Gets PolicyType. 46 /// </summary> 47 public Policy PolicyType 48 { 49 get { return this.policyType; } 50 } 51 52 /// <summary> 53 /// Gets Resource string associated with policy. 54 /// </summary> 55 public string ResourceString 56 { 57 get { return this.resourceString; } 58 } 59 60 /// <summary> 61 /// Gets Policy State. 62 /// </summary> 63 public PolicyState State 64 { 65 get { return this.state; } 66 } 67 68 /// <summary> 69 /// Gets a value indicating whether gets Is policy EnabledByDefault. 70 /// </summary> 71 internal bool EnabledByDefault 72 { 73 get { return this.isEnabledByDefault; } 74 } 75 76 /// <summary> 77 /// Gets Policy RegistryValue Name. 78 /// </summary> 79 internal string RegistryValueName 80 { 81 get { return this.registryValueName; } 82 } 83 84 /// <summary> 85 /// Gets value of the policy. 86 /// </summary> 87 internal int? Value 88 { 89 get { return this.value; } 90 } 91 92 /// <summary> 93 /// Creates an instance of TogglePolicy class. 94 /// </summary> 95 /// <param name="policy">Policy.</param> 96 /// <returns>An instance of <see cref="TogglePolicy"/> class.</returns> 97 /// <exception cref="ArgumentException">Argument exception is return if not matching Policy type passed.</exception> 98 internal static TogglePolicy Create(Policy policy) 99 { 100 switch (policy) 101 { 102 case Policy.WinGet: 103 return new TogglePolicy(policy, "EnableAppInstaller", GroupPolicyResource.PolicyEnableWinGet); 104 case Policy.Settings: 105 return new TogglePolicy(policy, "EnableSettings", GroupPolicyResource.PolicyEnableWinGetSettings); 106 case Policy.ExperimentalFeatures: 107 return new TogglePolicy(policy, "EnableExperimentalFeatures", GroupPolicyResource.PolicyEnableExperimentalFeatures); 108 case Policy.LocalManifestFiles: 109 return new TogglePolicy(policy, "EnableLocalManifestFiles", GroupPolicyResource.PolicyEnableLocalManifests); 110 case Policy.HashOverride: 111 return new TogglePolicy(policy, "EnableHashOverride", GroupPolicyResource.PolicyEnableHashOverride); 112 case Policy.LocalArchiveMalwareScanOverride: 113 return new TogglePolicy(policy, "EnableLocalArchiveMalwareScanOverride", GroupPolicyResource.PolicyEnableLocalArchiveMalwareScanOverride); 114 case Policy.DefaultSource: 115 return new TogglePolicy(policy, "EnableDefaultSource", GroupPolicyResource.PolicyEnableDefaultSource); 116 case Policy.MSStoreSource: 117 return new TogglePolicy(policy, "EnableMicrosoftStoreSource", GroupPolicyResource.PolicyEnableMSStoreSource); 118 case Policy.AdditionalSources: 119 return new TogglePolicy(policy, "EnableAdditionalSources", GroupPolicyResource.PolicyAdditionalSources); 120 case Policy.AllowedSources: 121 return new TogglePolicy(policy, "EnableAllowedSources", GroupPolicyResource.PolicyAllowedSources); 122 case Policy.BypassCertificatePinningForMicrosoftStore: 123 return new TogglePolicy(policy, "EnableBypassCertificatePinningForMicrosoftStore", GroupPolicyResource.PolicyEnableBypassCertificatePinningForMicrosoftStore); 124 case Policy.WinGetCommandLineInterfaces: 125 return new TogglePolicy(policy, "EnableWindowsPackageManagerCommandLineInterfaces", GroupPolicyResource.PolicyEnableWindowsPackageManagerCommandLineInterfaces); 126 case Policy.Configuration: 127 return new TogglePolicy(policy, "EnableWindowsPackageManagerConfiguration", GroupPolicyResource.PolicyEnableWinGetConfiguration); 128 case Policy.McpServer: 129 return new TogglePolicy(policy, "EnableWindowsPackageManagerMcpServer", GroupPolicyResource.PolicyEnableMcpServer); 130 default: 131 throw new ArgumentException(null, nameof(policy)); 132 } 133 } 134 135 /// <summary> 136 /// Creates a list of TogglePolicy instances. 137 /// </summary> 138 /// <returns>List of type <see cref="TogglePolicy"/> class.</returns> 139 internal static List<TogglePolicy> GetAllPolicies() 140 { 141 List<TogglePolicy> togglePolicies = new List<TogglePolicy>(); 142 143 foreach (Policy policy in Enum.GetValues(typeof(Policy))) 144 { 145 togglePolicies.Add(Create(policy)); 146 } 147 148 return togglePolicies; 149 } 150 151 /// <summary> 152 /// Sets value and state of the policy. 153 /// </summary> 154 /// <param name="policyValue">PolicyValue object.</param> 155 /// <param name="registryValueKind">RegistryValueKind.</param> 156 internal void SetValue(object policyValue, RegistryValueKind registryValueKind) 157 { 158 if (registryValueKind != RegistryValueKind.DWord 159 || policyValue == null) 160 { 161 this.state = PolicyState.NotConfigured; 162 this.value = null; 163 164 return; 165 } 166 167 this.value = (int)policyValue; 168 169 if (this.value != 0) 170 { 171 this.state = PolicyState.Enabled; 172 } 173 else 174 { 175 this.state = PolicyState.Disabled; 176 } 177 } 178 } 179 }