Created
May 19, 2023 21:49
-
-
Save adam-hanna/40733be32d4486529b48daa9d1f4972a to your computer and use it in GitHub Desktop.
Group Membership
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
// A group's effective members include the members for the group itself as well as all of its children. Write a function to detect if a given user is an effective member of a given group. | |
type Group struct { | |
id string | |
members []string | |
parent string | |
} | |
func main() { | |
groupList := []Group{ | |
Group{ | |
id: "design", | |
members: []string{"Jane"}, | |
parent: "data", | |
}, | |
Group{ | |
id: "data", | |
members: []string{"John"}, | |
}, | |
Group{ | |
id: "design-leadership", | |
parent: "design", | |
members: []string{"Jerry", "Mary", "Jim"}, | |
}, | |
Group{ | |
id: "foo", | |
parent: "design", | |
members: []string{"Mike"}, | |
}, | |
Group{ | |
id: "bar", | |
parent: "foo", | |
members: []string{"Mark"}, | |
}, | |
} | |
fmt.Println(isMember("Mary", "design", groupList) == true) | |
fmt.Println(isMember("Mark", "design", groupList) == true) | |
fmt.Println(isMember("Jane", "design", groupList) == true) | |
fmt.Println(isMember("Jane", "design-leadership", groupList) == false) | |
} | |
/* | |
* data - John1 | |
* design - Jane | |
* design-leadership - John2 Mary Jim | |
*/ | |
// isMember("Jane", "design-leadership") -> false | |
// isMember("John", "design") -> true | |
// isMember("Mary", "data") -> true | |
// isMember("John1", "design-leadsership") -> false | |
func isMember(name, groupID string, groupList []Group) bool { | |
// check if in first group | |
for _, group := range groupList { | |
if strings.ToLower(group.id) == strings.ToLower(groupID) { | |
for _, memberName := range group.members { | |
if strings.ToLower(name) == strings.ToLower(memberName) { | |
return true | |
} | |
} | |
} | |
} | |
// if not, next check for membership in children | |
childrenIDs := fetchChildrenIDs("design", groupList) | |
for _, childID := range childrenIDs { | |
for _, group := range groupList { | |
if strings.ToLower(group.id) == strings.ToLower(childID) { | |
for _, memberName := range group.members { | |
if strings.ToLower(name) == strings.ToLower(memberName) { | |
return true | |
} | |
} | |
} | |
} | |
} | |
return false | |
} | |
func fetchChildrenIDs(groupID string, groupList []Group) []string { | |
var ret []string | |
for _, group := range groupList { | |
if strings.ToLower(groupID) == strings.ToLower(group.parent) { | |
ret = append(ret, group.id) | |
} | |
} | |
// recursive search... | |
for _, tmpGroupID := range ret { | |
ret = append(ret, fetchChildrenIDs(tmpGroupID, groupList)...) | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment