Skip to content

Commit

Permalink
Test if user has admin privileges on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
beppler committed Oct 8, 2020
1 parent 79e78b1 commit 8fa5880
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type powerline struct {
args args
cwd string
userInfo user.User
userIsAdmin bool
hostname string
username string
pathAliases map[string]string
Expand Down Expand Up @@ -76,6 +77,7 @@ func newPowerline(args args, cwd string, priorities map[string]int, align alignm
p.username = usernameWithAd[1]
}
}
p.userIsAdmin = userIsAdmin()

p.theme = themes[*args.Theme]
p.shellInfo = shellInfos[*args.Shell]
Expand Down
3 changes: 1 addition & 2 deletions segment-username.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
pwl "github.com/justjanne/powerline-go/powerline"
"os"
)

func segmentUser(p *powerline) []pwl.Segment {
Expand All @@ -16,7 +15,7 @@ func segmentUser(p *powerline) []pwl.Segment {
}

var background uint8
if os.Getuid() == 0 {
if p.userIsAdmin {
background = p.theme.UsernameRootBg
} else {
background = p.theme.UsernameBg
Expand Down
11 changes: 11 additions & 0 deletions user-is-admin.go
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
}
33 changes: 33 additions & 0 deletions user-is-admin_windows.go
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
}

0 comments on commit 8fa5880

Please sign in to comment.