Files
opencloud/vendor/github.com/gookit/goutil/sysutil/user_nonwin.go
dependabot[bot] 89a7d171ee build(deps): bump github.com/gookit/config/v2 from 2.2.6 to 2.2.7
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.6 to 2.2.7.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.2.6...v2.2.7)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-version: 2.2.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-08-15 14:15:33 +00:00

43 lines
941 B
Go

//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.MustInt(u.Uid), strutil.MustInt(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
}