-
Notifications
You must be signed in to change notification settings - Fork 1
/
permissions_preset.go
91 lines (78 loc) · 2.34 KB
/
permissions_preset.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
package xrole
import (
"strings"
"github.com/admpub/nging/v5/application/library/perm"
"github.com/admpub/webx/application/handler/frontend/user/navigate"
"github.com/webx-top/echo"
)
func PermPageGenerator(ctx echo.Context) (string, error) {
return perm.BuildPermActions(ctx.FormValues(`permAction[]`)), nil
}
func PermPageChecker(ctx echo.Context, parsed interface{}, current string) (interface{}, error) {
current = strings.TrimPrefix(current, `/`)
if len(current) == 0 {
return NavTreeCached().Check(current), nil
}
//echo.Dump(parsed)
permPages, ok := parsed.(*perm.Map)
if !ok {
return false, nil
}
return permPages.Check(current), nil
}
func PermPageParser(ctx echo.Context, rule string) (interface{}, error) {
navTree := NavTreeCached()
permPages := perm.NewMap(navTree)
permPages.Parse(rule)
return permPages, nil
}
var PermPageList = func(ctx echo.Context) ([]interface{}, error) {
return nil, nil
}
func PermPageOnRender(ctx echo.Context) error {
ctx.Set(`customerLeftNavigate`, navigate.LeftNavigate)
ctx.Set(`customerTopNavigate`, navigate.TopNavigate)
return nil
}
func PermBehaviorGenerator(ctx echo.Context) (string, error) {
values := map[string][]string{}
for _, permName := range ctx.FormValues(`permBehavior[]`) {
values[permName] = ctx.FormValues(`permBehaviorConfig[` + permName + `]`)
}
if len(values) == 0 {
return ``, nil
}
return perm.SerializeBehaviorValues(values, Behaviors)
}
func PermBehaviorChecker(ctx echo.Context, parsed interface{}, current string) (interface{}, error) {
permBehaviors, ok := parsed.(perm.BehaviorPerms)
if !ok {
return &perm.CheckedBehavior{}, nil
}
return permBehaviors.CheckBehavior(current), nil
}
func PermBehaviorParser(ctx echo.Context, rule string) (interface{}, error) {
return perm.ParseBehavior(rule, Behaviors)
}
func PermBehaviorList(ctx echo.Context) ([]interface{}, error) {
behaviors := Behaviors.Slice()
list := make([]interface{}, len(behaviors))
for k, v := range behaviors {
list[k] = v
}
return list, nil
}
func PermBehaviorOnRender(ctx echo.Context) error {
behaviorList, err := PermBehaviorList(ctx)
if err != nil {
return err
}
ctx.Set(`customerBehaviorList`, behaviorList)
return nil
}
func PermBehaviorIsValid(ctx echo.Context) bool {
if list, ok := ctx.Get(`customerBehaviorList`).([]interface{}); ok {
return len(list) > 0
}
return false
}