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