From e654999fdf1604d205bc46dd42c13630b0dd4f9d Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Fri, 8 May 2020 14:31:15 -0700 Subject: [PATCH] Add support for win32 packaged apps to run as admin (#2812) * Updated UWP app to set entry point type for each app * Working run as admin for packaged app with permission * Updated UWP to allow apps with mediumIl trust level to be run as admin * cleanup variables not required --- .../Microsoft.Plugin.Program/Programs/UWP.cs | 91 +++++++++++++++---- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/UWP.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/UWP.cs index 6576213704..19ec8d85a5 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/UWP.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/UWP.cs @@ -21,6 +21,7 @@ using Windows.UI.Xaml.Media; using System.Windows.Controls; using Wox.Plugin; using System.Reflection; +using Wox.Plugin.SharedCommands; namespace Microsoft.Plugin.Program.Programs { @@ -257,10 +258,11 @@ namespace Microsoft.Plugin.Program.Programs public string UserModelId { get; set; } public string BackgroundColor { get; set; } + public string EntryPoint { get; set; } public string Name => DisplayName; public string Location => Package.Location; - public bool Enabled { get; set; } + public bool CanRunElevated {get;set;} public string LogoUri { get; set; } public string LogoPath { get; set; } @@ -311,24 +313,52 @@ namespace Microsoft.Plugin.Program.Programs public List ContextMenus(IPublicAPI api) { - var contextMenus = new List - { - new ContextMenuResult - { - PluginName = Assembly.GetExecutingAssembly().GetName().Name, - Title = api.GetTranslation("wox_plugin_program_open_containing_folder"), - Glyph = "\xE838", - FontFamily = "Segoe MDL2 Assets", - AcceleratorKey = "E", - AcceleratorModifiers = "Control,Shift", - Action = _ => - { - Main.StartProcess(Process.Start, new ProcessStartInfo("explorer", Package.Location)); + var contextMenus = new List(); - return true; - } - } - }; + if (CanRunElevated) + { + contextMenus.Add( + new ContextMenuResult + { + PluginName = Assembly.GetExecutingAssembly().GetName().Name, + Title = api.GetTranslation("wox_plugin_program_run_as_administrator"), + Glyph = "\xE7EF", + FontFamily = "Segoe MDL2 Assets", + AcceleratorKey = "Enter", + AcceleratorModifiers = "Control,Shift", + Action = _ => + { + string command = "shell:AppsFolder\\" + UniqueIdentifier; + command.Trim(); + command = Environment.ExpandEnvironmentVariables(command); + + var info = ShellCommand.SetProcessStartInfo(command, verb: "runas"); + info.UseShellExecute = true; + + Process.Start(info); + return true; + } + } + ); + } + contextMenus.Add( + new ContextMenuResult + { + PluginName = Assembly.GetExecutingAssembly().GetName().Name, + Title = api.GetTranslation("wox_plugin_program_open_containing_folder"), + Glyph = "\xE838", + FontFamily = "Segoe MDL2 Assets", + AcceleratorKey = "E", + AcceleratorModifiers = "Control,Shift", + Action = _ => + { + Main.StartProcess(Process.Start, new ProcessStartInfo("explorer", Package.Location)); + + return true; + } + }); + + return contextMenus; } @@ -361,13 +391,36 @@ namespace Microsoft.Plugin.Program.Programs Description = manifestApp.GetStringValue("Description"); BackgroundColor = manifestApp.GetStringValue("BackgroundColor"); Package = package; - + EntryPoint = manifestApp.GetStringValue("EntryPoint"); + DisplayName = ResourceFromPri(package.FullName, DisplayName); Description = ResourceFromPri(package.FullName, Description); LogoUri = LogoUriFromManifest(manifestApp); LogoPath = LogoPathFromUri(LogoUri); Enabled = true; + CanRunElevated = IfApplicationcanRunElevated(); + } + + private bool IfApplicationcanRunElevated() + { + if (EntryPoint == "Windows.FullTrustApplication") + { + return true; + } + else + { + var manifest = Package.Location + "\\AppxManifest.xml"; + if (File.Exists(manifest)) + { + var file = File.ReadAllText(manifest); + if(file.Contains("TrustLevel=\"mediumIL\"", StringComparison.InvariantCultureIgnoreCase)) + { + return true; + } + } + } + return false; } internal string ResourceFromPri(string packageFullName, string resourceReference)