package casbins
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/casbin/casbin/v2"
|
|
xormadapter "github.com/casbin/xorm-adapter/v2"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetEnforcer(t *testing.T) {
|
|
|
|
driveSource := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;port=%d;encrypt=disable",
|
|
"127.0.0.1", "GAAS_MOM", "sa", "Leit2020", 1433)
|
|
|
|
a, err := xormadapter.NewAdapter("mssql", driveSource, true)
|
|
if err != nil {
|
|
log.Printf("连接数据库错误: %v", err)
|
|
return
|
|
}
|
|
e, err := casbin.NewEnforcer("D:\\GoWrok\\src\\leit.com\\LAPP_LF_MOM_BACKEND\\conf\\rbac_model.conf", a)
|
|
if err != nil {
|
|
log.Printf("casbin初始化: %v", err)
|
|
return
|
|
}
|
|
|
|
if err = e.LoadPolicy(); err == nil {
|
|
|
|
}
|
|
fmt.Println(err)
|
|
//ok,err:=e.AddPolicy("test222", "/admin/base/person/checklogin/{personId}/{workPlaceNr}", "GET")
|
|
//if err !=nil{
|
|
// log.Printf("AddPolicy错误: %v", err)
|
|
//}
|
|
//if ok{
|
|
// log.Printf("AddPolicy添加成功")
|
|
//}
|
|
ok2,err:=e.Enforce("目检", "/admin/base/person/checklogin/1/1", "GET")
|
|
fmt.Println(ok2)
|
|
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
|
|
}
|