Comments (8)
@llh4github please use English.
from gorm-adapter.
@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.
@llh4github please modify all your existing code, text and comment into English.
from gorm-adapter.
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.
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.
from gorm-adapter.
what's this e, err := casbin.NewEnforcer("./rbac_model.toml", a)
?
casbin's model file is "xxx.conf".
@llh4github
from gorm-adapter.
@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)
- change default v7 to v5 HOT 9
- [Proposal] customizability CasbinRule structure HOT 2
- question: dbspecified not works when specify dbname in dsn HOT 4
- [bug] Resolver mysql HOT 5
- there is a problem with the processing of error HOT 2
- Support SQL Server in gorm-adapter HOT 1
- [suggest]No physical deletion HOT 2
- [feature] create if not exist for Update APIs HOT 6
- "accessible by" feature for ORM integrations HOT 4
- Support for connection pools HOT 4
- Adapter Transaction db table can not set HOT 2
- why not use Transaction to SavePolicy HOT 6
- Understanding how to correctly update policies in a database HOT 4
- [Question] Handle to handle policy_definition has more than 6 items HOT 4
- How to setup a casbin watcher along with the the Gorm adapter HOT 4
- Add GetImplicitUsersForResourceByDomain() API HOT 2
- use RemovePolicy() API Debug HOT 3
- policy get loaded twice HOT 4
- id increase when savePolicy HOT 6
- Unit test when using Transaction HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gorm-adapter.