Merge pull request #2173 from wabbajack-tools/no-admin

Alert when running as admin
This commit is contained in:
Timothy Baldridge 2022-11-05 13:56:48 -06:00 committed by GitHub
commit 5f56cd9c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,8 @@
using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
@ -36,6 +38,19 @@ namespace Wabbajack
private void OnStartup(object sender, StartupEventArgs e)
{
if (IsAdmin())
{
var messageBox = MessageBox.Show("Don't run Wabbajack as Admin!", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
if (messageBox == MessageBoxResult.OK)
{
Environment.Exit(1);
}
else
{
Environment.Exit(1);
}
}
WebView2AutoInstaller.CheckAndInstallAsync(false, false).Wait();
RxApp.MainThreadScheduler = new DispatcherScheduler(Dispatcher.CurrentDispatcher);
@ -80,6 +95,22 @@ namespace Wabbajack
});
}
private static bool IsAdmin()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return false;
try
{
var identity = WindowsIdentity.GetCurrent();
var principle = new WindowsPrincipal(identity);
return principle.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception)
{
return false;
}
}
private void AddLogging(ILoggingBuilder loggingBuilder)
{
var config = new NLog.Config.LoggingConfiguration();