-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributemap.go
75 lines (59 loc) · 1.68 KB
/
attributemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ldapx
import (
"strings"
"github.com/go-ldap/ldap/v3"
)
// AttributeMap is a map of attributes.
type AttributeMap map[string]*ldap.EntryAttribute
// NewAttributeMap creates a new AttributeMap.
func NewAttributeMap() AttributeMap {
return make(AttributeMap)
}
// AttributeNames returns the names of the attributes in the map.
func (m AttributeMap) AttributeNames() []string {
names := make([]string, 0, len(m))
for _, a := range m {
names = append(names, a.Name)
}
return names
}
// Get returns the attribute with the given name.
func (m AttributeMap) Get(attr string) *ldap.EntryAttribute {
v := m[strings.ToLower(attr)]
return v
}
// Put adds the attribute to the map.
func (m AttributeMap) Put(attr string, value *ldap.EntryAttribute) {
m[strings.ToLower(attr)] = value
}
// PutEntryAttribute adds the attribute from the given LDAP entry to the map
func (m AttributeMap) PutEntryAttribute(value *ldap.EntryAttribute) {
m[strings.ToLower(value.Name)] = value
}
// Delete removes the attribute with the given name from the map.
func (m AttributeMap) Delete(attr string) {
delete(m, strings.ToLower(attr))
}
// Rename renames the attribute with the given name to the new name.
func (m AttributeMap) Rename(from, to string) {
// If the names are the same, do nothing
if strings.EqualFold(from, to) {
return
}
// Get the attribute
v := m[strings.ToLower(from)]
// If it doesn't exist, do nothing
if v == nil {
return
}
// Rename the attribute in the value
v.Name = to
// Add "new" version
m.PutEntryAttribute(v)
// And delete old attribute
m.Delete(from)
}
func (m AttributeMap) AttributeExists(attr string) bool {
_, ok := m[strings.ToLower(attr)]
return ok
}