You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.5 KiB

package service
import (
"bkb-seller/global"
"bkb-seller/model"
"bkb-seller/model/request"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/util"
gormadapter "github.com/casbin/gorm-adapter/v3"
_ "github.com/go-sql-driver/mysql"
)
//@function: UpdateCasbin
//@description: 更新casbin权限
//@param: authorityId string, casbinInfos []request.CasbinInfo
//@return: error
func UpdateCasbin(authorityId uint, appid string, casbinInfos []request.CasbinInfo) error {
ClearCasbin(0, "api", appid, strconv.Itoa(int(authorityId)))
rules := [][]string{}
for _, v := range casbinInfos {
cm := model.CasbinModel{
Ptype: "p",
AuthorityId: authorityId,
Path: v.Path,
Method: v.Method,
}
rules = append(rules, []string{"api", appid, strconv.Itoa(int(cm.AuthorityId)), cm.Path, cm.Method})
}
e := Casbin()
success, _ := e.AddPolicies(rules)
if success == false {
return errors.New("存在相同api,添加失败,请联系管理员")
}
return nil
}
//@function: UpdateCasbinApi
//@description: API更新随动
//@param: oldPath string, newPath string, oldMethod string, newMethod string
//@return: error
func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod, appid string) error {
err := global.MG_DB.Table("casbin_rule").Model(&model.CasbinModel{}).Where("appid=? and v3 = ? AND v4 = ?", appid, oldPath, oldMethod).Updates(map[string]interface{}{
"v3": newPath,
"v4": newMethod,
}).Error
return err
}
//@function: GetPolicyPathByAuthorityId
//@description: 获取权限列表
//@param: authorityId string
//@return: pathMaps []request.CasbinInfo
func GetPolicyPathByAuthorityId(authorityId uint) (pathMaps []request.CasbinInfo) {
e := Casbin()
list := e.GetFilteredPolicy(0, strconv.Itoa(int(authorityId)))
for _, v := range list {
pathMaps = append(pathMaps, request.CasbinInfo{
Path: v[1],
Method: v[2],
})
}
return pathMaps
}
//@function: ClearCasbin
//@description: 清除匹配的权限
//@param: v int, p ...string
//@return: bool
func ClearCasbin(v int, p ...string) bool {
e := Casbin()
success, _ := e.RemoveFilteredPolicy(v, p...)
return success
}
// 初始化商家角色
func InitSellerRole(userID, roleID, Appid string) {
e := Casbin()
e.AddRoleForUserInDomain(userID, roleID, Appid)
}
//@function: Casbin
//@description: 持久化到数据库 引入自定义规则
//@return: *casbin.Enforcer
var (
syncedEnforcer *casbin.SyncedEnforcer
once sync.Once
)
func Casbin() *casbin.SyncedEnforcer {
once.Do(func() {
var err error
a, _ := gormadapter.NewAdapterByDB(global.MG_DB)
syncedEnforcer, err = casbin.NewSyncedEnforcer(global.MG_CONFIG.Casbin.ModelPath, a)
if err != nil {
fmt.Println("casbin.NewSyncedEnforcer err", err)
}
syncedEnforcer.AddFunction("ParamsMatch", ParamsMatchFunc)
})
_ = syncedEnforcer.LoadPolicy()
return syncedEnforcer
}
//@function: ParamsMatch
//@description: 自定义规则函数
//@param: fullNameKey1 string, key2 string
//@return: bool
func ParamsMatch(fullNameKey1 string, key2 string) bool {
key1 := strings.Split(fullNameKey1, "?")[0]
// 剥离路径后再使用casbin的keyMatch2
return util.KeyMatch2(key1, key2)
}
//@function: ParamsMatchFunc
//@description: 自定义规则函数
//@param: args ...interface{}
//@return: interface{}, error
func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
name1 := args[0].(string)
name2 := args[1].(string)
return ParamsMatch(name1, name2), nil
}