diff --git a/modules/engine/object.go b/modules/engine/object.go index e92ffb7..ecd4417 100644 --- a/modules/engine/object.go +++ b/modules/engine/object.go @@ -417,40 +417,31 @@ func (o *Object) AttrString(attr Attribute) []string { return o.Attr(attr).StringSlice() } -func (o *Object) AttrRendered(attr Attribute) []string { +func (o *Object) AttrRendered(attr Attribute) AttributeValues { switch attr { case ObjectCategory: - // ocguid := o.ObjectCategoryGUID() - // if ocguid != UnknownGUID { - // if schemaobject, found := AllObjects.Find(SchemaIDGUID, AttributeValueGUID(o.ObjectCategoryGUID())); found { - // // fmt.Println(schemaobject) - // return []string{schemaobject.OneAttrString(Name)} - // } - // } - - cat := o.OneAttrString(ObjectCategory) - if cat != "" { - - splitted := strings.Split(cat, ",") + vals := o.Attr(attr) + if vals.Len() == 1 { + cat := vals.First() + splitted := strings.Split(cat.String(), ",") if len(splitted) > 1 { - // This is a DN pointing to a category - otherwise it's just something we made up! :) - return []string{splitted[0][3:]} + return AttributeValueOne{ + Value: AttributeValueString(splitted[0][3:]), + } } - return []string{cat} } - - return []string{"Unknown"} + return NoValues{} default: - return o.Attr(attr).StringSlice() + return o.Attr(attr) } } func (o *Object) OneAttrRendered(attr Attribute) string { r := o.AttrRendered(attr) - if len(r) == 0 { + if r.Len() == 0 { return "" } - return r[0] + return r.First().String() } // Returns synthetic blank attribute value if it isn't set @@ -510,7 +501,7 @@ func (o *Object) OneAttrRaw(attr Attribute) interface{} { return nil } if a.Len() == 1 { - return a.Slice()[0].Raw() + return a.First().Raw() } return nil } @@ -521,7 +512,7 @@ func (o *Object) OneAttr(attr Attribute) AttributeValue { return nil } if a.Len() == 1 { - return a.Slice()[0] + return a.First() } return nil } diff --git a/modules/engine/objects.go b/modules/engine/objects.go index 2f398c3..1ccf8bf 100644 --- a/modules/engine/objects.go +++ b/modules/engine/objects.go @@ -223,7 +223,7 @@ func (os *Objects) refreshIndex(attribute Attribute, index *Index) { // add all existing stuff to index for _, o := range os.asarray { for _, value := range o.Attr(attribute).Slice() { - key := attributeValueToIndex(value) + key := AttributeValueToIndex(value) // Add to index index.Add(key, o, false) @@ -247,9 +247,9 @@ func (os *Objects) refreshMultiIndex(attribute, attribute2 Attribute, index *Ind values := o.Attr(attribute).Slice() values2 := o.Attr(attribute2).Slice() for _, value := range values { - key := attributeValueToIndex(value) + key := AttributeValueToIndex(value) for _, value2 := range values2 { - key2 := attributeValueToIndex(value2) + key2 := AttributeValueToIndex(value2) // Add to index index.Add(multiindexkey{key, key2}, o, false) @@ -286,9 +286,9 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) { for i, index := range os.indexes { attribute := Attribute(i) if index != nil { - for _, value := range o.Attr(attribute).Slice() { + for _, value := range o.AttrRendered(attribute).Slice() { // If it's a string, lowercase it before adding to index, we do the same on lookups - indexval := attributeValueToIndex(value) + indexval := AttributeValueToIndex(value) unique := attribute.IsUnique() @@ -319,9 +319,9 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) { values := o.Attr(attribute).Slice() values2 := o.Attr(attribute2).Slice() for _, value := range values { - key := attributeValueToIndex(value) + key := AttributeValueToIndex(value) for _, value2 := range values2 { - key2 := attributeValueToIndex(value2) + key2 := AttributeValueToIndex(value2) index.Add(multiindexkey{key, key2}, o, !isnew) } @@ -330,7 +330,7 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) { os.indexlock.RUnlock() } -func attributeValueToIndex(value AttributeValue) interface{} { +func AttributeValueToIndex(value AttributeValue) interface{} { if vs, ok := value.(AttributeValueString); ok { return strings.ToLower(string(vs)) } @@ -394,9 +394,9 @@ func (os *Objects) Merge(attrtomerge []Attribute, o *Object) bool { for _, mergetarget := range mergetargets { for attr, values := range o.AttributeValueMap() { if attr.IsSingle() && mergetarget.HasAttr(attr) { - if !CompareAttributeValues(values.Slice()[0], mergetarget.Attr(attr).Slice()[0]) { + if !CompareAttributeValues(values.First(), mergetarget.Attr(attr).First()) { // Conflicting attribute values, we can't merge these - ui.Debug().Msgf("Not merging %v into %v on %v with value '%v', as attribute %v is different", o.Label(), mergetarget.Label(), mergeattr.String(), lookfor.String(), attr.String()) + ui.Debug().Msgf("Not merging %v into %v on %v with value '%v', as attribute %v is different (%v != %v)", o.Label(), mergetarget.Label(), mergeattr.String(), lookfor.String(), attr.String(), values.First().String(), mergetarget.Attr(attr).First().String()) // if attr == WhenCreated { // ui.Debug().Msgf("Object details: %v", o.StringNoACL()) // ui.Debug().Msgf("Mergetarget details: %v", mergetarget.StringNoACL()) @@ -577,11 +577,11 @@ func (os *Objects) FindTwoMultiOrAdd(attribute Attribute, value AttributeValue, if addifnotfound == nil { if attribute2 == NonExistingAttribute { // Lookup by one attribute - matches, found := os.GetIndex(attribute).Lookup(attributeValueToIndex(value)) + matches, found := os.GetIndex(attribute).Lookup(AttributeValueToIndex(value)) return matches, found } else { // Lookup by two attributes - matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{attributeValueToIndex(value), attributeValueToIndex(value2)}) + matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{AttributeValueToIndex(value), AttributeValueToIndex(value2)}) return matches, found } } @@ -591,14 +591,14 @@ func (os *Objects) FindTwoMultiOrAdd(attribute Attribute, value AttributeValue, if attribute2 == NonExistingAttribute { // Lookup by one attribute - matches, found := os.GetIndex(attribute).Lookup(attributeValueToIndex(value)) + matches, found := os.GetIndex(attribute).Lookup(AttributeValueToIndex(value)) if found { os.objectmutex.Unlock() return matches, found } } else { // Lookup by two attributes - matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{attributeValueToIndex(value), attributeValueToIndex(value2)}) + matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{AttributeValueToIndex(value), AttributeValueToIndex(value2)}) if found { os.objectmutex.Unlock() return matches, found diff --git a/modules/engine/processing.go b/modules/engine/processing.go index 3c21f62..8007f56 100644 --- a/modules/engine/processing.go +++ b/modules/engine/processing.go @@ -64,7 +64,7 @@ func Merge(aos []*Objects) (*Objects, error) { for mergeobject, _ := range needsmerge { if mergeobject.HasAttr(UniqueSource) { - us := attributeValueToIndex(mergeobject.OneAttr(UniqueSource)) + us := AttributeValueToIndex(mergeobject.OneAttr(UniqueSource)) if sourcemap[us] == nil { sourcemap[us] = NewObjects() } @@ -95,7 +95,7 @@ func Merge(aos []*Objects) (*Objects, error) { pb.Add(1) // Here we'll deduplicate DNs, because sometimes schema and config context slips in twice if dn := addobject.OneAttr(DistinguishedName); dn != nil { - if existing, exists := dnindex.Lookup(attributeValueToIndex(dn)); exists { + if existing, exists := dnindex.Lookup(AttributeValueToIndex(dn)); exists { existing[0].AbsorbEx(addobject, true) continue } @@ -108,7 +108,7 @@ func Merge(aos []*Objects) (*Objects, error) { pb.Add(1) // Here we'll deduplicate DNs, because sometimes schema and config context slips in twice if dn := addobject.OneAttr(DistinguishedName); dn != nil { - if existing, exists := dnindex.Lookup(attributeValueToIndex(dn)); exists { + if existing, exists := dnindex.Lookup(AttributeValueToIndex(dn)); exists { existing[0].AbsorbEx(addobject, true) continue } diff --git a/modules/query/types.go b/modules/query/types.go index c74c2c5..8c32be3 100644 --- a/modules/query/types.go +++ b/modules/query/types.go @@ -143,10 +143,11 @@ type LowerStringAttribute engine.Attribute func (a LowerStringAttribute) Strings(o *engine.Object) []string { l := o.AttrRendered(engine.Attribute(a)) - for i, s := range l { - l[i] = strings.ToLower(s) + lo := make([]string, l.Len()) + for i, s := range l.Slice() { + lo[i] = strings.ToLower(s.String()) } - return l + return lo } type andquery struct { @@ -497,13 +498,13 @@ type hasStringMatch struct { } func (hsm hasStringMatch) Evaluate(a engine.Attribute, o *engine.Object) bool { - for _, value := range o.AttrRendered(a) { + for _, value := range o.AttrRendered(a).Slice() { if !hsm.casesensitive { - if strings.EqualFold(hsm.m, value) { + if strings.EqualFold(hsm.m, value.String()) { return true } } else { - if hsm.m == value { + if hsm.m == value.String() { return true } } @@ -530,13 +531,13 @@ type hasGlobMatch struct { } func (hgm hasGlobMatch) Evaluate(a engine.Attribute, o *engine.Object) bool { - for _, value := range o.AttrRendered(a) { + for _, value := range o.AttrRendered(a).Slice() { if !hgm.casesensitive { - if hgm.m.Match(strings.ToLower(value)) { + if hgm.m.Match(strings.ToLower(value.String())) { return true } } else { - if hgm.m.Match(value) { + if hgm.m.Match(value.String()) { return true } } @@ -557,8 +558,8 @@ type hasRegexpMatch struct { } func (hrm hasRegexpMatch) Evaluate(a engine.Attribute, o *engine.Object) bool { - for _, value := range o.AttrRendered(a) { - if hrm.m.MatchString(value) { + for _, value := range o.AttrRendered(a).Slice() { + if hrm.m.MatchString(value.String()) { return true } } @@ -596,13 +597,13 @@ func recursiveDNmatchFunc(o *engine.Object, a engine.Attribute, dn string, maxde return false } // Check all attribute values for match or ancestry - for _, value := range o.AttrRendered(a) { + for _, value := range o.AttrRendered(a).Slice() { // We're at the end - if strings.EqualFold(value, dn) { + if strings.EqualFold(value.String(), dn) { return true } // Perhaps parent matches? - if parent, found := ao.Find(activedirectory.DistinguishedName, engine.AttributeValueString(value)); found { + if parent, found := ao.Find(activedirectory.DistinguishedName, engine.AttributeValueString(value.String())); found { return recursiveDNmatchFunc(parent, a, dn, maxdepth-1, ao) } }