Skip to content

Commit

Permalink
(chocolateyGH-99) Warn when admin is not elevated
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed Feb 12, 2015
1 parent a40064f commit d793663
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace chocolatey.console
using infrastructure.app.builders;
using infrastructure.app.configuration;
using infrastructure.app.runners;
using infrastructure.commandline;
using infrastructure.configuration;
using infrastructure.extractors;
using infrastructure.filesystem;
Expand Down Expand Up @@ -66,9 +67,7 @@ private static void Main(string[] args)
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion));
#endif
}

remove_old_chocolatey_exe(fileSystem);


if (config.HelpRequested)
{
pause_execution_if_debug();
Expand All @@ -80,6 +79,9 @@ 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);

//refactor - thank goodness this is temporary, cuz manifest resource streams are dumb
Expand Down Expand Up @@ -135,6 +137,30 @@ private static void Main(string[] args)
}
}

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

0 comments on commit d793663

Please sign in to comment.