-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Security.Principal; | ||
using System.Threading.Tasks; | ||
using ModernWpf.Controls; | ||
using NeatShift.Views; | ||
|
||
namespace NeatShift.Services | ||
{ | ||
public static class AdminManager | ||
{ | ||
private static bool _isAdminGranted = false; | ||
|
||
public static bool IsAdminGranted | ||
{ | ||
get | ||
{ | ||
if (_isAdminGranted) return true; | ||
|
||
// Double check if we're actually running as admin | ||
var identity = WindowsIdentity.GetCurrent(); | ||
var principal = new WindowsPrincipal(identity); | ||
_isAdminGranted = principal.IsInRole(WindowsBuiltInRole.Administrator); | ||
|
||
return _isAdminGranted; | ||
} | ||
} | ||
|
||
public static async Task<bool> EnsureAdmin(string reason, string actions) | ||
{ | ||
if (IsAdminGranted) return true; | ||
|
||
// Show our custom prompt first | ||
var dialog = new AdminPromptDialog(reason, actions); | ||
if (await dialog.ShowAsync() != ContentDialogResult.Primary) | ||
return false; | ||
|
||
try | ||
{ | ||
// Try to restart with admin rights | ||
var processInfo = new ProcessStartInfo | ||
{ | ||
UseShellExecute = true, | ||
FileName = Process.GetCurrentProcess().MainModule?.FileName, | ||
Verb = "runas" | ||
}; | ||
|
||
Process.Start(processInfo); | ||
// Shutdown the current instance | ||
System.Windows.Application.Current.Shutdown(); | ||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public static class Messages | ||
{ | ||
public static (string Reason, string Actions) SymbolicLink => ( | ||
"Moving files with symbolic links requires administrator access to create special file system links.", | ||
"• Create symbolic links\n• Maintain file access for applications" | ||
); | ||
|
||
public static (string Reason, string Actions) SystemRestore => ( | ||
"Creating system restore points requires administrator access to modify system protection settings.", | ||
"• Create system restore points\n• Manage system protection" | ||
); | ||
|
||
public static (string Reason, string Actions) ViewLinks => ( | ||
"Viewing symbolic links requires administrator access to read special file system attributes.", | ||
"• Read symbolic link information\n• View file system attributes" | ||
); | ||
|
||
public static (string Reason, string Actions) Backup => ( | ||
"Managing backups requires administrator access to create system restore points and manage file system operations.", | ||
"• Create and manage system restore points\n• Access protected file locations for NeatSaves\n• Manage system protection settings" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ui:ContentDialog | ||
x:Class="NeatShift.Views.AdminPromptDialog" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:ui="http://schemas.modernwpf.com/2019" | ||
Title="Administrator Access Required" | ||
PrimaryButtonText="Continue" | ||
CloseButtonText="Cancel" | ||
DefaultButton="Primary"> | ||
|
||
<StackPanel Margin="0,10,0,0"> | ||
<TextBlock x:Name="ReasonText" | ||
TextWrapping="Wrap" | ||
Margin="0,0,0,20"/> | ||
|
||
<TextBlock Text="This action requires administrator privileges to:" | ||
FontWeight="SemiBold" | ||
Margin="0,0,0,10"/> | ||
|
||
<TextBlock x:Name="ActionText" | ||
TextWrapping="Wrap" | ||
Margin="20,0,0,0"/> | ||
</StackPanel> | ||
</ui:ContentDialog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ModernWpf.Controls; | ||
|
||
namespace NeatShift.Views | ||
{ | ||
public partial class AdminPromptDialog : ContentDialog | ||
{ | ||
public AdminPromptDialog(string reason, string actions) | ||
{ | ||
InitializeComponent(); | ||
ReasonText.Text = reason; | ||
ActionText.Text = actions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters