Skip to content

Commit

Permalink
refactor: 🔨 Rewrite "is_administrator" to use "Owned" type
Browse files Browse the repository at this point in the history
  • Loading branch information
AntwortEinesLebens committed Oct 19, 2024
1 parent 8efa812 commit 20973b4
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/windows/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use windows::{
core::Error as WindowsError,
core::{Owned, Result as WindowsResult},
Win32::{
Foundation::BOOL,
Security::{
AllocateAndInitializeSid, CheckTokenMembership, FreeSid, PSID, SECURITY_NT_AUTHORITY,
},
Security::{AllocateAndInitializeSid, CheckTokenMembership, PSID, SECURITY_NT_AUTHORITY},
System::SystemServices::{DOMAIN_ALIAS_RID_ADMINS, SECURITY_BUILTIN_DOMAIN_RID},
},
};

pub fn is_administrator() -> Result<bool, WindowsError> {
let is_admin: *mut BOOL = &mut BOOL::from(false);
let mut administrators_group: PSID = PSID::default();
pub fn is_administrator() -> WindowsResult<bool> {
let mut is_admin: bool = false;

unsafe {
let mut administrators_group: Owned<PSID> = Owned::new(PSID::default());

AllocateAndInitializeSid(
&SECURITY_NT_AUTHORITY,
2,
Expand All @@ -29,16 +27,15 @@ pub fn is_administrator() -> Result<bool, WindowsError> {
0,
0,
0,
&mut administrators_group,
&mut *administrators_group,
)?;

let result: Result<(), WindowsError> =
CheckTokenMembership(None, administrators_group, is_admin);

FreeSid(administrators_group);

result?;
CheckTokenMembership(
None,
*administrators_group,
&mut is_admin as *mut _ as *mut _,
)?;
}

Ok(unsafe { (*is_admin).into() })
Ok(is_admin)
}

0 comments on commit 20973b4

Please sign in to comment.