-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
map.go
216 lines (201 loc) · 4.44 KB
/
map.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package perm
import (
"regexp"
"strings"
"github.com/admpub/nging/v4/application/registry/navigate"
)
type Map struct {
V map[string]*Map `json:",omitempty" xml:",omitempty"`
Nav *navigate.Item `json:",omitempty" xml:",omitempty"`
cached *Map
}
//Import 导入菜单(用户缓存结果)
func (m *Map) Import(navList *navigate.List) *Map {
if navList == nil {
return m
}
for _, nav := range *navList {
item := NewMap(m.cached)
item.Nav = nav
if _, ok := m.V[nav.Action]; ok {
panic(`The navigate name conflicts. Already existed name: ` + nav.Action)
}
m.V[nav.Action] = item
item.Import(nav.Children)
}
return m
}
func BuildPermActions(values []string) string {
var permActions string
if len(values) > 0 && values[0] == `*` {
permActions = `*`
return permActions
}
var prefix string
for _, v := range values {
length := len(v)
var suffix string
if length > 2 {
suffix = v[length-2:]
}
if suffix == `/*` {
if len(prefix) > 0 {
prefix += `|`
}
prefix += regexp.QuoteMeta(v[0 : length-2])
if len(permActions) > 0 {
permActions += `,`
}
permActions += v
continue
}
if len(prefix) > 0 {
re := regexp.MustCompile(`^(` + prefix + `)`)
if re.MatchString(v) {
continue
}
}
if len(permActions) > 0 {
permActions += `,`
}
permActions += v
}
return permActions
}
//Parse 解析用户获取的权限
func (m *Map) Parse(permActions string) *Map {
perms := strings.Split(permActions, `,`)
for _, perm := range perms {
arr := strings.Split(perm, `/`)
tree := m.cached
result := m.V
var spath string
for _, a := range arr {
if _, y := result[a]; !y {
result[a] = NewMap(m.cached)
}
if a == `*` { //"*"是最后一个字符
break
}
if mp, y := tree.V[a]; y {
result[a].Nav = mp.Nav
spath = ``
tree = mp
} else {
if len(spath) > 0 {
spath += `/`
}
spath += a
if mp, y := tree.V[spath]; y {
result[a].Nav = mp.Nav
spath = ``
tree = mp
}
}
result = result[a].V
}
}
return m
}
func (m *Map) checkByNav(perm string) bool {
if m.Nav != nil && m.Nav.Unlimited {
return true
}
arr := strings.Split(perm, `/`)
navResult := m.V
var prefix string
for _, a := range arr {
key := prefix + a
navV, hasNav := navResult[key]
if !hasNav {
prefix += key + `/`
continue
}
prefix = ``
if navV.Nav != nil && navV.Nav.Unlimited {
return true
}
navResult = navV.V
}
return false
}
//Check 检测权限
//perm: /a/b/c
func (m *Map) Check(perm string) bool {
if m.cached == nil || m == m.cached {
return m.checkByNav(perm)
}
return m.checkByChecked(perm, m.cached)
}
func (m *Map) checkByChecked(perm string, nav *Map) bool {
if m.Nav != nil && m.Nav.Unlimited {
return true
}
arr := strings.Split(perm, `/`)
result := m.V
navResult := nav.V
hasPerm := true
var prefix string
for _, a := range arr {
key := prefix + a
navV, hasNav := navResult[key]
if !hasNav {
if hasPerm {
var v *Map
v, hasPerm = result[a]
if hasPerm {
if v.Nav != nil && v.Nav.Unlimited {
return true
}
if _, y := v.V[`*`]; y {
return true
}
result = v.V
}
}
prefix += key + `/`
continue
}
prefix = ``
if !hasPerm {
if navV.Nav != nil && navV.Nav.Unlimited {
return true
}
navResult = navV.V
continue
}
var v *Map
v, hasPerm = result[a]
if hasPerm {
if v.Nav != nil && v.Nav.Unlimited {
return true
}
if _, y := v.V[`*`]; y {
return true
}
result = v.V
}
if navV.Nav != nil && navV.Nav.Unlimited {
return true
}
navResult = navV.V
}
return hasPerm
}
func NewMap(cached *Map) *Map {
return &Map{V: map[string]*Map{}, cached: cached}
}