-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathentity.go
121 lines (93 loc) · 2.3 KB
/
entity.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ldsview
import (
"strings"
)
type Entity struct {
attributes map[string]EntityAttribute
}
func NewEntity() Entity {
return Entity{
attributes: make(map[string]EntityAttribute),
}
}
func (e *Entity) AddAttribute(attr EntityAttribute) {
attrName := strings.ToLower(attr.Name)
existing, found := e.GetAttribute(attrName)
if !found {
e.attributes[attrName] = attr
return
}
existing.Value.Add(attr.Value.Values()...)
}
func (e Entity) IsEmpty() bool {
return len(e.attributes) == 0
}
func (e Entity) Groups() []string {
groupAttr, found := e.GetAttribute("memberOf")
if !found {
return []string{}
}
return groupAttr.Value.Values()
}
func (e Entity) GetDN() (EntityAttribute, bool) {
dn, found := e.GetAttribute("dn")
if !found {
dn, found = e.GetAttribute("distinguishedName")
}
return dn, found
}
func (e Entity) GetAllAttributeNames() []string {
names := make([]string, e.Size())
i := 0
for key := range e.attributes {
names[i] = key
i++
}
return names
}
func (e Entity) GetAllAttributes() []EntityAttribute {
attrs := make([]EntityAttribute, e.Size())
i := 0
for _, val := range e.attributes {
attrs[i] = val
}
return attrs
}
func (e *Entity) SetAttribute(attr EntityAttribute) {
attrName := strings.ToLower(attr.Name)
e.attributes[attrName] = attr
}
func (e Entity) GetAttribute(name string) (EntityAttribute, bool) {
val, found := e.attributes[strings.ToLower(name)]
return val, found
}
func (e Entity) Size() int {
return len(e.attributes)
}
func (e *Entity) decodeFromGeneralizedTime(attrName string) {
timeAttr, found := e.GetAttribute(attrName)
if !found {
return
}
origTime := timeAttr.Value.Values()[0]
decodedTime, _ := TimeFromADGeneralizedTime(origTime)
timeAttr.SetValue(decodedTime.String())
e.SetAttribute(timeAttr)
}
func (e *Entity) decodeFromADTimestamp(attrName string) {
timeAttr, found := e.GetAttribute(attrName)
if !found {
return
}
origTime := timeAttr.Value.Values()[0]
decodedTime := TimeFromADTimestamp(origTime)
timeAttr.SetValue(decodedTime.String())
e.SetAttribute(timeAttr)
}
func (e *Entity) DeocdeTimestamps() {
e.decodeFromGeneralizedTime("whenCreated")
e.decodeFromGeneralizedTime("whenChanged")
e.decodeFromADTimestamp("pwdLastSet")
e.decodeFromADTimestamp("lastLogon")
e.decodeFromADTimestamp("lastLogonTimestamp")
}