Closed antipopp closed 3 years ago
@closetool @tangyang9464
@antipopp Can you give me the complete code? I'm a little confused whether your code can run?
such as _, err := enforcer.Enforcer.UpdatePolicy(oldPolicy, newPolicy)
, Isn't it like this _, err := enforcer.UpdatePolicy(oldPolicy, newPolicy)
Yes, the UpdatePolicy bit is in a GraphQL endpoint generated with gqlgen, the enforcer object is in it's own package.
The full generated endpoint is something like this:
import (
"backend/enforcer"
"backend/graph/auth"
"backend/graph/model"
"context"
"log"
"strings"
)
func (r *mutationResolver) UpdatePolicy(ctx context.Context, subject string, object string, action string) (*model.Policy, error) {
user := auth.ForContext(ctx)
if err := CheckPerms(ctx, "policy", "allow"); err != nil {
return nil, err
}
oldAction := "allow"
if action == "allow" {
oldAction = "deny"
}
domain := user.Company
oldPolicy := []string{strings.ToLower(subject), strings.ToLower(domain), object, oldAction}
newPolicy := []string{strings.ToLower(subject), strings.ToLower(domain), object, action}
_, err := enforcer.Enforcer.UpdatePolicy(oldPolicy, newPolicy)
if err != nil {
log.Println(err)
return nil, err
}
return &model.Policy{}, nil
}
So the first enforcer
of enforcer.Enforcer
is the package, the capitalized one is the real object (declared in the configuration block of the first post). Sorry for the confusion.
@tangyang9464 I think we need to implement new function UpdateFilteredPolicies()
in this adapter code to satisfy the UpdatableAdapter
interface. Plz make a PR.
@tangyang9464 I think we need to implement new function
UpdateFilteredPolicies()
in this adapter code to satisfy theUpdatableAdapter
interface. Plz make a PR.
@hsluoyz But the function has been implemented.
@antipopp What version are you using? UpdateFilteredPolicies
only be supported after v1.0.0
and my test code is as follows, there will be no error
a, _ := pgadapter.NewAdapter("postgresql://postgres:root@localhost:5432/postgres?sslmode=disable")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
e.LoadPolicy()
_, err := e.UpdatePolicy([]string{"alice","data1","read"},[]string{"alice","data1","write"})
Maybe you can check your code again or use other code to test.
It doesn't work in my setup.
This is how I create the initial policies (this function is called right after my Init() function in the original post):
func AddDefaultPoliciesAndGroups() error {
// p = sub, dom, obj, act
policies := [][]string{
{"admin", "domain1", "users", "allow"},
}
_, err := Enforcer.AddPolicies(policies)
if err != nil {
return err
}
groups := [][]string{
{"admin@domain1.com", "admin", "domain1"},
}
_, err = Enforcer.AddNamedGroupingPolicies("g", groups)
if err != nil {
return err
}
// I added this to test, and it still throws the same error
_, err = Enforcer.UpdatePolicy([]string{"admin", "domain1", "users", "allow"}, []string{"admin", "domain1", "users", "deny"})
if err != nil {
return err
}
return nil
}
Maybe is it because I'm using domains?
This is the full panic log:
panic: interface conversion: *pgadapter.Adapter is not persist.UpdatableAdapter: missing method UpdateFilteredPolicies
goroutine 1 [running]:
github.com/casbin/casbin/v2.(*Enforcer).updatePolicy(0xc0000c6800, 0x1088809, 0x1, 0x1088809, 0x1, 0xc00015d600, 0x4, 0x4, 0xc00015d640, 0x4, ...)
/home/dearr/go/pkg/mod/github.com/casbin/casbin/v2@v2.30.0/internal_api.go:154 +0x459
github.com/casbin/casbin/v2.(*Enforcer).UpdateNamedPolicy(...)
/home/dearr/go/pkg/mod/github.com/casbin/casbin/v2@v2.30.0/management_api.go:166
github.com/casbin/casbin/v2.(*Enforcer).UpdatePolicy(0xc0000c6800, 0xc00015d600, 0x4, 0x4, 0xc00015d640, 0x4, 0x4, 0x1, 0xc000511c00, 0x0)
/home/dearr/go/pkg/mod/github.com/casbin/casbin/v2@v2.30.0/management_api.go:162 +0xa5
backend/enforcer.AddDefaultPoliciesAndGroups(0xf253a0, 0xc0000a4600)
/home/dearr/Work/whistle/backend/enforcer/enforcer.go:97 +0xcf1
backend/enforcer.Init(0x0, 0x0)
/home/dearr/Work/whistle/backend/enforcer/enforcer.go:55 +0x225
main.init.0()
/home/dearr/Work/whistle/backend/main.go:43 +0xb8
exit status 2
my go.mod has this versions:
github.com/casbin/casbin-pg-adapter v1.0.2
github.com/casbin/casbin/v2 v2.30.0
@antipopp This adapter implementation is outdated, I will update it
:tada: This issue has been resolved in version 1.0.3 :tada:
The release is available on GitHub release
Your semantic-release bot :package::rocket:
Hello, when running
UpdatePolicy
this error comes up.my configuration:
the code that triggers the error: