Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Infrastructure/AppRunner.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using WinHome.Infrastructure.Helpers;
using WinHome.Interfaces;
using WinHome.Models;
using WinHome.Services.Logging;
Expand Down Expand Up @@ -37,6 +38,8 @@ public async Task<int> RunAsync(FileInfo configFile, bool dryRun, string? profil
{
try
{
AdminGuard.EnsureAdministrator();

if (!configFile.Exists)
{
_logger.LogError($"[Error] Configuration file not found: {configFile.FullName}");
Expand Down
39 changes: 39 additions & 0 deletions src/Infrastructure/Helpers/AdminGuard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Security.Principal;
using System.Runtime.Versioning;

namespace WinHome.Infrastructure.Helpers;

/// <summary>Validates that the current process has administrative privileges.</summary>
[SupportedOSPlatform("windows")]
public static class AdminGuard
{
internal static Func<bool> IsAdministrator = () =>

Check failure on line 10 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.
{

Check failure on line 11 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.
using var identity = WindowsIdentity.GetCurrent();

Check failure on line 12 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 4 characters.
var principal = new WindowsPrincipal(identity);

Check failure on line 13 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 4 characters.
return principal.IsInRole(WindowsBuiltInRole.Administrator);

Check failure on line 14 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 4 characters.
};

Check failure on line 15 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.

/// <summary>Throws if the current process is not running as Administrator.</summary>

Check failure on line 17 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.
/// <exception cref="UnauthorizedAccessException">Thrown when not running with admin privileges.</exception>

Check failure on line 18 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Replace 111 characters with '///\s<exception\scref="UnauthorizedAccessException">Thrown\swhen\snot\srunning\swith\sadmin\sprivileges.</exception>\n'.
public static void EnsureAdministrator()

Check failure on line 19 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.
{

Check failure on line 20 in src/Infrastructure/Helpers/AdminGuard.cs

View workflow job for this annotation

GitHub Actions / lint

Fix whitespace formatting. Delete 2 characters.
if (!IsAdministrator())
{
throw new UnauthorizedAccessException(
"Error: WinHome requires Administrative Privileges to manage system configurations. " +
"Please re-run this command from an elevated (Administrator) terminal.");
}
}

/// <summary>Resets the administrator check delegate to its default implementation (used for testing).</summary>
internal static void ResetAdminCheck()
{
IsAdministrator = () =>
{
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
};
}
}
32 changes: 32 additions & 0 deletions tests/WinHome.Tests/AdminGuardTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using WinHome.Infrastructure.Helpers;
using Xunit;

namespace WinHome.Tests;

[Collection("SequentialTests")]
public class AdminGuardTests
{
[Fact]
public void EnsureAdministrator_DoesNotThrow_WhenAdmin()
{
AdminGuard.IsAdministrator = () => true;
var ex = Record.Exception(() => AdminGuard.EnsureAdministrator());
Assert.Null(ex);
}

[Fact]
public void EnsureAdministrator_ThrowsUnauthorizedAccessException_WhenNotAdmin()
{
AdminGuard.IsAdministrator = () => false;
var ex = Assert.Throws<UnauthorizedAccessException>(() => AdminGuard.EnsureAdministrator());
Assert.Contains("Administrative Privileges", ex.Message);
}

[Fact]
public void ResetAdminCheck_RestoresDefaultDelegate()
{
AdminGuard.IsAdministrator = () => false;
AdminGuard.ResetAdminCheck();
Assert.NotNull(AdminGuard.IsAdministrator);
}
}