Skip to content

Commit

Permalink
fix: Fixed the issue that the manual administrator permission startup…
Browse files Browse the repository at this point in the history
… tray program displayed the administrator status incorrectly
  • Loading branch information
ZGGSONG committed Jan 31, 2024
1 parent 3af7e09 commit 5f002ff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
13 changes: 13 additions & 0 deletions STranslate.Util/CommonUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -398,6 +399,18 @@ public static System.Windows.Point GetMousePositionWindowsForms()
return new System.Windows.Point(ms.X / dpiScale, ms.Y / dpiScale);
}

/// <summary>
/// 是否为管理员权限
/// </summary>
/// <returns></returns>
public static bool IsUserAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new(identity);

return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

#endregion Other
}
}
18 changes: 1 addition & 17 deletions STranslate/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
Expand Down Expand Up @@ -76,28 +75,13 @@ protected override void OnExit(ExitEventArgs e)

private bool NeedAdministrator()
{
// 初始化管理员缓存
Current.Properties["admin"] = false;

// 加载配置
var isRole = Singleton<ConfigHelper>.Instance.CurrentConfig?.NeedAdministrator ?? false;

if (!isRole)
return false;

// 更新管理员缓存
bool role = IsUserAdministrator();
Current.Properties["admin"] = role;

return !role;
}

private bool IsUserAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);

return principal.IsInRole(WindowsBuiltInRole.Administrator);
return !CommonUtil.IsUserAdministrator();
}

private bool TryRunAsAdministrator()
Expand Down
2 changes: 1 addition & 1 deletion STranslate/ViewModels/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public NotifyIconViewModel()

public void UpdateToolTip(string msg = "")
{
_ = bool.TryParse(Application.Current.Properties["admin"]?.ToString(), out bool isAdmin);
bool isAdmin = CommonUtil.IsUserAdministrator();

string toolTipFormat = isAdmin ? "STranslate {0}\r\n[Administrator] #\r\n{1}" : "STranslate {0} #\r\n{1}";

Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected override void OnSourceInitialized(EventArgs e)
{
Hide();

_ = bool.TryParse(Application.Current.Properties["admin"]?.ToString(), out bool isAdmin);
bool isAdmin = CommonUtil.IsUserAdministrator();

string toolTipFormat = isAdmin ? "STranslate[Admin] {0} started" : "STranslate {0} started";

Expand Down

0 comments on commit 5f002ff

Please sign in to comment.