util/groupmember: fail earlier if group doesn't exist, use slices.Contains

Noticed both while re-reading this code.

Updates #cleanup

Change-Id: I3b70f1d5dc372853fa292ae1adbdee8cfc6a9a7b
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2023-11-01 18:58:03 -07:00 committed by Brad Fitzpatrick
parent 228a82f178
commit 673ff2cb0b

View File

@ -7,6 +7,7 @@
import (
"os/user"
"slices"
)
// IsMemberOfGroup reports whether the provided user is a member of
@ -16,18 +17,13 @@ func IsMemberOfGroup(group, userName string) (bool, error) {
if err != nil {
return false, err
}
ugids, err := u.GroupIds()
if err != nil {
return false, err
}
g, err := user.LookupGroup(group)
if err != nil {
return false, err
}
for _, ugid := range ugids {
if g.Gid == ugid {
return true, nil
ugids, err := u.GroupIds()
if err != nil {
return false, err
}
}
return false, nil
return slices.Contains(ugids, g.Gid), nil
}