package casbins
|
|
|
|
import (
|
|
"LAPP_SJA_ME/conf"
|
|
"fmt"
|
|
"github.com/casbin/casbin/v2"
|
|
xormadapter "github.com/casbin/xorm-adapter/v2"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetEnforcer(t *testing.T) {
|
|
|
|
c := conf.MasterDbConfig
|
|
driveSource := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;port=%d;encrypt=disable",
|
|
c.Host, c.DbName, c.User, c.Pwd, c.Port)
|
|
|
|
a, err := xormadapter.NewAdapter(conf.DriverName, driveSource, true)
|
|
if err != nil {
|
|
log.Printf("连接数据库错误: %v", err)
|
|
return
|
|
}
|
|
|
|
e, err := casbin.NewEnforcer("conf/rbac_model.conf", a)
|
|
if err != nil {
|
|
log.Printf("casbin初始化: %v", err)
|
|
return
|
|
}
|
|
|
|
if err = e.LoadPolicy(); err == nil {
|
|
|
|
}
|
|
//ok,err:=e.AddPolicy("admin", "admin/users/add", "POST")
|
|
//if err !=nil{
|
|
// log.Printf("AddPolicy错误: %v", err)
|
|
//}
|
|
//if ok{
|
|
// log.Printf("AddPolicy添加成功")
|
|
//}
|
|
ok2,err:=e.Enforce("test222", "/admin/users/userlist", "GET")
|
|
if !ok2{
|
|
fmt.Println("权限不通过")
|
|
}else{
|
|
fmt.Println("权限通过")
|
|
}
|
|
|
|
}
|
|
|
|
func Test2(t *testing.T) {
|
|
if strings.Contains("/public/uploadfile/admin.jpg","/public/uploadfile") {
|
|
fmt.Println("权限通过")
|
|
}else{
|
|
fmt.Println("权限不通过")
|
|
}
|
|
|
|
}
|
|
|
|
func Test(t *testing.T) {
|
|
rutes := []string{"/sysMenu","/user/getuserinfo","/public/uploadfile"}
|
|
ok := IsContain(rutes,"/public/uploadfile/admin.jpg")
|
|
if !ok {
|
|
fmt.Println("权限不通过")
|
|
}else{
|
|
fmt.Println("权限通过")
|
|
}
|
|
|
|
}
|
|
func IsContain(items []string, item string) bool {
|
|
for _, eachItem := range items {
|
|
if eachItem == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|