sys_casbin.go 4.1 KB
Newer Older
1 2 3 4 5 6 7 8
package service

import (
	"errors"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"github.com/casbin/casbin/util"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9 10 11
	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	_ "github.com/go-sql-driver/mysql"
12 13 14 15 16
	"strings"
)

// @title    UpdateCasbin
// @description   update casbin authority, 更新casbin权限
17
// @auth                     (2020/04/05  20:22)
18 19 20
// @param     authorityId      string
// @param     casbinInfos      []CasbinInfo
// @return                     error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
21

22
func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	ClearCasbin(0, authorityId)
	for _, v := range casbinInfos {
		cm := model.CasbinModel{
			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, 添加权限
41 42
// @auth                     (2020/04/05  20:22)
// @param     cm              model.CasbinModel
43
// @return                    bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
44

45
func AddCasbin(cm model.CasbinModel) bool {
46
	e := Casbin()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
47 48
	success, _ := e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
	return success
49 50 51 52
}

// @title    UpdateCasbinApi
// @description   update casbin apis, API更新随动
53
// @auth                     (2020/04/05  20:22)
54 55
// @param     oldPath          string
// @param     newPath          string
56 57
// @param     oldMethod        string
// @param     newMethod        string
58
// @return                     error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59

60
func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
61
	err := global.GVA_DB.Table("casbin_rule").Model(&model.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
62 63 64
		"v1": newPath,
		"v2": newMethod,
	}).Error
65 66 67 68 69
	return err
}

// @title    GetPolicyPathByAuthorityId
// @description   get policy path by authorityId, 获取权限列表
70
// @auth                     (2020/04/05  20:22)
71 72
// @param     authorityId     string
// @return                    []string
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73

74
func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
75 76 77
	e := Casbin()
	list := e.GetFilteredPolicy(0, authorityId)
	for _, v := range list {
78 79 80
		pathMaps = append(pathMaps, request.CasbinInfo{
			Path:   v[1],
			Method: v[2],
81
		})
82
	}
83
	return pathMaps
84 85 86 87
}

// @title    ClearCasbin
// @description   清除匹配的权限
88
// @auth                     (2020/04/05  20:22)
89 90 91
// @param     v               int
// @param     p               string
// @return                    bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
92

93
func ClearCasbin(v int, p ...string) bool {
94
	e := Casbin()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
95 96
	success, _ := e.RemoveFilteredPolicy(v, p...)
	return success
97 98 99 100 101

}

// @title    Casbin
// @description   store to DB, 持久化到数据库  引入自定义规则
102
// @auth                     (2020/04/05  20:22)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
103

104
func Casbin() *casbin.Enforcer {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
105 106 107
	admin := global.GVA_CONFIG.Mysql
	a, _ := gormadapter.NewAdapter(global.GVA_CONFIG.System.DbType, admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname, true)
	e, _ := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
108 109 110 111 112 113 114
	e.AddFunction("ParamsMatch", ParamsMatchFunc)
	_ = e.LoadPolicy()
	return e
}

// @title    ParamsMatch
// @description   customized rule, 自定义规则函数
115
// @auth                     (2020/04/05  20:22)
116 117 118
// @param     fullNameKey1    string
// @param     key2            string
// @return                    bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
119

120 121
func ParamsMatch(fullNameKey1 string, key2 string) bool {
	key1 := strings.Split(fullNameKey1, "?")[0]
122
	// 剥离路径后再使用casbin的keyMatch2
123 124 125 126 127
	return util.KeyMatch2(key1, key2)
}

// @title    ParamsMatchFunc
// @description   customized function, 自定义规则函数
128
// @auth                     (2020/04/05  20:22)
129 130 131
// @param     args            ...interface{}
// @return                    interface{}
// @return                    error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
132

133 134 135 136
func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
	name1 := args[0].(string)
	name2 := args[1].(string)

137
	return ParamsMatch(name1, name2), nil
138
}