| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package permlib
- import (
- "strings"
- "github.com/xiaozi/permlib/pb"
- )
- func (e *Engine) collectPerms() []*pb.PermItem {
- seen := make(map[string]bool)
- var perms []*pb.PermItem
- add := func(code, name string) {
- if seen[code] {
- return
- }
- seen[code] = true
- if name == "" {
- name = generatePermName(code)
- }
- perms = append(perms, &pb.PermItem{Code: code, Name: name})
- }
- for _, decl := range e.staticPerms {
- add(decl.Code, decl.Name)
- dataCode := apiToDataCode(decl.Code)
- if dataCode != "" {
- add(dataCode, "")
- }
- }
- return perms
- }
- func apiToDataCode(apiCode string) string {
- if !strings.HasPrefix(apiCode, "api:") {
- return ""
- }
- return "data:" + apiCode[4:]
- }
- func generatePermName(code string) string {
- parts := strings.Split(code, ":")
- if len(parts) < 2 {
- return code
- }
- nameMap := map[string]string{
- "read": "读取",
- "write": "写入",
- "create": "创建",
- "update": "更新",
- "delete": "删除",
- "list": "列表",
- "detail": "详情",
- }
- var result []string
- for i := 1; i < len(parts); i++ {
- p := parts[i]
- if mapped, ok := nameMap[p]; ok {
- result = append(result, mapped)
- } else {
- result = append(result, p)
- }
- }
- return strings.Join(result, "-")
- }
|