-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathuser_nonwin.go
42 lines (34 loc) · 947 Bytes
/
user_nonwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//go:build !windows
package sysutil
import (
"os"
"syscall"
"github.com/gookit/goutil/strutil"
)
// IsAdmin Determine whether the current user is an administrator(root)
func IsAdmin() bool {
return os.Getuid() == 0
}
// ChangeUserByName change work user by new username.
func ChangeUserByName(newUname string) error {
u := MustFindUser(newUname)
// syscall.Setlogin(newUname)
return ChangeUserUIDGid(strutil.IntOrPanic(u.Uid), strutil.IntOrPanic(u.Gid))
}
// ChangeUserUidGid change work user by new username uid,gid
//
// Deprecated: use ChangeUserUIDGid instead
func ChangeUserUidGid(newUID int, newGid int) error {
return ChangeUserUIDGid(newUID, newGid)
}
// ChangeUserUIDGid change work user by new username uid,gid
func ChangeUserUIDGid(newUID int, newGid int) (err error) {
if newUID > 0 {
err = syscall.Setuid(newUID)
// update group id
if err == nil && newGid > 0 {
err = syscall.Setgid(newGid)
}
}
return
}