|
|
@ -2,9 +2,9 @@ package sysModel |
|
|
|
|
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
"gin-vue-admin/init/qmsql" |
|
|
|
"github.com/casbin/casbin" |
|
|
|
gormadapter "github.com/casbin/gorm-adapter" |
|
|
|
"main/init/qmsql" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
|
|
|
@ -16,13 +16,11 @@ type CasbinModel struct { |
|
|
|
Method string `json:"method" gorm:"column:v2"` |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新权限
|
|
|
|
func (c *CasbinModel) CasbinPUpdata(AuthorityId string,Paths []string)error{ |
|
|
|
c.clearCasbin(0,AuthorityId) |
|
|
|
for _,v:=range Paths{ |
|
|
|
cm:= CasbinModel{ |
|
|
|
func (c *CasbinModel) CasbinPUpdata(AuthorityId string, Paths []string) error { |
|
|
|
c.clearCasbin(0, AuthorityId) |
|
|
|
for _, v := range Paths { |
|
|
|
cm := CasbinModel{ |
|
|
|
ID: 0, |
|
|
|
Ptype: "p", |
|
|
|
AuthorityId: AuthorityId, |
|
|
@ -30,7 +28,7 @@ func (c *CasbinModel) CasbinPUpdata(AuthorityId string,Paths []string)error{ |
|
|
|
Method: "POST", |
|
|
|
} |
|
|
|
addflag := c.AddCasbin(cm) |
|
|
|
if(addflag == false){ |
|
|
|
if addflag == false { |
|
|
|
return errors.New("存在相同api,添加失败,请联系管理员") |
|
|
|
} |
|
|
|
} |
|
|
@ -38,39 +36,39 @@ func (c *CasbinModel) CasbinPUpdata(AuthorityId string,Paths []string)error{ |
|
|
|
} |
|
|
|
|
|
|
|
// API更新随动
|
|
|
|
func (c *CasbinModel) CasbinApiUpdata(oldPath string,newPath string)error{ |
|
|
|
func (c *CasbinModel) CasbinApiUpdata(oldPath string, newPath string) error { |
|
|
|
var cs []CasbinModel |
|
|
|
err := qmsql.DEFAULTDB.Table("casbin_rule").Where("v1 = ?",oldPath).Find(&cs).Update("v1", newPath).Error |
|
|
|
err := qmsql.DEFAULTDB.Table("casbin_rule").Where("v1 = ?", oldPath).Find(&cs).Update("v1", newPath).Error |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//添加权限
|
|
|
|
func (c *CasbinModel) AddCasbin(cm CasbinModel) bool { |
|
|
|
e := Casbin() |
|
|
|
return e.AddPolicy( cm.AuthorityId, cm.Path, cm.Method) |
|
|
|
return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method) |
|
|
|
} |
|
|
|
|
|
|
|
//获取权限列表
|
|
|
|
func (c *CasbinModel) GetPolicyPathByAuthorityId(AuthorityId string)[]string { |
|
|
|
func (c *CasbinModel) GetPolicyPathByAuthorityId(AuthorityId string) []string { |
|
|
|
e := Casbin() |
|
|
|
var pathList []string |
|
|
|
list := e.GetFilteredPolicy(0, AuthorityId) |
|
|
|
for _,v:=range list{ |
|
|
|
for _, v := range list { |
|
|
|
pathList = append(pathList, v[1]) |
|
|
|
} |
|
|
|
return pathList |
|
|
|
} |
|
|
|
|
|
|
|
//清除匹配的权限
|
|
|
|
func (c *CasbinModel) clearCasbin(v int,p string) bool { |
|
|
|
func (c *CasbinModel) clearCasbin(v int, p string) bool { |
|
|
|
e := Casbin() |
|
|
|
return e.RemoveFilteredPolicy(v,p) |
|
|
|
return e.RemoveFilteredPolicy(v, p) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 自定义规则函数
|
|
|
|
func ParamsMatch(key1 string, key2 string) bool { |
|
|
|
k1arr := strings.Split(key1,"?") |
|
|
|
k1arr := strings.Split(key1, "?") |
|
|
|
return k1arr[0] == key2 |
|
|
|
} |
|
|
|
|
|
|
|