func (s *MongoStore) GroupsOf(principal string, transitive bool) ([]string, error) { var result []string unique := make(map[string]bool) // used to make result unique pending := []string{principal} for len(pending) > 0 { current := pending[0] pending = pending[1:] var groupMembers []groupMember err := s.members.Find(bson.M{"member": current}).All(&groupMembers) if err == mgo.ErrNotFound { continue } if err != nil { return nil, err } var groups []string for _, gm := range groupMembers { if _, has := unique[gm.Group]; !has { groups = append(groups, gm.Group) unique[gm.Group] = true } } result = append(result, groups...) if transitive { pending = append(pending, groups...) } } return util.UniqueStrings(result), nil }
func (s *memStore) GroupsOf(principal string, transitive bool) ([]string, error) { var result []string pending := []string{principal} for len(pending) > 0 { current := pending[0] pending = pending[1:] for groupName, members := range s.groups { if _, has := members[current]; has { result = append(result, groupName) if transitive { pending = append(pending, groupName) } } } } return util.UniqueStrings(result), nil }
func (s *UtilSuite) TestUniqueStrings(c *C) { c.Check(util.UniqueStrings([]string{"foo", "foo", "bar", "bar", "bar", "baz"}), HasLen, 3) c.Check(util.UniqueStrings([]string{"foo", "bar", "baz"}), HasLen, 3) c.Check(util.UniqueStrings([]string{}), HasLen, 0) }