sys_casbin.go 3.9 KB
Newer Older
1
package system
2 3 4

import (
	"errors"
S
songzhibin97 已提交
5 6 7
	"strings"
	"sync"

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

m0_50812349's avatar
m0_50812349 已提交
17 18 19 20 21
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCasbin
//@description: 更新casbin权限
//@param: authorityId string, casbinInfos []request.CasbinInfo
//@return: error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
22

23 24 25 26 27 28 29
type CasbinService struct {
}

var CasbinServiceApp = new(CasbinService)

func (casbinService *CasbinService) UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
	casbinService.ClearCasbin(0, authorityId)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
30
	rules := [][]string{}
31
	for _, v := range casbinInfos {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
32
		cm := system.CasbinModel{
33 34 35 36 37
			Ptype:       "p",
			AuthorityId: authorityId,
			Path:        v.Path,
			Method:      v.Method,
		}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38
		rules = append(rules, []string{cm.AuthorityId, cm.Path, cm.Method})
39
	}
40
	e := casbinService.Casbin()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
41 42 43 44 45
	success, _ := e.AddPolicies(rules)
	if success == false {
		return errors.New("存在相同api,添加失败,请联系管理员")
	}
	return nil
46 47
}

m0_50812349's avatar
m0_50812349 已提交
48 49 50 51 52
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCasbinApi
//@description: API更新随动
//@param: oldPath string, newPath string, oldMethod string, newMethod string
//@return: error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
53

54
func (casbinService *CasbinService) UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
	err := global.GVA_DB.Table("casbin_rule").Model(&system.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
56 57 58
		"v1": newPath,
		"v2": newMethod,
	}).Error
59 60 61
	return err
}

m0_50812349's avatar
m0_50812349 已提交
62 63 64 65 66 67
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetPolicyPathByAuthorityId
//@description: 获取权限列表
//@param: authorityId string
//@return: pathMaps []request.CasbinInfo

68 69
func (casbinService *CasbinService) GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
	e := casbinService.Casbin()
70 71
	list := e.GetFilteredPolicy(0, authorityId)
	for _, v := range list {
72 73 74
		pathMaps = append(pathMaps, request.CasbinInfo{
			Path:   v[1],
			Method: v[2],
75
		})
76
	}
77
	return pathMaps
78 79
}

m0_50812349's avatar
m0_50812349 已提交
80 81 82 83 84
//@author: [piexlmax](https://github.com/piexlmax)
//@function: ClearCasbin
//@description: 清除匹配的权限
//@param: v int, p ...string
//@return: bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85

86 87
func (casbinService *CasbinService) ClearCasbin(v int, p ...string) bool {
	e := casbinService.Casbin()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
88 89
	success, _ := e.RemoveFilteredPolicy(v, p...)
	return success
90 91 92

}

m0_50812349's avatar
m0_50812349 已提交
93 94 95 96
//@author: [piexlmax](https://github.com/piexlmax)
//@function: Casbin
//@description: 持久化到数据库  引入自定义规则
//@return: *casbin.Enforcer
Mr.奇淼('s avatar
Mr.奇淼( 已提交
97

98 99 100 101 102
var (
	syncedEnforcer *casbin.SyncedEnforcer
	once           sync.Once
)

103
func (casbinService *CasbinService) Casbin() *casbin.SyncedEnforcer {
104 105 106
	once.Do(func() {
		a, _ := gormadapter.NewAdapterByDB(global.GVA_DB)
		syncedEnforcer, _ = casbin.NewSyncedEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
107
		syncedEnforcer.AddFunction("ParamsMatch", casbinService.ParamsMatchFunc)
108 109 110
	})
	_ = syncedEnforcer.LoadPolicy()
	return syncedEnforcer
111 112
}

m0_50812349's avatar
m0_50812349 已提交
113 114 115 116 117
//@author: [piexlmax](https://github.com/piexlmax)
//@function: ParamsMatch
//@description: 自定义规则函数
//@param: fullNameKey1 string, key2 string
//@return: bool
Mr.奇淼('s avatar
Mr.奇淼( 已提交
118

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

m0_50812349's avatar
m0_50812349 已提交
125 126 127 128 129
//@author: [piexlmax](https://github.com/piexlmax)
//@function: ParamsMatchFunc
//@description: 自定义规则函数
//@param: args ...interface{}
//@return: interface{}, error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
130

131
func (casbinService *CasbinService) ParamsMatchFunc(args ...interface{}) (interface{}, error) {
132 133 134
	name1 := args[0].(string)
	name2 := args[1].(string)

135
	return casbinService.ParamsMatch(name1, name2), nil
136
}