代码拉取完成,页面将自动刷新
/*
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{}}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。