Skip to content

Commit 12df068

Browse files
committed
Reworked the privilege checking process
1 parent 738ccf3 commit 12df068

6 files changed

Lines changed: 145 additions & 159 deletions

File tree

src/WGet.NET/Components/ProcessManager.cs renamed to src/WGet.NET/Components/Internal/ProcessManager.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
using WGetNET.Models;
1010
using WGetNET.Extensions;
1111

12-
namespace WGetNET
12+
namespace WGetNET.Components.Internal
1313
{
1414
/// <summary>
15-
/// The <see langword="internal"/> class <see cref="WGetNET.ProcessManager"/>
15+
/// The <see langword="internal"/> class <see cref="ProcessManager"/>
1616
/// provides the winget process execution.
1717
/// </summary>
1818
internal class ProcessManager
1919
{
2020
private readonly ProcessStartInfo _winGetStartInfoTemplate;
2121

2222
/// <summary>
23-
/// Initializes a new instance of the <see cref="WGetNET.ProcessManager"/> class.
23+
/// Initializes a new instance of the <see cref="ProcessManager"/> class.
2424
/// </summary>
2525
/// <param name="processName">
2626
/// The name of the process to execute.
@@ -41,10 +41,10 @@ public ProcessManager(string processName)
4141
/// Executes a winget process with the given command and returns the result.
4242
/// </summary>
4343
/// <param name="cmd">
44-
/// A <see cref="System.String"/> representing the command that winget should be executed with.
44+
/// A <see cref="string"/> representing the command that winget should be executed with.
4545
/// </param>
4646
/// <returns>
47-
/// A <see cref="WGetNET.Models.ProcessResult"/> object,
47+
/// A <see cref="ProcessResult"/> object,
4848
/// containing the output an exit id of the process.
4949
/// </returns>
5050
public ProcessResult ExecuteWingetProcess(string cmd)
@@ -56,10 +56,10 @@ public ProcessResult ExecuteWingetProcess(string cmd)
5656
/// Asynchronous executes a winget process with the given command and returns the result.
5757
/// </summary>
5858
/// <param name="cmd">
59-
/// A <see cref="System.String"/> representing the command that winget should be executed with.
59+
/// A <see cref="string"/> representing the command that winget should be executed with.
6060
/// </param>
6161
/// <returns>
62-
/// A <see cref="WGetNET.Models.ProcessResult"/> object,
62+
/// A <see cref="ProcessResult"/> object,
6363
/// containing the output an exit id of the process.
6464
/// </returns>
6565
public async Task<ProcessResult> ExecuteWingetProcessAsync(string cmd)
@@ -74,7 +74,7 @@ public async Task<ProcessResult> ExecuteWingetProcessAsync(string cmd)
7474
/// String containig the arguments for the action.
7575
/// </param>
7676
/// <returns>
77-
/// A <see cref="System.Diagnostics.ProcessStartInfo"/> object, for the process.
77+
/// A <see cref="ProcessStartInfo"/> object, for the process.
7878
/// </returns>
7979
private ProcessStartInfo GetStartInfo(string cmd)
8080
{
@@ -94,7 +94,7 @@ private ProcessStartInfo GetStartInfo(string cmd)
9494
/// Runs a process with the current start informations.
9595
/// </summary>
9696
/// <returns>
97-
/// A <see cref="WGetNET.Models.ProcessResult"/> object,
97+
/// A <see cref="ProcessResult"/> object,
9898
/// containing the output an exit id of the process.
9999
/// </returns>
100100
private ProcessResult RunProcess(ProcessStartInfo processStartInfo)
@@ -120,7 +120,7 @@ private ProcessResult RunProcess(ProcessStartInfo processStartInfo)
120120
/// Asynchronous runs a process with the current start informations.
121121
/// </summary>
122122
/// <returns>
123-
/// A <see cref="WGetNET.Models.ProcessResult"/> object,
123+
/// A <see cref="ProcessResult"/> object,
124124
/// containing the output an exit id of the process.
125125
/// </returns>
126126
private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartInfo)
@@ -146,10 +146,10 @@ private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartI
146146
/// Reads the data from the process output to a string array.
147147
/// </summary>
148148
/// <param name="output">
149-
/// The <see cref="System.IO.StreamReader"/> with the process output.
149+
/// The <see cref="StreamReader"/> with the process output.
150150
/// </param>
151151
/// <returns>
152-
/// A <see cref="System.String"/> array
152+
/// A <see cref="string"/> array
153153
/// containing the process output stream content by lines.
154154
/// </returns>
155155
private string[] ReadSreamOutput(StreamReader output)
@@ -175,10 +175,10 @@ private string[] ReadSreamOutput(StreamReader output)
175175
/// Asynchronous reads the data from the process output to a string array.
176176
/// </summary>
177177
/// <param name="output">
178-
/// The <see cref="System.IO.StreamReader"/> with the process output.
178+
/// The <see cref="StreamReader"/> with the process output.
179179
/// </param>
180180
/// <returns>
181-
/// A <see cref="System.String"/> array
181+
/// A <see cref="string"/> array
182182
/// containing the process output stream content by lines.
183183
/// </returns>
184184
private async Task<string[]> ReadSreamOutputAsync(StreamReader output)

‎src/WGet.NET/Components/WinGet.cs‎

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
using System;
66
using System.IO;
77
using System.Threading.Tasks;
8+
using System.Security.Principal;
89
using WGetNET.Models;
910
using WGetNET.Exceptions;
1011
using WGetNET.HelperClasses;
12+
using WGetNET.Components.Internal;
13+
using System.Security;
1114

1215
namespace WGetNET
1316
{
@@ -26,6 +29,8 @@ public class WinGet
2629
private string _versionString;
2730
private Version _version;
2831

32+
private readonly bool _administratorPrivileges;
33+
2934
/// <summary>
3035
/// Gets if winget is installed on the system.
3136
/// </summary>
@@ -97,6 +102,9 @@ public Version Version
97102
/// </summary>
98103
public WinGet()
99104
{
105+
// Check if the current process has administrator privileges
106+
_administratorPrivileges = CheckAdministratorPrivileges();
107+
100108
// Set inital values
101109
_processManager = new ProcessManager("winget");
102110
_wingetExePath = string.Empty;
@@ -327,16 +335,28 @@ protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null
327335
/// <param name="args">
328336
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
329337
/// </param>
338+
/// <param name="needsAdminRights">
339+
/// Sets if the process that should be executed needs administrator privileges.
340+
/// </param>
330341
/// <returns>
331342
/// The <see cref="WGetNET.Models.ProcessResult"/> for the process.
332343
/// </returns>
333-
/// <exception cref="WinGetNotInstalledException">
344+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
334345
/// WinGet is not installed or not found on the system.
335346
/// </exception>
336-
private protected ProcessResult Execute(string args)
347+
/// <exception cref="System.Security.SecurityException">
348+
/// The current process does not have administrator privileges.
349+
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
350+
/// </exception>
351+
private protected ProcessResult Execute(string args, bool needsAdminRights = false)
337352
{
338353
ThrowIfNotInstalled();
339354

355+
if (needsAdminRights)
356+
{
357+
ThrowIfNotAdmin();
358+
}
359+
340360
return _processManager.ExecuteWingetProcess(args);
341361
}
342362

@@ -346,17 +366,29 @@ private protected ProcessResult Execute(string args)
346366
/// <param name="args">
347367
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
348368
/// </param>
369+
/// <param name="needsAdminRights">
370+
/// Sets if the process that should be executed needs administrator privileges.
371+
/// </param>
349372
/// <returns>
350373
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
351374
/// The result is the <see cref="WGetNET.Models.ProcessResult"/> for the process.
352375
/// </returns>
353-
/// <exception cref="WinGetNotInstalledException">
376+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
354377
/// WinGet is not installed or not found on the system.
355378
/// </exception>
356-
private protected async Task<ProcessResult> ExecuteAsync(string args)
379+
/// <exception cref="System.Security.SecurityException">
380+
/// The current process does not have administrator privileges.
381+
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
382+
/// </exception>
383+
private protected async Task<ProcessResult> ExecuteAsync(string args, bool needsAdminRights = false)
357384
{
358385
ThrowIfNotInstalled();
359386

387+
if (needsAdminRights)
388+
{
389+
ThrowIfNotAdmin();
390+
}
391+
360392
return await _processManager.ExecuteWingetProcessAsync(args);
361393
}
362394

@@ -374,6 +406,20 @@ private void ThrowIfNotInstalled()
374406
}
375407
}
376408

409+
/// <summary>
410+
/// Throws a <see cref="System.Security.SecurityException"/> if the current process does not have administrator privileges.
411+
/// </summary>
412+
/// <exception cref="System.Security.SecurityException">
413+
/// The current process does not have administrator privileges.
414+
/// </exception>
415+
private void ThrowIfNotAdmin()
416+
{
417+
if (!_administratorPrivileges)
418+
{
419+
throw new SecurityException("Administrator privileges are missing.");
420+
}
421+
}
422+
377423
/// <summary>
378424
/// Checks the winget version and returns it as a <see cref="System.String"/>.
379425
/// </summary>
@@ -484,5 +530,31 @@ private bool QueryInstallation()
484530

485531
return isInstalled;
486532
}
533+
534+
/// <summary>
535+
/// Check if the current user has administrator privileges.
536+
/// </summary>
537+
/// <returns>
538+
/// <see langword="true"/> if the current user has administrator privileges and
539+
/// <see langword="false"/> if not.
540+
/// </returns>
541+
private bool CheckAdministratorPrivileges()
542+
{
543+
if (Environment.OSVersion.Platform == PlatformID.Unix)
544+
{
545+
// Making sure windows related functions dont get called on none windows systems.
546+
return true;
547+
}
548+
549+
using WindowsIdentity? identity = WindowsIdentity.GetCurrent(false);
550+
551+
if (identity != null)
552+
{
553+
return new WindowsPrincipal(identity)
554+
.IsInRole(WindowsBuiltInRole.Administrator);
555+
}
556+
557+
return false;
558+
}
487559
}
488560
}

‎src/WGet.NET/Components/WinGetPackageManager.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public class WinGetPackageManager : WinGet
2323
private const string _searchCmd = "search \"{0}\" --accept-source-agreements";
2424
private const string _searchBySourceCmd = "search \"{0}\" --source {1} --accept-source-agreements";
2525
private const string _installCmd = "install \"{0}\"";
26-
private const string _upgradeCmd = "upgrade \"{0}\"";
27-
private const string _upgradeAllCmd = "upgrade --all";
26+
private const string _upgradeCmd = "upgrade \"{0}\" --accept-source-agreements";
27+
private const string _upgradeAllCmd = "upgrade --al --accept-source-agreementsl";
2828
private const string _getUpgradeableCmd = "upgrade";
2929
private const string _includeUnknown = "--include-unknown";
3030
private const string _uninstallCmd = "uninstall \"{0}\"";
3131
private const string _exportCmd = "export -o {0}";
3232
private const string _importCmd = "import -i {0} --ignore-unavailable";
3333
private const string _hashCmd = "hash {0}";
34-
private const string _downloadCmd = "download {0} --download-directory {1}";
34+
private const string _downloadCmd = "download {0} --download-directory {1} --accept-source-agreements";
3535
private const string _pinListCmd = "pin list";
3636
private const string _pinAddCmd = "pin add \"{0}\"";
3737
private const string _pinAddByVersionCmd = "pin add \"{0}\" --version \"{1}\"";

0 commit comments

Comments
 (0)