chore: 管理员权限启动无法使用开机自启 (#11290)

* chore: 管理员权限启动无法使用开机自启

* i18n: zh-tw tweak

---------

Co-authored-by: momomochi987 <as99us301@gmail.com>
This commit is contained in:
uye
2024-12-05 13:25:56 +08:00
committed by GitHub
parent 327fb9e6f7
commit 1ef4bcba2a
8 changed files with 34 additions and 10 deletions

View File

@@ -215,14 +215,7 @@ namespace MaaWpfGui.Main
}
}
private static bool IsUserAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
SecurityIdentifier adminSid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
return principal.IsInRole(adminSid);
}
public static bool IsUserAdministrator() => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
/// <inheritdoc/>
protected override void ConfigureIoC(IStyletIoCBuilder builder)

View File

@@ -270,6 +270,9 @@ You can cancel this popup by clicking the Settings - About Us - Issue Reporting
<system:String x:Key="AddConfigSuccess">Configuration &lt;{0}&gt; added successfully</system:String>
<system:String x:Key="ConfigExists">Configuration &lt;{0}&gt; already exists</system:String>
<system:String x:Key="LaunchOnSystemStartup">Auto start with PC</system:String>
<system:String x:Key="LaunchOnSystemStartupAdminPrompt">
Administrator mode does not support startup on boot. It is not recommended to run MAA with administrator privileges for an extended period.\nIf necessary, please go to Control Panel > Windows Tools > Task Scheduler to create a custom task. When setting the trigger, make sure to check「Run with highest privileges」
</system:String>
<system:String x:Key="RunTaskAfterLaunch">Auto Run task after launched</system:String>
<system:String x:Key="MinimizeAfterLaunch">Auto Minimize after launched</system:String>
<system:String x:Key="ClientType">Client Type</system:String>

View File

@@ -270,6 +270,9 @@
<system:String x:Key="AddConfigSuccess">構成&lt; {0}&gt;が正常に追加されました</system:String>
<system:String x:Key="ConfigExists">構成 &lt; {0}&gt; は既に存在します</system:String>
<system:String x:Key="LaunchOnSystemStartup">スタートアップ</system:String>
<system:String x:Key="LaunchOnSystemStartupAdminPrompt">
管理者権限での起動は、起動時の自動実行をサポートしていません。MAAを長時間管理者権限で実行することはお勧めしません。\n必要な場合は、「コントロールパネル > Windowsツール > タスクスケジューラ」に移動してカスタムタスクを作成してください。トリガーを設定する際に、「最も高い特権で実行」を必ず選択してください。
</system:String>
<system:String x:Key="RunTaskAfterLaunch">アプリの起動時に自動的に実行</system:String>
<system:String x:Key="MinimizeAfterLaunch">アプリの起動時に自動的に最小化</system:String>
<system:String x:Key="ClientType">クライアントバージョン</system:String>

View File

@@ -270,6 +270,9 @@
<system:String x:Key="AddConfigSuccess">구성 &lt; {0}&gt;가 성공적으로 추가되었습니다</system:String>
<system:String x:Key="ConfigExists">구성 &lt; {0}&gt;가 이미 있습니다</system:String>
<system:String x:Key="LaunchOnSystemStartup">시스템 시작 시 MAA 실행</system:String>
<system:String x:Key="LaunchOnSystemStartupAdminPrompt">
관리자 권한으로 실행 시 부팅 시 자동 실행이 지원되지 않습니다. MAA를 관리자 권한으로 장시간 실행하는 것은 권장하지 않습니다.\n필요할 경우 제어판 > Windows 도구 > 작업 스케줄러에서 사용자 지정 작업을 생성하세요. 트리거를 설정할 때 "가장 높은 권한으로 실행"을 반드시 선택하십시오.
</system:String>
<system:String x:Key="RunTaskAfterLaunch">MAA 실행 후 작업 자동 시작</system:String>
<system:String x:Key="MinimizeAfterLaunch">MAA 실행 후 최소화</system:String>
<system:String x:Key="ClientType">클라이언트 종류</system:String>

View File

@@ -270,6 +270,9 @@
<system:String x:Key="AddConfigSuccess">配置 &lt;{0}&gt; 添加成功</system:String>
<system:String x:Key="ConfigExists">配置 &lt;{0}&gt; 已存在</system:String>
<system:String x:Key="LaunchOnSystemStartup">开机自动启动 MAA</system:String>
<system:String x:Key="LaunchOnSystemStartupAdminPrompt">
管理员权限启动不支持开机自启。不建议长期以管理员权限运行 MAA。\n如确有需要请前往 控制面板 > Windows 工具 > 任务计划程序 创建自定义任务。设置触发器时,请确保勾选「使用最高权限运行」。
</system:String>
<system:String x:Key="RunTaskAfterLaunch">启动 MAA 后直接运行</system:String>
<system:String x:Key="MinimizeAfterLaunch">启动 MAA 后直接最小化</system:String>
<system:String x:Key="ClientType">客户端类型</system:String>

View File

@@ -270,6 +270,9 @@
<system:String x:Key="AddConfigSuccess">配置 &lt;{0}&gt; 新增成功</system:String>
<system:String x:Key="ConfigExists">配置 &lt;{0}&gt; 已存在</system:String>
<system:String x:Key="LaunchOnSystemStartup">開機自動啟動 MAA</system:String>
<system:String x:Key="LaunchOnSystemStartupAdminPrompt">
"以系統管理員身分執行" 不支援開機自動啟動。不建議長期以系統管理員身分執行 MAA。\n如有需要請前往 控制台 > 系統管理工具 > 工作排程器 建立工作。設定安全性選項時,請確保已勾選「以最高權限執行」。
</system:String>
<system:String x:Key="RunTaskAfterLaunch">啟動 MAA 後直接執行</system:String>
<system:String x:Key="MinimizeAfterLaunch">啟動 MAA 後直接最小化</system:String>
<system:String x:Key="ClientType">用戶端類型</system:String>

View File

@@ -15,6 +15,9 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Security;
using MaaWpfGui.Helper;
using System.Windows;
using MaaWpfGui.Main;
using Microsoft.Win32;
using Serilog;
@@ -61,6 +64,12 @@ namespace MaaWpfGui.Utilities
/// <returns>The value.</returns>
public static bool CheckStart()
{
if (Bootstrapper.IsUserAdministrator())
{
SetStart(false, out _);
return false;
}
try
{
using var key = Registry.CurrentUser.OpenSubKey(CurrentUserRunKey, false);
@@ -83,6 +92,12 @@ namespace MaaWpfGui.Utilities
{
error = string.Empty;
if (set && Bootstrapper.IsUserAdministrator())
{
error = LocalizationHelper.GetString("LaunchOnSystemStartupAdminPrompt");
return false;
}
try
{
using var key = Registry.CurrentUser.OpenSubKey(CurrentUserRunKey, true);

View File

@@ -24,6 +24,7 @@ using System.Threading;
using System.Windows;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using MaaWpfGui.Main;
using MaaWpfGui.States;
using MaaWpfGui.Utilities;
using MaaWpfGui.ViewModels.UI;
@@ -59,8 +60,8 @@ public class StartSettingsUserControlModel : PropertyChangedBase
{
if (!AutoStart.SetStart(value, out var error))
{
_logger.Error("Failed to set startup.");
MessageBoxHelper.Show(error);
_logger.Error($"Failed to set startup: {error}");
MessageBoxHelper.Show(error, LocalizationHelper.GetString("Warning"), icon: MessageBoxImage.Warning);
return;
}