casbin / xorm-adapter

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

Missing method UpdateFilteredPolicies #44

Closed ghost closed 3 years ago

ghost commented 3 years ago

I am using xorm-adapter and mysql together to update policy and group policy, but here the error occurred:

panic: interface conversion: xormadapter.Adapter is not persist.UpdatableAdapter: missing method UpdateFilteredPolicies goroutine 1 [running]: github.com/casbin/casbin/v2.(Enforcer).updatePolicy(0xc000132e00, 0x867e9b, 0x1, 0x867e9b, 0x1, 0xc0000c69e0, 0x2, 0x2, 0xc0000c6a00, 0x2, ...) /usr/local/go/bin/pkg/mod/github.com/casbin/casbin/v2@v2.35.0/internal_api.go:159 +0x459 github.com/casbin/casbin/v2.(Enforcer).UpdateNamedGroupingPolicy(...) /usr/local/go/bin/pkg/mod/github.com/casbin/casbin/v2@v2.35.0/management_api.go:322 github.com/casbin/casbin/v2.(Enforcer).UpdateGroupingPolicy(0xc000132e00, 0xc0000c69e0, 0x2, 0x2, 0xc0000c6a00, 0x2, 0x2, 0xc0000bf700, 0x0, 0x0) /usr/local/go/bin/pkg/mod/github.com/casbin/casbin/v2@v2.35.0/management_api.go:318 +0xa5

And if I use a csv file above is not happened.

ghost commented 3 years ago

Here is my go.mod:

go 1.16

require ( github.com/EntropyPool/entropy-logger v0.0.0-20210320022718-3091537e035f github.com/NpoolRD/http-daemon v0.0.0-20210505073728-a1d91b8af9df github.com/casbin/casbin v1.9.1 github.com/casbin/casbin/v2 v2.36.1 github.com/casbin/xorm-adapter/v2 v2.3.2 github.com/go-sql-driver/mysql v1.6.0 github.com/urfave/cli/v2 v2.3.0 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 )

ghost commented 3 years ago

here is my code:

a, err := xormadapter.NewAdapter("mysql", "root:123456@tcp(172.17.0.2:3306)/casbin", true)
    if err != nil {
        log.Fatalf("error: adapter: %s", err)
    }

    m, err := model.NewModelFromString(`
    [request_definition]
    r = sub, obj, act

    [policy_definition]
    p = sub, obj, act

    [role_definition]
    g = _, _

    [policy_effect]
    e = some(where (p.eft == allow))

    [matchers]
    m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act 
`)
    if err != nil {
        log.Fatalf("error: model: %s", err)
    }

    e, err := casbin.NewEnforcer(m, a)
    if err != nil {
        log.Fatalf("error: enforcer: %s", err)
    }

    updated, err := e.UpdateGroupingPolicy([]string{"zpl", "admin"}, []string{"zpl", "person"})
    if err != nil {
        fmt.Println("update err is:", err)
        return
    }
    if updated {
        fmt.Println("updated")
    } else {
        fmt.Println("not updated")
    }
hsluoyz commented 3 years ago

@closetool @tangyang9464

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.4.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: