mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Add support for detecting if user has administrative privileges.
This commit is contained in:
@@ -93,6 +93,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
|
||||||
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
|
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
|
||||||
|
<PackageReference Include="System.Security.Principal.Windows" Version="4.5.1" />
|
||||||
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
|
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
|
||||||
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.2.2-beta" />
|
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.2.2-beta" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ using System;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security.Principal;
|
||||||
|
|
||||||
namespace DiscImageChef.CommonTypes.Interop
|
namespace DiscImageChef.CommonTypes.Interop
|
||||||
{
|
{
|
||||||
@@ -57,6 +58,11 @@ namespace DiscImageChef.CommonTypes.Interop
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly bool Is32Bit = IntPtr.Size == 4;
|
public static readonly bool Is32Bit = IntPtr.Size == 4;
|
||||||
|
|
||||||
|
public static bool IsWindows =>
|
||||||
|
GetRealPlatformID() == PlatformID.Win32NT || GetRealPlatformID() == PlatformID.Win32S ||
|
||||||
|
GetRealPlatformID() == PlatformID.Win32Windows || GetRealPlatformID() == PlatformID.WinCE ||
|
||||||
|
GetRealPlatformID() == PlatformID.WindowsPhone || GetRealPlatformID() == PlatformID.Xbox;
|
||||||
|
|
||||||
[DllImport("libc", SetLastError = true)]
|
[DllImport("libc", SetLastError = true)]
|
||||||
static extern int uname(out utsname name);
|
static extern int uname(out utsname name);
|
||||||
|
|
||||||
@@ -270,6 +276,28 @@ namespace DiscImageChef.CommonTypes.Interop
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsAdmin
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if(!IsWindows) return Environment.UserName == "root";
|
||||||
|
|
||||||
|
bool isAdmin;
|
||||||
|
WindowsIdentity user = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
user = WindowsIdentity.GetCurrent();
|
||||||
|
WindowsPrincipal principal = new WindowsPrincipal(user);
|
||||||
|
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
|
||||||
|
}
|
||||||
|
catch(UnauthorizedAccessException ex) { isAdmin = false; }
|
||||||
|
catch(Exception ex) { isAdmin = false; }
|
||||||
|
finally { user?.Dispose(); }
|
||||||
|
|
||||||
|
return isAdmin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// POSIX uname structure, size from OSX, big enough to handle extra fields
|
/// POSIX uname structure, size from OSX, big enough to handle extra fields
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -302,10 +330,5 @@ namespace DiscImageChef.CommonTypes.Interop
|
|||||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||||
public string machine;
|
public string machine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsWindows =>
|
|
||||||
GetRealPlatformID() == PlatformID.Win32NT || GetRealPlatformID() == PlatformID.Win32S ||
|
|
||||||
GetRealPlatformID() == PlatformID.Win32Windows || GetRealPlatformID() == PlatformID.WinCE ||
|
|
||||||
GetRealPlatformID() == PlatformID.WindowsPhone || GetRealPlatformID() == PlatformID.Xbox;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user