[PT Run] Run as user feature (#17283)

* run as different user

* fix tests

* Update src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs

* fix typo
This commit is contained in:
Heiko
2022-04-04 11:37:08 +02:00
committed by GitHub
parent 12282a8dc7
commit e444881320
17 changed files with 205 additions and 45 deletions

View File

@@ -28,7 +28,7 @@ namespace Microsoft.Plugin.Indexer
File,
}
// Extensions for adding run as admin context menu item for applications
// Extensions for adding run as admin and run as other user context menu item for applications
private readonly string[] appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
public ContextMenuLoader(PluginInitContext context)
@@ -53,6 +53,7 @@ namespace Microsoft.Plugin.Indexer
if (CanFileBeRunAsAdmin(record.Path))
{
contextMenus.Add(CreateRunAsAdminContextMenu(record));
contextMenus.Add(CreateRunAsUserContextMenu(record));
}
contextMenus.Add(new ContextMenuResult
@@ -145,6 +146,34 @@ namespace Microsoft.Plugin.Indexer
};
}
// Function to add the context menu item to run as admin
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log the exception message")]
private static ContextMenuResult CreateRunAsUserContextMenu(SearchResult record)
{
return new ContextMenuResult
{
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
Title = Properties.Resources.Microsoft_plugin_indexer_run_as_user,
Glyph = "\xE7EE",
FontFamily = "Segoe MDL2 Assets",
AcceleratorKey = Key.U,
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
try
{
Task.Run(() => Helper.RunAsUser(record.Path));
return true;
}
catch (Exception e)
{
Log.Exception($"Failed to run {record.Path} as different user, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
return false;
}
},
};
}
// Function to test if the file can be run as admin
private bool CanFileBeRunAsAdmin(string path)
{

View File

@@ -19,7 +19,7 @@ namespace Microsoft.Plugin.Indexer.Properties {
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Resources {
@@ -61,7 +61,7 @@ namespace Microsoft.Plugin.Indexer.Properties {
}
/// <summary>
/// Looks up a localized string similar to Disable drive detection warning.
/// Looks up a localized string similar to Disable non-indexed files warning.
/// </summary>
public static string disable_drive_detection_warning {
get {
@@ -186,6 +186,15 @@ namespace Microsoft.Plugin.Indexer.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Run as different user (Ctrl+Shift+U).
/// </summary>
public static string Microsoft_plugin_indexer_run_as_user {
get {
return ResourceManager.GetString("Microsoft_plugin_indexer_run_as_user", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Search.
/// </summary>

View File

@@ -162,4 +162,7 @@
<data name="disable_drive_detection_warning" xml:space="preserve">
<value>Disable non-indexed files warning</value>
</data>
</root>
<data name="Microsoft_plugin_indexer_run_as_user" xml:space="preserve">
<value>Run as different user (Ctrl+Shift+U)</value>
</data>
</root>