-
Notifications
You must be signed in to change notification settings - Fork 3
/
permission.go
143 lines (111 loc) · 3.17 KB
/
permission.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
package organization
import (
"errors"
"fmt"
"strings"
ldap "gopkg.in/ldap.v2"
)
// AddPermission to ldap server
func (org *Organization) AddPermission(name, description string, types []string, isUnit bool) (string, error) {
ts, _ := org.TypeByIDs(types)
if len(ts) != len(types) {
return ``, errors.New(`invalid types`)
}
id := generateNewID()
dn := org.dn(id, permissionCategory(isUnit))
aq := ldap.NewAddRequest(dn)
aq.Attribute(`objectClass`, []string{`permission`, `top`})
aq.Attribute(`cn`, []string{name})
aq.Attribute(`description`, []string{description})
aq.Attribute(`rbacType`, types)
return id, org.Add(aq)
}
// ModifyPermission in ldap
func (org *Organization) ModifyPermission(id, name, description string, types []string) error {
op, err := org.PermissionByID(id)
if err != nil {
return err
}
mq := ldap.NewModifyRequest(op[`dn`].(string))
if len(name) != 0 {
mq.Replace(`cn`, []string{name})
}
if len(description) != 0 {
mq.Replace(`description`, []string{description})
}
if types != nil {
mq.Replace(`rbacType`, types)
}
err = org.Modify(mq)
if err != nil {
return err
}
rids, _ := org.RoleIDsByPermissionID(id)
if len(rids) != 0 { // 有角色应用改权限
org.refreshRBACIfNeeded(op[`rbacType`].([]string), types)
}
return nil
}
// DelPermission in ldap
func (org *Organization) DelPermission(id string) error {
rids, err := org.RoleIDsByPermissionID(id)
if err != nil {
return err
}
if len(rids) > 0 {
return fmt.Errorf(`有角色/岗位引用当前权限 count %d`, len(rids))
}
op, err := org.PermissionByID(id)
if err != nil {
return err
}
dq := ldap.NewDelRequest(op[`dn`].(string), nil)
return org.Del(dq)
}
// PermissionByType all permission which contain this dolorestype
func (org *Organization) PermissionByType(dtype string, isUnit bool) ([]string, error) {
sq := ldap.NewSearchRequest(org.parentDN(permissionCategory(isUnit)),
ldap.ScopeSingleLevel,
ldap.DerefAlways, 0, 0, false,
fmt.Sprintf(`(rbacType=%s)`, dtype),
[]string{`id`}, nil)
sr, err := org.Search(sq)
if err != nil {
return nil, err
}
var ids []string
for _, entry := range sr.Entries {
ids = append(ids, entry.GetAttributeValue(`id`))
}
return ids, nil
}
// Permissions in ldap
func (org *Organization) Permissions(isUnit bool, pageSize uint32, cookie []byte) (*SearchResult, error) {
return org.searchPermission(``, org.parentDN(permissionCategory(isUnit)), pageSize, cookie)
}
// PermissionByIDs in ldap
func (org *Organization) PermissionByIDs(ids []string) (*SearchResult, error) {
filter, err := sqConvertIDsToFilter(ids)
if err != nil {
return nil, err
}
dn := fmt.Sprintf(`ou=permission, %s`, org.subffix)
return org.searchPermission(filter, dn, 0, nil)
}
// PermissionByID in ldap
func (org *Organization) PermissionByID(id string) (map[string]interface{}, error) {
rs, e := org.PermissionByIDs([]string{id})
if e != nil {
return nil, e
}
if len(rs.Data) == 0 {
return nil, fmt.Errorf(`[%s] permission doesn't exist`, id)
}
if len(rs.Data) > 1 {
return nil, fmt.Errorf(`[%s] found many permission`, id)
}
p := rs.Data[0]
dn := p[`dn`].(string)
p[`isUnit`] = strings.Contains(dn, `ou=unit`)
return p, nil
}