sys_casbin.go 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package service

import (
	"errors"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"github.com/casbin/casbin"
	"github.com/casbin/casbin/util"
	gormadapter "github.com/casbin/gorm-adapter"
	"strings"
)

// @title    UpdateCasbin
// @description   update casbin authority, 更新casbin权限
16
// @auth                     (2020/04/05  20:22)
17 18 19
// @param     authorityId      string
// @param     casbinInfos      []CasbinInfo
// @return                     error
20
func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	ClearCasbin(0, authorityId)
	for _, v := range casbinInfos {
		cm := model.CasbinModel{
			ID:          0,
			Ptype:       "p",
			AuthorityId: authorityId,
			Path:        v.Path,
			Method:      v.Method,
		}
		addflag := AddCasbin(cm)
		if addflag == false {
			return errors.New("存在相同api,添加失败,请联系管理员")
		}
	}
	return nil
}

// @title    AddCasbin
// @description   add casbin authority, 添加权限
40 41
// @auth                     (2020/04/05  20:22)
// @param     cm              model.CasbinModel
42
// @return                    bool
43
func AddCasbin(cm model.CasbinModel) bool {
44 45 46 47 48 49
	e := Casbin()
	return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
}

// @title    UpdateCasbinApi
// @description   update casbin apis, API更新随动
50
// @auth                     (2020/04/05  20:22)
51 52
// @param     oldPath          string
// @param     newPath          string
53 54
// @param     oldMethod        string
// @param     newMethod        string
55
// @return                     error
56
func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
57
	var cs []model.CasbinModel
58
	err := global.GVA_DB.Table("casbin_rule").Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Find(&cs).Updates(map[string]string{
59 60 61
		"v1": newPath,
		"v2": newMethod,
	}).Error
62 63 64 65 66
	return err
}

// @title    GetPolicyPathByAuthorityId
// @description   get policy path by authorityId, 获取权限列表
67
// @auth                     (2020/04/05  20:22)
68 69
// @param     authorityId     string
// @return                    []string
70
func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []map[string]string) {
71 72 73
	e := Casbin()
	list := e.GetFilteredPolicy(0, authorityId)
	for _, v := range list {
74 75 76 77
		pathMaps = append(pathMaps, map[string]string{
			"path":   v[1],
			"method": v[2],
		})
78
	}
79
	return pathMaps
80 81 82 83
}

// @title    ClearCasbin
// @description   清除匹配的权限
84
// @auth                     (2020/04/05  20:22)
85 86 87
// @param     v               int
// @param     p               string
// @return                    bool
88
func ClearCasbin(v int, p ...string) bool {
89
	e := Casbin()
90
	return e.RemoveFilteredPolicy(v, p...)
91 92 93 94 95

}

// @title    Casbin
// @description   store to DB, 持久化到数据库  引入自定义规则
96
// @auth                     (2020/04/05  20:22)
97 98 99 100 101 102 103 104 105 106
func Casbin() *casbin.Enforcer {
	a := gormadapter.NewAdapterByDB(global.GVA_DB)
	e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
	e.AddFunction("ParamsMatch", ParamsMatchFunc)
	_ = e.LoadPolicy()
	return e
}

// @title    ParamsMatch
// @description   customized rule, 自定义规则函数
107
// @auth                     (2020/04/05  20:22)
108 109 110 111 112 113 114 115 116 117 118
// @param     fullNameKey1    string
// @param     key2            string
// @return                    bool
func ParamsMatch(fullNameKey1 string, key2 string) bool {
	key1 := strings.Split(fullNameKey1, "?")[0]
	//剥离路径后再使用casbin的keyMatch2
	return util.KeyMatch2(key1, key2)
}

// @title    ParamsMatchFunc
// @description   customized function, 自定义规则函数
119
// @auth                     (2020/04/05  20:22)
120 121 122 123 124 125 126 127
// @param     args            ...interface{}
// @return                    interface{}
// @return                    error
func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
	name1 := args[0].(string)
	name2 := args[1].(string)

	return (bool)(ParamsMatch(name1, name2)), nil
128
}