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>
pull/10081/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent 228a82f178
commit 673ff2cb0b

@ -7,6 +7,7 @@ package groupmember
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()
g, err := user.LookupGroup(group)
if err != nil {
return false, err
}
g, err := user.LookupGroup(group)
ugids, err := u.GroupIds()
if err != nil {
return false, err
}
for _, ugid := range ugids {
if g.Gid == ugid {
return true, nil
}
}
return false, nil
return slices.Contains(ugids, g.Gid), nil
}

Loading…
Cancel
Save