65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
map.go 3.11 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-06-24 20:11 . update
/*
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/application/registry/navigate"
)
type Map struct {
V map[string]*Map
Nav *navigate.Item
}
//Import 导入菜单(用户缓存结果)
func (m *Map) Import(navList *navigate.List) *Map {
if navList == nil {
return m
}
for _, nav := range *navList {
item := NewMap()
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, navTree *Map) *Map {
perms := strings.Split(permActions, `,`)
for _, perm := range perms {
arr := strings.Split(perm, `/`)
tree := navTree
result := m.V
var spath string
for _, a := range arr {
if _, y := result[a]; !y {
result[a] = NewMap()
}
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
}
//Check 检测权限
func (m *Map) Check(perm string) bool {
if m.Nav != nil && m.Nav.Unlimited {
return true
}
arr := strings.Split(perm, `/`)
result := m.V
for _, a := range arr {
v, y := result[a]
if !y {
return false
}
if v.Nav != nil && v.Nav.Unlimited {
return true
}
if _, y := v.V[`*`]; y {
return true
}
result = v.V
}
return true
}
func NewMap() *Map {
return &Map{V: map[string]*Map{}}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.1

搜索帮助