casbin / xorm-adapter

Xorm adapter for Casbin
https://github.com/casbin/casbin
Apache License 2.0
384 stars 58 forks source link

panic: interface conversion: *xormadapter.Adapter is not persist.Adapter: missing method LoadPolicy #22

Closed mrchar closed 4 years ago

mrchar commented 4 years ago
package main

import (
    "github.com/casbin/casbin/v2"
    _ "github.com/go-sql-driver/mysql"

    "github.com/casbin/xorm-adapter"
)

func main() {
    // Initialize a Xorm adapter and use it in a Casbin enforcer:
    // The adapter will use the MySQL database named "casbin".
    // If it doesn't exist, the adapter will create it automatically.
    a, _ := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source. 

    // Or you can use an existing DB "abc" like this:
    // The adapter will use the table named "casbin_rule".
    // If it doesn't exist, the adapter will create it automatically.
    // a := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc", true)

    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

    // Load the policy from DB.
    e.LoadPolicy()

    // Check the permission.
    e.Enforce("alice", "data1", "read")

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    e.SavePolicy()
}
panic: interface conversion: *xormadapter.Adapter is not persist.Adapter: missing method LoadPolicy

goroutine 1 [running]:
github.com/casbin/casbin/v2.NewEnforcer(0xc00012bf00, 0x2, 0x2, 0x1c, 0x0, 0x0)
        /home/char/go/pkg/mod/github.com/casbin/casbin/v2@v2.1.2/enforcer.go:84 +0x143
main.main()
        /home/char/Documents/nova/dingyue/authorization/test/adapter/xorm.go:23 +0xde
mrchar commented 4 years ago

go.mod

module authorization

go 1.13

require (
    github.com/casbin/casbin v1.9.1
    github.com/casbin/casbin/v2 v2.1.2
    github.com/casbin/xorm-adapter v1.0.0
    github.com/go-sql-driver/mysql v1.4.1
    github.com/go-xorm/xorm v0.7.9 // indirect
    google.golang.org/appengine v1.6.5 // indirect
)
mrchar commented 4 years ago

I guess it's because xorm-adapter v2's go.mod incorrectly declares the module as xorm-adapter.

hsluoyz commented 4 years ago

Can you make a PR to fix it?

hsluoyz commented 4 years ago

@nodece

fengjihua commented 4 years ago

do NOT work, fix please

import "github.com/casbin/xorm-adapter/v2"

cannot load github.com/casbin/xorm-adapter/v2: cannot find module providing package github.com/casbin/xorm-adapter/v2

hsluoyz commented 4 years ago

@fengjihua can you make a fix PR?

mrchar commented 4 years ago

@hsluoyz can not fix with commit, maybe you should add a new tag like 2.0.1.

hsluoyz commented 4 years ago

Released: https://github.com/casbin/xorm-adapter/releases/tag/v2.0.1

fengjihua commented 4 years ago

Released: https://github.com/casbin/xorm-adapter/releases/tag/v2.0.1

work like a charm! awesome!