town/user/user.go

29 lines
490 B
Go

package user
import (
"fmt"
"os/user"
)
const adminGroup = "admin"
func IsAdmin(u *user.User) (bool, error) {
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
}