-
Notifications
You must be signed in to change notification settings - Fork 3
/
type.go
149 lines (122 loc) · 3.39 KB
/
type.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package organization
import (
"errors"
"fmt"
"strings"
ldap "gopkg.in/ldap.v2"
)
// AddType desgined to add a new dolresType
func (org *Organization) AddType(name, description string, isUnit bool) (string, error) {
id := generateNewID()
dn := org.dn(id, typeCategory(isUnit))
aq := ldap.NewAddRequest(dn)
aq.Attribute(`objectClass`, []string{`doloresType`, `top`})
aq.Attribute(`cn`, []string{name})
aq.Attribute(`description`, []string{description})
return id, org.Add(aq)
}
// ModifyType update name or description of doloresType
func (org *Organization) ModifyType(id string, name, description string, isUnit bool) error {
dn := org.dn(id, typeCategory(isUnit))
mq := ldap.NewModifyRequest(dn)
if len(name) != 0 {
mq.Replace(`cn`, []string{name})
}
if len(description) != 0 {
mq.Replace(`description`, []string{description})
}
return org.Modify(mq)
}
// DelType by id
func (org *Organization) DelType(id string, isUnit bool) error {
pIDs, e := org.PermissionByType(id, isUnit)
if e != nil {
return e
}
if len(pIDs) != 0 {
return fmt.Errorf(`有权限规则包含此类型,请先修改权限规则 count: %d`, len(pIDs))
}
if isUnit {
us, e := org.UnitIDsByTypeIDs([]string{id})
if e != nil {
return e
}
if len(us) != 0 {
return fmt.Errorf(`有部门属于此类型,请先修改部门所属类型 count: %d`, len(pIDs))
}
} else {
// 通过Type 找人
mIDs, e := org.MemberIDsByTypeIDs([]string{id})
if e != nil {
return e
}
if len(mIDs) != 0 {
return fmt.Errorf(`有员工属于此类型,请先修改员工所属类型 count: %d`, len(pIDs))
}
}
dn := org.dn(id, typeCategory(isUnit))
dq := ldap.NewDelRequest(dn, nil)
return org.Del(dq)
}
// Types in ldap server
func (org *Organization) Types(isUnit bool, pageSize uint32, cookie []byte) (*SearchResult, error) {
sq := &searchRequest{
org.parentDN(typeCategory(isUnit)),
`(objectClass=doloresType)`,
[]string{`id`, `cn`, `description`, `modifyTimestamp`}, nil,
pageSize,
cookie}
return org.search(sq)
}
// TypeByIDs ...
func (org *Organization) TypeByIDs(ids []string) ([]map[string]interface{}, error) {
filter, err := sqConvertIDsToFilter(ids)
if err != nil {
return nil, err
}
dn := fmt.Sprintf(`ou=type,%s`, org.subffix)
sq := ldap.NewSearchRequest(dn,
ldap.ScopeWholeSubtree,
ldap.DerefAlways,
0, 0, false, filter, []string{`id`, `cn`, `description`}, nil)
sr, err := org.Search(sq)
if err != nil {
return nil, err
}
var result []map[string]interface{}
for _, e := range sr.Entries {
isUnit := false
if strings.Contains(e.DN, `ou=unit`) {
isUnit = true
}
result = append(result, map[string]interface{}{
`id`: e.GetAttributeValue(`id`),
`cn`: e.GetAttributeValue(`cn`),
`description`: e.GetAttributeValue(`description`),
`isUnit`: isUnit,
})
}
if result == nil {
return nil, errors.New(`not found`)
}
return result, nil
}
// TypeByID ...
func (org *Organization) TypeByID(id string) (map[string]interface{}, error) {
types, err := org.TypeByIDs([]string{id})
if err != nil {
return nil, err
}
if len(types) != 1 {
return nil, errors.New(`found many types`)
}
return types[0], nil
}
// TypeByPermissionID ...
func (org *Organization) TypeByPermissionID(id string) ([]map[string]interface{}, error) {
p, e := org.PermissionByID(id)
if e != nil {
return nil, e
}
return org.TypeByIDs(p[`rbacType`].([]string))
}