-
Notifications
You must be signed in to change notification settings - Fork 9
/
add_entry.go
109 lines (91 loc) · 2.19 KB
/
add_entry.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
package main
type AddEntry struct {
schemaMap *SchemaMap
dn *DN
attributes map[string]*SchemaValue
}
func NewAddEntry(schemaMap *SchemaMap, dn *DN) *AddEntry {
entry := &AddEntry{
schemaMap: schemaMap,
attributes: map[string]*SchemaValue{},
}
entry.SetDN(dn)
return entry
}
func (j *AddEntry) HasAttr(attrName string) bool {
s, ok := j.schemaMap.AttributeType(attrName)
if !ok {
return false
}
_, ok = j.attributes[s.Name]
return ok
}
func (j *AddEntry) SetDN(dn *DN) {
j.dn = dn
rdn := dn.RDN()
for k, v := range rdn {
// rdn is validated already
j.attributes[k], _ = NewSchemaValue(j.schemaMap, k, []string{v.Orig})
}
}
func (j *AddEntry) IsRoot() bool {
return j.dn.IsRoot()
}
func (j *AddEntry) DN() *DN {
return j.dn
}
func (j *AddEntry) ParentDN() *DN {
return j.dn.ParentDN()
}
func (j *AddEntry) IsDC() bool {
return j.dn.IsDC()
}
func (j *AddEntry) Validate() error {
// objectClass is required
if !j.HasAttr("objectClass") {
return NewObjectClassViolation()
}
// Validate objectClass
sv := j.attributes["objectClass"]
if err := j.schemaMap.ValidateObjectClass(sv.Orig(), j.attributes); err != nil {
return err
}
return nil
}
// Append to current value(s).
func (j *AddEntry) Add(attrName string, attrValue []string) error {
if len(attrValue) == 0 {
return nil
}
// Duplicate error is detected here
sv, err := NewSchemaValue(j.schemaMap, attrName, attrValue)
if err != nil {
return err
}
if sv.IsNoUserModificationWithMigrationDisabled() {
return NewNoUserModificationAllowedConstraintViolation(sv.Name())
}
return j.addsv(sv)
}
func (j *AddEntry) addsv(value *SchemaValue) error {
name := value.Name()
current, ok := j.attributes[name]
if !ok {
j.attributes[name] = value
return nil
} else {
// When adding the attribute with same value as both DN and attribute,
// we need to ignore the duplicate error.
current.Add(value)
}
return nil
}
func (j *AddEntry) Attrs() (map[string][]interface{}, map[string][]string) {
norm := make(map[string][]interface{}, len(j.attributes))
orig := make(map[string][]string, len(j.attributes))
for k, v := range j.attributes {
norm[k] = v.Norm()
orig[k] = v.Orig()
}
return norm, orig
}