Code Monkey home page Code Monkey logo

Comments (8)

hsluoyz avatar hsluoyz commented on May 18, 2024

@llh4github please use English.

from gorm-adapter.

llh4github avatar llh4github commented on May 18, 2024

@hsluoyz Sorry,I describe the problem again in English.
When running the following code, I found a null pointer error. The error occurred in this line, e, err := casbin.NewEnforcer("./rbac_model.toml", a).

import (
	"log"
	"os"
        "testing"
	"time"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func TestC() {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second, // 慢 SQL 阈值
			LogLevel:      logger.Info, // Log level
			Colorful:      false,       // 禁用彩色打印
		},
	)
	db, err := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"),
		&gorm.Config{
			Logger: newLogger,
			NamingStrategy: schema.NamingStrategy{
				SingularTable: true, // 表名单数
				TablePrefix:   "",   // 表前缀
			},
		})
	if err != nil {
		println("数据库连接失败!")
	}
	// con1()
	con2(db)
	db.Debug()
	println("hello")
}
// 使用已有DB连接
func con2(db *gorm.DB) {
	a, err := gormadapter.NewAdapterByDB(db)
	if err != nil {
		println(err)
	}
	e, err := casbin.NewEnforcer("./rbac_model.toml", a)
	if err != nil {
		println(err)
	}
	e.LoadPolicy()
	// e.AddRoleForUser("tom", "admin")
	println(e.GetAdapter())

}

I found that the problem is a type conversion error in the source code of gorm-adapter.
Line 85 of the ennforcer.go file: err := e.InitWithAdapter(p0, p1.(persist.Adapter)).
The prompt information of the Line 85 is interface conversion: interface {} is *github.com/casbin/gorm-adapter/v3.Adapter, not github.com/casbin/casbin/v2/persist.Adapter

from gorm-adapter.

hsluoyz avatar hsluoyz commented on May 18, 2024

@llh4github please modify all your existing code, text and comment into English.

from gorm-adapter.

llh4github avatar llh4github commented on May 18, 2024
import (
	"log"
	"os"
        "testing"
	"time"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func TestC() {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second,  
			LogLevel:      logger.Info, // Log level
			Colorful:      false,        
		},
	)
	db, err := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"),
		&gorm.Config{
			Logger: newLogger,
			NamingStrategy: schema.NamingStrategy{
				SingularTable: true,  
				TablePrefix:   "",    
			},
		})
	if err != nil {
		println("Database connection failed!")
	}
	con2(db)
	db.Debug()
	println("hello")
}
//  Use gorm to connect to the database
func con2(db *gorm.DB) {
	a, err := gormadapter.NewAdapterByDB(db)
	if err != nil {
		println(err)
	}
	e, err := casbin.NewEnforcer("./rbac_model.toml", a)
	if err != nil {
		println(err)
	}
	e.LoadPolicy()
	// e.AddRoleForUser("tom", "admin")
	println(e.GetAdapter())

}

@hsluoyz Now, No Chinese characters.

from gorm-adapter.

llh4github avatar llh4github commented on May 18, 2024

#47 (comment)

When running the following code, I found a null pointer error. The error occurred in this line, e, err := casbin.NewEnforcer("./rbac_model.toml", a).

import (
	"log"
	"os"
        "testing"
	"time"

	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func TestC(t *testing.T) {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second,  
			LogLevel:      logger.Info, // Log level
			Colorful:      false,        
		},
	)
	db, err := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local"),
		&gorm.Config{
			Logger: newLogger,
			NamingStrategy: schema.NamingStrategy{
				SingularTable: true,  
				TablePrefix:   "",    
			},
		})
	if err != nil {
		println("Database connection failed!")
	}
	con2(db)
	db.Debug()
	println("hello")
}
//  Use gorm to connect to the database
func con2(db *gorm.DB) {
	a, err := gormadapter.NewAdapterByDB(db)
	if err != nil {
		println(err)
	}
	e, err := casbin.NewEnforcer("./rbac_model.toml", a)
	if err != nil {
		println(err)
	}
	e.LoadPolicy()
	// e.AddRoleForUser("tom", "admin")
	println(e.GetAdapter())

}

I found that the problem is a type conversion error in the source code of gorm-adapter.
Line 85 of the ennforcer.go file: err := e.InitWithAdapter(p0, p1.(persist.Adapter)).
The prompt information of the Line 85 is interface conversion: interface {} is *github.com/casbin/gorm-adapter/v3.Adapter, not github.com/casbin/casbin/v2/persist.Adapter.

from gorm-adapter.

hsluoyz avatar hsluoyz commented on May 18, 2024

@nodece @00LT00

from gorm-adapter.

00LT00 avatar 00LT00 commented on May 18, 2024

what's this e, err := casbin.NewEnforcer("./rbac_model.toml", a) ?
casbin's model file is "xxx.conf".
@llh4github

from gorm-adapter.

nodece avatar nodece commented on May 18, 2024

@llh4github I tested your code, it works fine.

one thing to note is here.

e, err := casbin.NewEnforcer("./rbac_model.toml", a) // the rbac_model.toml must exist.
if err != nil {
    println(err)  // miss return.
}

e.LoadPolicy() // when NewEnforcer returns `err != nil`, e is `nil`.

from gorm-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.