-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test if user has admin privileges on Windows.
- Loading branch information
Showing
4 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func userIsAdmin() bool { | ||
return os.Getuid() == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func userIsAdmin() bool { | ||
var sid *windows.SID | ||
|
||
err := windows.AllocateAndInitializeSid( | ||
&windows.SECURITY_NT_AUTHORITY, | ||
2, | ||
windows.SECURITY_BUILTIN_DOMAIN_RID, | ||
windows.DOMAIN_ALIAS_RID_ADMINS, | ||
0, 0, 0, 0, 0, 0, | ||
&sid, | ||
) | ||
if err != nil { | ||
return false | ||
} | ||
defer windows.FreeSid(sid) | ||
|
||
t := windows.Token(0) | ||
|
||
member, err := t.IsMember(sid) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return member | ||
} |