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
This commit is contained in:
Divyansh Srivastava 2020-05-08 14:31:15 -07:00 committed by GitHub
parent efe98f7fad
commit e654999fdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<ContextMenuResult> ContextMenus(IPublicAPI api)
{
var contextMenus = new List<ContextMenuResult>
{
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<ContextMenuResult>();
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)