-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add Windows administrator privilege check #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,52 @@ pub fn check_privileges() -> miette::Result<()> { | |
| )); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| { | ||
| if !is_elevated() { | ||
| return Err(miette!( | ||
| "This program must be run as Administrator (try: run terminal as administrator)" | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| fn is_elevated() -> bool { | ||
| use std::ptr::null_mut; | ||
| use windows_sys::Win32::Foundation::{CloseHandle, HANDLE}; | ||
| use windows_sys::Win32::Security::{ | ||
| GetTokenInformation, TokenElevation, TOKEN_ELEVATION, TOKEN_QUERY, | ||
| }; | ||
| use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken}; | ||
|
|
||
| unsafe { | ||
| let mut token_handle: HANDLE = null_mut(); | ||
| if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token_handle) == 0 { | ||
| return false; | ||
| } | ||
|
|
||
| let mut elevation = TOKEN_ELEVATION { TokenIsElevated: 0 }; | ||
| let mut return_length: u32 = 0; | ||
| let size = std::mem::size_of::<TOKEN_ELEVATION>() as u32; | ||
|
|
||
| let result = GetTokenInformation( | ||
| token_handle, | ||
| TokenElevation, | ||
| &mut elevation as *mut _ as *mut _, | ||
| size, | ||
| &mut return_length, | ||
| ); | ||
|
|
||
| CloseHandle(token_handle); | ||
|
|
||
| result != 0 && elevation.TokenIsElevated != 0 | ||
|
Comment on lines
+48
to
+58
|
||
| } | ||
| } | ||
|
|
||
| #[allow(unused)] | ||
| pub fn find_it() -> miette::Result<PathBuf> { | ||
| if let Ok(path) = which("it") { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The job name 'test-windows' is inconsistent with the existing job name 'test', which runs on Linux. For consistency and clarity, consider renaming the existing job to 'test-linux' or renaming this job to something that better reflects it's a build-only job (like 'build-windows'), since neither job actually runs tests.