From a377b0ee354de04b4ee11cd823ba32f00a4f1183 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sat, 9 Apr 2016 14:24:09 -0500 Subject: [PATCH] (GH-596) Allow failure on invalid license Add feature that allows users to know when a license is invalid, expired, or missing from a machine. --- .../infrastructure.app/ApplicationParameters.cs | 1 + .../builders/ConfigurationBuilder.cs | 1 + .../configuration/ChocolateyConfiguration.cs | 1 + .../infrastructure.app/runners/GenericRunner.cs | 13 +++++++++++++ 4 files changed, 16 insertions(+) diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index e3391c3be7..a54b5a4b36 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -112,6 +112,7 @@ public static class Features public static readonly string UsePowerShellHost = "powershellHost"; public static readonly string LogEnvironmentValues = "logEnvironmentValues"; public static readonly string VirusCheck = "virusCheck"; + public static readonly string FailOnInvalidOrMissingLicense = "failOnInvalidOrMissingLicense"; } public static class Messages diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index e81ad9af35..8d7f9485f0 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -244,6 +244,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile config.Features.UsePowerShellHost = set_feature_flag(ApplicationParameters.Features.UsePowerShellHost, configFileSettings, defaultEnabled: true, description: "Use Chocolatey's built-in PowerShell host."); config.Features.LogEnvironmentValues = set_feature_flag(ApplicationParameters.Features.LogEnvironmentValues, configFileSettings, defaultEnabled: false, description: "Log Environment Values - will log values of environment before and after install (could disclose sensitive data)."); config.Features.VirusCheck = set_feature_flag(ApplicationParameters.Features.VirusCheck, configFileSettings, defaultEnabled: false, description: "Virus Check [licensed versions only] - perform virus checking on downloaded files."); + config.Features.FailOnInvalidOrMissingLicense = set_feature_flag(ApplicationParameters.Features.FailOnInvalidOrMissingLicense, configFileSettings, defaultEnabled: false, description: "Fail On Invalid Or Missing License - allows knowing when a license is expired or not applied to a machine."); config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass."); } diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index ab703a4452..5db1b955c9 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -340,6 +340,7 @@ public sealed class FeaturesConfiguration public bool UsePowerShellHost { get; set; } public bool LogEnvironmentValues { get; set; } public bool VirusCheck { get; set; } + public bool FailOnInvalidOrMissingLicense { get; set; } } //todo: retrofit other command configs this way diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 7a8c0583a1..e9ca1509ee 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -122,8 +122,17 @@ private void set_source_type(ChocolateyConfiguration config) this.Log().Debug(()=> "The source '{0}' evaluated to a '{1}' source type".format_with(config.Sources,sourceType.to_string())); } + public void fail_when_license_is_missing_or_invalid_if_requested(ChocolateyConfiguration config) + { + if (!config.Features.FailOnInvalidOrMissingLicense) return; + + if (!config.Information.IsLicensedVersion) throw new ApplicationException("License is missing or invalid."); + } + public void run(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) { + fail_when_license_is_missing_or_invalid_if_requested(config); + var command = find_command(config, container, isConsole, parseArgs); if(command != null) { @@ -134,6 +143,8 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons public IEnumerable list(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) { + fail_when_license_is_missing_or_invalid_if_requested(config); + var command = find_command(config, container, isConsole, parseArgs) as IListCommand; if (command == null) { @@ -152,6 +163,8 @@ public IEnumerable list(ChocolateyConfiguration config, Container containe public int count(ChocolateyConfiguration config, Container container, bool isConsole, Action parseArgs) { + fail_when_license_is_missing_or_invalid_if_requested(config); + var command = find_command(config, container, isConsole, parseArgs) as IListCommand; if (command == null) {