Skip to content

Commit

Permalink
(GH-160) No admin warning unless command request
Browse files Browse the repository at this point in the history
Do not issue an administrator warning with a pause unless the command
being run requests that it may need administrative rights.
  • Loading branch information
ferventcoder committed Apr 13, 2015
1 parent 3b3ea75 commit 1baa1ff
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 26 deletions.
25 changes: 0 additions & 25 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private static void Main(string[] args)
"chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string()));
//"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine));

warn_when_admin_needs_elevation(config);
remove_old_chocolatey_exe(fileSystem);

LicenseValidation.validate(fileSystem);
Expand Down Expand Up @@ -180,30 +179,6 @@ private static void trap_exit_scenarios(ChocolateyConfiguration config)
ExitScenarioHandler.SetHandler();
}

private static void warn_when_admin_needs_elevation(ChocolateyConfiguration config)
{
// NOTE: blended options may not have been fully initialized yet
if (!config.PromptForConfirmation) return;

if (!config.Information.IsProcessElevated && config.Information.IsUserAdministrator)
{
var selection = InteractivePrompt.prompt_for_confirmation(@"
Chocolatey detected you are not running from an elevated command shell
(cmd/powershell). You may experience errors - many functions/packages
require admin rights. Only advanced users should run choco w/out an
elevated shell. When you open the command shell, you should ensure
that you do so with ""Run as Administrator"" selected.
Do you want to continue?", new[] {"yes", "no"}, "no", requireAnswer: true);

if (selection.is_equal_to("no"))
{
pause_execution_if_debug();
Environment.Exit(-1);
}
}
}

private static void remove_old_chocolatey_exe(IFileSystem fileSystem)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,10 @@ public void run(ChocolateyConfiguration configuration)
_configSettingsService.set_api_key(configuration);
}
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@ public void run(ChocolateyConfiguration configuration)
break;
}
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,10 @@ public void run(ChocolateyConfiguration configuration)
{
_packageService.install_run(configuration);
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,10 @@ public void run(ChocolateyConfiguration configuration)
{
_packageService.list_run(configuration, logResults: true);
}

public bool may_require_admin_access()
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,10 @@ public void run(ChocolateyConfiguration configuration)
{
_templateService.generate(configuration);
}

public bool may_require_admin_access()
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,10 @@ public void run(ChocolateyConfiguration configuration)
{
_packageService.pack_run(configuration);
}

public bool may_require_admin_access()
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,10 @@ public void set_pin(IPackageManager packageManager, ChocolateyConfiguration conf
pkgInfo.IsPinned = config.PinCommand.Command == PinCommandType.add;
_packageInfoService.save_package_information(pkgInfo);
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,10 @@ public void run(ChocolateyConfiguration configuration)
{
_packageService.push_run(configuration);
}

public bool may_require_admin_access()
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,10 @@ public void run(ChocolateyConfiguration configuration)
break;
}
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,10 @@ public void run(ChocolateyConfiguration configuration)
{
_packageService.uninstall_run(configuration);
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,10 @@ public void run(ChocolateyConfiguration configuration)
overwriteExisting: configuration.Force,
logOutput: true);
}

public bool may_require_admin_access()
{
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,10 @@ public virtual void run(ChocolateyConfiguration configuration)
{
_packageService.upgrade_run(configuration);
}

public bool may_require_admin_access()
{
return true;
}
}
}
31 changes: 30 additions & 1 deletion src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace chocolatey.infrastructure.app.runners
using SimpleInjector;
using adapters;
using attributes;
using commandline;
using configuration;
using infrastructure.commands;
using logging;
Expand Down Expand Up @@ -49,6 +50,11 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
}
else
{
if (command.may_require_admin_access())
{
warn_when_admin_needs_elevation(config);
}

if (parseArgs != null)
{
parseArgs.Invoke(command);
Expand Down Expand Up @@ -104,6 +110,29 @@ now be in a bad state. Only official builds are to be trusted.
}
}
}
}

public void warn_when_admin_needs_elevation(ChocolateyConfiguration config)
{
// NOTE: blended options may not have been fully initialized yet
if (!config.PromptForConfirmation) return;

if (!config.Information.IsProcessElevated && config.Information.IsUserAdministrator)
{
var selection = InteractivePrompt.prompt_for_confirmation(@"
Chocolatey detected you are not running from an elevated command shell
(cmd/powershell). You may experience errors - many functions/packages
require admin rights. Only advanced users should run choco w/out an
elevated shell. When you open the command shell, you should ensure
that you do so with ""Run as Administrator"" selected.
Do you want to continue?", new[] { "yes", "no" }, "no", requireAnswer: true);

if (selection.is_equal_to("no"))
{
Environment.Exit(-1);
}
}
}

}
}
6 changes: 6 additions & 0 deletions src/chocolatey/infrastructure/commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ public interface ICommand
/// </summary>
/// <param name="config">The configuration.</param>
void run(ChocolateyConfiguration config);

/// <summary>
/// This command may require admin rights
/// </summary>
/// <returns></returns>
bool may_require_admin_access();
}
}

0 comments on commit 1baa1ff

Please sign in to comment.