winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PolicyEnforcedInstanceInitializer.cs (1318B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 
      4 namespace Microsoft.Management.Deployment.Projection.Initializers
      5 {
      6     using Microsoft.WinGet.SharedLib.Exceptions;
      7     using Microsoft.WinGet.SharedLib.PolicySettings;
      8 
      9     /// <summary>
     10     /// An abstract base that enforces group policy before creating derived class instance.
     11     /// </summary>
     12     public abstract class PolicyEnforcedInstanceInitializer : IInstanceInitializer
     13     {
     14         /// <summary>
     15         /// CLSID context.
     16         /// </summary>
     17         public abstract ClsidContext Context { get; }
     18 
     19         /// <summary>
     20         /// Create instance of the provided type.
     21         /// </summary>
     22         /// <typeparam name="T">Projected class type.</typeparam>
     23         /// <returns>Instance of the provided type.</returns>
     24         public T CreateInstance<T>() where T : new()
     25         {
     26             GroupPolicy groupPolicy = GroupPolicy.GetInstance();
     27 
     28             if (!groupPolicy.IsEnabled(Policy.WinGet))
     29             {
     30                 throw new GroupPolicyException(Policy.WinGet, GroupPolicyFailureType.BlockedByPolicy);
     31             }
     32 
     33             return this.CreateInstanceInternal<T>();
     34         }
     35 
     36         protected abstract T CreateInstanceInternal<T>() where T : new();
     37     }
     38 }