Skip to content

Commit 136c740

Browse files
committed
Improvements to UAC caching (relates to #3248)
1 parent 8d07764 commit 136c740

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

src/UniGetUI.PackageEngine.Operations/PackageOperations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ protected sealed override void PrepareProcessStartInfo()
9696
admin = true;
9797
if (Settings.Get("DoCacheAdminRights") || Settings.Get("DoCacheAdminRightsForBatches"))
9898
{
99-
CoreTools.CacheUACForCurrentProcess().GetAwaiter().GetResult();
99+
RequestCachingOfUACPrompt();
100100
}
101101

102102
if (Package.Manager is WinGet)
103103
{
104+
// Change WinGet's TEMP folder location, to prevent ACL corruption
104105
string WinGetTemp = Path.Join(Path.GetTempPath(), "UniGetUI", "ElevatedWinGetTemp");
105106
process.StartInfo.Environment["TEMP"] = WinGetTemp;
106107
process.StartInfo.Environment["TMP"] = WinGetTemp;

src/UniGetUI.PackageEngine.Operations/ProcessOperation.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Diagnostics;
22
using System.Text;
33
using Windows.System;
4+
using UniGetUI.Core.Data;
45
using UniGetUI.Core.Logging;
6+
using UniGetUI.Core.Tools;
57
using UniGetUI.PackageEngine.Enums;
68

79
namespace UniGetUI.PackageOperations;
@@ -57,6 +59,12 @@ protected AbstractProcessOperation(bool queue_enabled, AbstractOperation? req) :
5759
};
5860
}
5961

62+
private bool _requiresUACCache;
63+
protected void RequestCachingOfUACPrompt()
64+
{
65+
_requiresUACCache = true;
66+
}
67+
6068
protected override async Task<OperationVeredict> PerformOperation()
6169
{
6270
if(process.StartInfo.UseShellExecute) throw new InvalidOperationException("UseShellExecute must be set to false");
@@ -71,6 +79,12 @@ protected override async Task<OperationVeredict> PerformOperation()
7179
Line($" - Arguments: \"{process.StartInfo.Arguments.Trim()}\"", LineType.VerboseDetails);
7280
Line($"Start Time: \"{DateTime.Now}\"", LineType.VerboseDetails);
7381

82+
if (_requiresUACCache)
83+
{
84+
_requiresUACCache = false;
85+
await CoreTools.CacheUACForCurrentProcess();
86+
}
87+
7488
process.Start();
7589
// process.BeginOutputReadLine();
7690
try { process.BeginErrorReadLine(); }

src/UniGetUI/Controls/OperationWidgets/OperationControl.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,11 @@ private async void OnOperationSucceeded(object? sender, EventArgs e)
111111
// Success notification
112112
ShowSuccessToast();
113113

114-
// Handle UAC for batches
115-
if (Settings.Get("DoCacheAdminRightsForBatches"))
116-
{
117-
bool isOpRunning = false;
118-
foreach (var op in MainApp.Operations._operationList)
119-
{
120-
if (op.Operation.Status is OperationStatus.Running or OperationStatus.InQueue)
121-
{
122-
isOpRunning = true;
123-
break;
124-
}
125-
}
126-
if(!isOpRunning) await CoreTools.ResetUACForCurrentProcess();
127-
}
128-
129114
// Clean succesful operation from list
130-
if(!Settings.Get("MaintainSuccessfulInstalls") && Operation is not DownloadOperation)
115+
if (!Settings.Get("MaintainSuccessfulInstalls") && Operation is not DownloadOperation)
116+
{
131117
await TimeoutAndClose();
118+
}
132119
}
133120

134121
private void OnOperationFailed(object? sender, EventArgs e)
@@ -160,6 +147,16 @@ private async void OnOperationFinished(object? sender, EventArgs e)
160147
rawOutput.Add("");
161148
rawOutput.Add("");
162149

150+
// Handle UAC for batches
151+
if (Settings.Get("DoCacheAdminRightsForBatches"))
152+
{
153+
if (!MainApp.Operations.AreThereRunningOperations())
154+
{
155+
Logger.Info("Clearing UAC prompt since there are no remaining operations");
156+
await CoreTools.ResetUACForCurrentProcess();
157+
}
158+
}
159+
163160
// Handle newly created shortcuts
164161
if(Settings.Get("AskToDeleteNewDesktopShortcuts")
165162
&& !MainApp.Operations.AreThereRunningOperations()

0 commit comments

Comments
 (0)