From 673ff2cb0b6b25ef040a3a1244f2c97ac0e1c8b7 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 1 Nov 2023 18:58:03 -0700 Subject: [PATCH] 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 --- util/groupmember/groupmember.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/util/groupmember/groupmember.go b/util/groupmember/groupmember.go index d0cace805..d60416816 100644 --- a/util/groupmember/groupmember.go +++ b/util/groupmember/groupmember.go @@ -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 }