Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] GetRolesForUserInDomain can not obtain data after using AddNamedDomainLinkConditionFunc #1336

Open
301lj opened this issue Oct 25, 2023 · 2 comments

Comments

@301lj
Copy link

301lj commented Oct 25, 2023

What's your scenario? What do you want to achieve?
I want to use GetRolesForUserInDomain to obtain roles that the user have

Your model:

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _, (_, _)

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

[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && \
(keyMatch(r.act, p.act) || keyMatch2(r.act, p.act) || keyMatch3(r.act, p.act) || keyMatch4(r.act, p.act) || keyMatch5(r.act, p.act) || globMatch(r.act, p.act))

Your policy:

p, test1, domain1, service1, /list
p, test1, domain1, service1, /get/:id/*
p, test1, domain1, service1, /add
p, test1, domain1, service1, /user/*
p, admin, domain1, service1, /*
p, qa1, domain2, service2, /broadcast
p, qa1, domain2, service2, /trip
p, qa1, domain2, service2, /notify
p, qa1, domain2, service2, /dynamic-sql

g, alice, test1, domain1, _, 2023-12-30 00:00:00
g, bob, qa1, domain2, _, 2023-12-30 00:00:00

codes:

package main

import (
	"github.com/casbin/casbin/v2"
	"github.com/casbin/casbin/v2/model"
	fileAdapter "github.com/casbin/casbin/v2/persist/file-adapter"
	"github.com/casbin/casbin/v2/util"
	"log"
)

func main() {

	m, _ := model.NewModelFromFile("model5.conf")
	a := fileAdapter.NewAdapter("policy5.csv")
	e, _ := casbin.NewEnforcer(m, a)

	g := e.GetNamedGroupingPolicy("g")
	for _, gp := range g {
		e.AddNamedDomainLinkConditionFunc("g", gp[0], gp[1], gp[2], util.TimeMatchFunc)
	}
	check5(e, []interface{}{"alice", "domain1", "service1", "/add"})
	check5(e, []interface{}{"alice", "domain1", "service1", "/list"})
	check5(e, []interface{}{"alice", "domain1", "service1", "/userEdit"})
	check5(e, []interface{}{"alice", "domain1", "service1", "/get/9527/info"})

	log.Println("-------")

	/**
	here, the error will occer
	*/
	rolesForUserInDomain := e.GetRolesForUserInDomain("alice", "domain1")
	for i := range rolesForUserInDomain {
		role := rolesForUserInDomain[i]
		permissionsForUser := e.GetPermissionsForUser(role)
		for j := range permissionsForUser {
			log.Println(permissionsForUser[j])
		}
	}
	log.Println("--------------")
}

func check5(e *casbin.Enforcer, slice1 []interface{}) {
	result1, error := e.Enforce(slice1...)
	if error != nil {
		log.Println(error)
	}
	log.Println(result1)
}

Your request(s):

I want to use GetRolesForUserInDomain to obtain roles that the user have, but a error occurred.
I found that if I not use this method AddNamedDomainLinkConditionFunc, there is no problem.
I want to know how to solve this? think you!
@casbin-bot
Copy link
Member

@tangyang9464 @JalinWang

@301lj
Copy link
Author

301lj commented Oct 25, 2023

image
Almost all the methods can not be used, except GetAllNamedRoles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants