// ReadAll searches for all objects which are of the same type as item and match the criteria. // dn is the root of the subtree which is searched, scope is the scope of the search, filter // is a fmt format string used as filter with args being values for the format string. The arguments are // automatically escaped and must fmt.Print to a sane (at least for your LDAP data) value. func (c *Manager) ReadAll(item Item, dn string, scope Scope, filter string, args ...interface{}) ([]Item, error) { filteredArgs := make([]interface{}, len(args)) for i, v := range args { filteredArgs[i] = ldap.FilterReplace(fmt.Sprint(v)) } realFilter := fmt.Sprintf(filter, filteredArgs...) searchRequest := ldap.NewSimpleSearchRequest(c.appendBaseDn(dn), ldap.Scope(scope), realFilter, nil) if c.Debug { log.Println("Search Request:", searchRequest) } results, err := c.conn.Search(searchRequest) if err != nil { return nil, err } items := make([]Item, len(results.Entries)) for i, v := range results.Entries { v.DN = c.removeBaseDn(v.DN) items[i] = item.Copy() items[i].UnmarshalLDAP(v) if err != nil { return nil, err } } return items, nil }
// Read values for the attributes of item from LDAP func (c *Manager) Read(item Item) error { searchRequest := ldap.NewSimpleSearchRequest(c.appendBaseDn(item.Dn()), ldap.ScopeBaseObject, "(objectClass=*)", nil) if c.Debug { log.Println("Search request:", searchRequest) } results, err := c.conn.Search(searchRequest) if err != nil { return err } if len(results.Entries) == 0 { return errors.New("No search results.") } else if len(results.Entries) > 1 { return errors.New("More than one search result.") } results.Entries[0].DN = c.removeBaseDn(results.Entries[0].DN) return item.UnmarshalLDAP(results.Entries[0]) }
// Helper method to recursively delete a subtree func (c *Manager) deleteRecursive(dn string) error { // first recursively delete all subentrys searchRequest := ldap.NewSimpleSearchRequest(dn, ldap.ScopeSingleLevel, "(objectClass=*)", nil) if c.Debug { log.Println("Search Request:", searchRequest) } results, err := c.conn.Search(searchRequest) if err != nil { return err } for _, v := range results.Entries { err = c.deleteRecursive(v.DN) if err != nil { return err } } // delete the root of the current tree deleteRequest := ldap.NewDeleteRequest(dn) return c.conn.Delete(deleteRequest) }
// Search for all objects which are of the same type as item and match the criteria. // dn is the root of the subtree which is searched, scope is the scope of the search, filter // is an ldap filter string. func (c *Manager) ReadAll(item Item, dn string, scope Scope, filter string) ([]Item, error) { searchRequest := ldap.NewSimpleSearchRequest(c.appendBaseDn(dn), ldap.Scope(scope), filter, nil) if c.Debug { log.Println("Search Request:", searchRequest) } results, err := c.conn.Search(searchRequest) if err != nil { return nil, err } items := make([]Item, len(results.Entries)) for i, v := range results.Entries { v.DN = c.removeBaseDn(v.DN) items[i] = item.Copy() items[i].UnmarshalLDAP(v) if err != nil { return nil, err } } return items, nil }