From 08f8a2feb2f72e5f44924a3485ee79e5cd7840a3 Mon Sep 17 00:00:00 2001 From: Richard Simpson Date: Tue, 31 Jan 2017 17:39:26 -0600 Subject: [PATCH 1/7] (GH-371) Enable self-service mode When using self-service installs, a feature in Chocolatey for Business, it provides a background agent that actually performs the installations. Chocolatey itself handles passing through the proper information to the background service so that installations can occur. The GUI should notice the feature is switched on and adjust to allow non-administrators to run certain commands in the GUI without attempting to elevate. Original commit from @RichiCoder1. --- Source/ChocolateyGui/Bootstrapper.cs | 26 +++++++- Source/ChocolateyGui/ChocolateyGui.csproj | 2 +- Source/ChocolateyGui/Elevation.cs | 62 +++++++++++++++++++ Source/ChocolateyGui/Resources/Controls.xaml | 50 ++++++++++----- .../ChocolateyRemotePackageService.cs | 16 ++++- .../Startup/ChocolateyGuiModule.cs | 1 + Source/ChocolateyGui/Views/PackageView.xaml | 22 ++++--- 7 files changed, 149 insertions(+), 30 deletions(-) create mode 100644 Source/ChocolateyGui/Elevation.cs diff --git a/Source/ChocolateyGui/Bootstrapper.cs b/Source/ChocolateyGui/Bootstrapper.cs index 1e0e7fccc..8fa49e5e7 100644 --- a/Source/ChocolateyGui/Bootstrapper.cs +++ b/Source/ChocolateyGui/Bootstrapper.cs @@ -7,12 +7,16 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; using System.Windows; using Autofac; using Caliburn.Micro; using CefSharp; +using chocolatey; using ChocolateyGui.Properties; +using ChocolateyGui.Services; using ChocolateyGui.Startup; using ChocolateyGui.Utilities; using ChocolateyGui.ViewModels; @@ -89,10 +93,26 @@ protected override void Configure() Internationalization.Initialize(); } - protected override void OnStartup(object sender, StartupEventArgs e) + protected override async void OnStartup(object sender, StartupEventArgs e) { - App.SplashScreen.Close(TimeSpan.FromMilliseconds(300)); - DisplayRootViewFor(); + try + { + var packageSerice = Container.Resolve(); + var features = await packageSerice.GetFeatures(); + + var backgroundFeature = features.FirstOrDefault(feature => string.Equals(feature.Name, "useBackgroundService", StringComparison.OrdinalIgnoreCase)); + var elevationProvider = Elevation.Instance; + elevationProvider.IsBackgroundRunning = backgroundFeature?.Enabled ?? false; + + App.SplashScreen.Close(TimeSpan.FromMilliseconds(300)); + DisplayRootViewFor(); + } + catch (Exception ex) + { + MessageBox.Show($"Failed to start application.\n{ex.Message}\n\nMore details available in application logs."); + Logger.Fatal(ex, "Failed to start application."); + await OnExitAsync(); + } } protected override object GetInstance(Type service, string key) diff --git a/Source/ChocolateyGui/ChocolateyGui.csproj b/Source/ChocolateyGui/ChocolateyGui.csproj index 012a49ad4..3a79db520 100644 --- a/Source/ChocolateyGui/ChocolateyGui.csproj +++ b/Source/ChocolateyGui/ChocolateyGui.csproj @@ -233,7 +233,7 @@ - + diff --git a/Source/ChocolateyGui/Elevation.cs b/Source/ChocolateyGui/Elevation.cs new file mode 100644 index 000000000..e5cade919 --- /dev/null +++ b/Source/ChocolateyGui/Elevation.cs @@ -0,0 +1,62 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright 2014 - Present Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC +// +// -------------------------------------------------------------------------------------------------------------------- + +using Autofac; +using Caliburn.Micro; +using ChocolateyGui.Utilities; +using ChocolateyGui.Utilities.Extensions; + +namespace ChocolateyGui +{ + public class Elevation : PropertyChangedBase + { + private bool _isElevated = Hacks.IsElevated; + private bool _isBackgroundRunning = false; + + public static Elevation Instance + { + get { return Bootstrapper.Container.Resolve(); } + } + + public bool IsElevated + { + get + { + return _isElevated; + } + + set + { + this.SetPropertyValue(ref _isElevated, value); + NotifyOfPropertyChange(nameof(CanDoCentralActions)); + } + } + + public bool IsBackgroundRunning + { + get + { + return _isBackgroundRunning; + } + + set + { + this.SetPropertyValue(ref _isBackgroundRunning, value); + NotifyOfPropertyChange(nameof(CanDoCentralActions)); + } + } + + public bool CanDoCentralActions + { + get { return _isBackgroundRunning || _isElevated; } + } + + public bool CanDoTertiaryActions + { + get { return _isElevated; } + } + } +} \ No newline at end of file diff --git a/Source/ChocolateyGui/Resources/Controls.xaml b/Source/ChocolateyGui/Resources/Controls.xaml index 5e13be5bc..19f0b9bf0 100644 --- a/Source/ChocolateyGui/Resources/Controls.xaml +++ b/Source/ChocolateyGui/Resources/Controls.xaml @@ -8,7 +8,7 @@ xmlns:converters="clr-namespace:ChocolateyGui.Utilities.Converters" xmlns:commands="clr-namespace:ChocolateyGui.Commands" xmlns:utilities="clr-namespace:ChocolateyGui.Utilities" - xmlns:providers="clr-namespace:ChocolateyGui.Providers" + xmlns:app="clr-namespace:ChocolateyGui" xmlns:lang="clr-namespace:ChocolateyGui.Properties" mc:Ignorable="d"> @@ -23,17 +23,33 @@ - + - - - - - + + + + + + + + + + + + + 15 @@ -649,10 +665,10 @@ - - @@ -661,7 +677,7 @@ - @@ -670,13 +686,13 @@ - - - diff --git a/Source/ChocolateyGui/Services/ChocolateyRemotePackageService.cs b/Source/ChocolateyGui/Services/ChocolateyRemotePackageService.cs index 7e336f725..ee3241ed8 100644 --- a/Source/ChocolateyGui/Services/ChocolateyRemotePackageService.cs +++ b/Source/ChocolateyGui/Services/ChocolateyRemotePackageService.cs @@ -95,6 +95,11 @@ public async Task>> GetOutdatedPack public async Task InstallPackage(string id, SemanticVersion version = null, Uri source = null, bool force = false) { await Initialize(true); + if (Elevation.Instance.IsBackgroundRunning) + { + source = null; + } + var result = await _chocolateyService.InstallPackage(id, version?.ToString(), source, force); if (!result.Successful) { @@ -145,6 +150,11 @@ await _progressService.ShowMessageAsync( public async Task UpdatePackage(string id, Uri source = null) { await Initialize(true); + if (Elevation.Instance.IsBackgroundRunning) + { + source = null; + } + var result = await _chocolateyService.UpdatePackage(id, source); if (!result.Successful) { @@ -226,6 +236,10 @@ public async Task SetFeature(ChocolateyFeature feature) { await Initialize(true); await _chocolateyService.SetFeature(feature); + if (string.Equals(feature.Name, "useBackgroundService", StringComparison.OrdinalIgnoreCase)) + { + Elevation.Instance.IsBackgroundRunning = feature.Enabled; + } } public async Task> GetSettings() @@ -380,7 +394,7 @@ private async Task InitializeImpl(bool requireAdmin = false) _chocolateyService = CreateClient(); // ReSharper disable once PossibleNullReferenceException - ((ElevationStatusProvider)Application.Current.FindResource("Elevation")).IsElevated = await _chocolateyService.IsElevated(); + Elevation.Instance.IsElevated = await _chocolateyService.IsElevated(); } } diff --git a/Source/ChocolateyGui/Startup/ChocolateyGuiModule.cs b/Source/ChocolateyGui/Startup/ChocolateyGuiModule.cs index ed6fcabb4..197a4fa37 100644 --- a/Source/ChocolateyGui/Startup/ChocolateyGuiModule.cs +++ b/Source/ChocolateyGui/Startup/ChocolateyGuiModule.cs @@ -31,6 +31,7 @@ protected override void Load(ContainerBuilder builder) // Register Providers builder.RegisterType().As().SingleInstance(); + builder.RegisterType().SingleInstance(); var configurationProvider = new ChocolateyConfigurationProvider(); builder.RegisterInstance(configurationProvider).As().SingleInstance(); diff --git a/Source/ChocolateyGui/Views/PackageView.xaml b/Source/ChocolateyGui/Views/PackageView.xaml index 161b2f5c1..bb1ee3331 100644 --- a/Source/ChocolateyGui/Views/PackageView.xaml +++ b/Source/ChocolateyGui/Views/PackageView.xaml @@ -8,7 +8,7 @@ xmlns:commands="clr-namespace:ChocolateyGui.Commands" xmlns:viewModels="clr-namespace:ChocolateyGui.ViewModels" xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" - xmlns:controls="clr-namespace:ChocolateyGui.Controls" + xmlns:controls="clr-namespace:ChocolateyGui.Controls" xmlns:lang="clr-namespace:ChocolateyGui.Properties" mc:Ignorable="d" d:DesignHeight="786" d:DesignWidth="1366" @@ -19,7 +19,13 @@ - + - + - + - + - + - + + + + - + - + - - + + + + + - +