GroupPolicy.cs (4692B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="GroupPolicy.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.SharedLib.PolicySettings 8 { 9 using System; 10 using System.Collections.Generic; 11 12 using Microsoft.Win32; 13 using Microsoft.WinGet.SharedLib.Exceptions; 14 15 /// <summary> 16 /// Helper class to read Group Policies backed by registry store. 17 /// </summary> 18 public class GroupPolicy 19 { 20 private const string AppInstallerPolicyRegistryPath = "SOFTWARE\\Policies\\Microsoft\\Windows\\AppInstaller"; 21 22 private Dictionary<Policy, TogglePolicy> togglePolicyMap; 23 24 private GroupPolicy() 25 { 26 this.togglePolicyMap = new Dictionary<Policy, TogglePolicy>(); 27 } 28 29 /// <summary> 30 /// Gets an instance of GroupPolicy. 31 /// </summary> 32 /// <returns>An instance of type<see cref="GroupPolicy"/> class.</returns> 33 public static GroupPolicy GetInstance() 34 { 35 GroupPolicy groupPolicy = new GroupPolicy(); 36 37 try 38 { 39 List<TogglePolicy> togglePolicies = TogglePolicy.GetAllPolicies(); 40 groupPolicy.Load(togglePolicies); 41 42 return groupPolicy; 43 } 44 catch (Exception ex) 45 { 46 throw new GroupPolicyException(GroupPolicyFailureType.LoadError, ex); 47 } 48 } 49 50 /// <summary> 51 /// Gets the current of status of Policy. 52 /// </summary> 53 /// <param name="policy">Policy.</param> 54 /// <returns>Policy configuration status.</returns> 55 public PolicyState GetPolicyState(Policy policy) 56 { 57 if (!this.togglePolicyMap.ContainsKey(policy)) 58 { 59 throw new GroupPolicyException(policy, GroupPolicyFailureType.NotFound); 60 } 61 62 return this.togglePolicyMap[policy].State; 63 } 64 65 /// <summary> 66 /// Return status of is Policy enabled. 67 /// </summary> 68 /// <param name="policy">Policy.</param> 69 /// <returns>Boolean status indicates is Policy enabled.</returns> 70 public bool IsEnabled(Policy policy) 71 { 72 if (!this.togglePolicyMap.ContainsKey(policy)) 73 { 74 throw new GroupPolicyException(policy, GroupPolicyFailureType.NotFound); 75 } 76 77 TogglePolicy togglePolicy = this.togglePolicyMap[policy]; 78 79 if (togglePolicy.State == PolicyState.Enabled 80 || (togglePolicy.State == PolicyState.NotConfigured && togglePolicy.EnabledByDefault)) 81 { 82 return true; 83 } 84 85 return false; 86 } 87 88 /// <summary> 89 /// Loads the collection of TogglePolicy. 90 /// </summary> 91 /// <param name="policies">Enumerable collection of TogglePolicy.</param> 92 internal void Load(IEnumerable<TogglePolicy> policies) 93 { 94 using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(AppInstallerPolicyRegistryPath)) 95 { 96 foreach (TogglePolicy togglePolicy in policies) 97 { 98 // It is likely expected if none of the Windows Package Manager policies are configured i.e Not Configured. 99 if (regKey != null) 100 { 101 var policyValue = regKey.GetValue(togglePolicy.RegistryValueName); 102 103 RegistryValueKind valueKind = RegistryValueKind.None; 104 105 if (policyValue != null) 106 { 107 valueKind = regKey.GetValueKind(togglePolicy.RegistryValueName); 108 } 109 110 #pragma warning disable CS8604 // Possible null reference argument. 111 togglePolicy.SetValue(policyValue, valueKind); 112 #pragma warning restore CS8604 // Possible null reference argument. 113 } 114 115 if (!this.togglePolicyMap.ContainsKey(togglePolicy.PolicyType) 116 || (this.togglePolicyMap.ContainsKey(togglePolicy.PolicyType) && this.togglePolicyMap[togglePolicy.PolicyType] == null)) 117 { 118 this.togglePolicyMap.Add(togglePolicy.PolicyType, togglePolicy); 119 } 120 } 121 } 122 } 123 } 124 }