town/user/user.go

29 lines
489 B
Go
Raw Permalink Normal View History

2021-03-23 21:58:17 +00:00
package user
2021-03-21 03:01:12 +00:00
import (
"fmt"
"os/user"
)
const adminGroup = "admin"
2021-03-23 21:38:38 +00:00
func IsAdmin(u user.User) (bool, error) {
2021-03-21 03:01:12 +00:00
adminGroup, err := user.LookupGroup(adminGroup)
if err != nil {
return false, fmt.Errorf("failed to get admin group info: %w", err)
}
groupIds, err := u.GroupIds()
if err != nil {
return false, fmt.Errorf("failed to get groups info: %w", err)
}
for _, groupId := range groupIds {
if groupId == adminGroup.Gid {
return true, nil
}
}
return false, nil
}