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] how to getPermissionByRoleInDomain and getAllRolesInDomain #1036

Closed
panapol-p opened this issue Jun 23, 2022 · 6 comments
Closed

Comments

@panapol-p
Copy link

Hi, guys
I read document and didn't see getAllPermissionsByRoleInDomain and getAllRolesInDomain
Do you have way to get this one?

@casbin-bot
Copy link
Member

@tangyang9464 @closetool @sagilio

@panapol-p
Copy link
Author

after I try to get both with policy per below

p, admin, domain1, data1
p, admin, domain1, data1-1
p, admin2, domain1, data2
p, admin2, domain1, data2-1
p, admin2, domain1, data2-2
p, admin, domain2, data3
p, admin, domain2, data3-2
p, admin, domain2, data3-3
g, alice, admin, domain1
g, wanda, admin2, domain1
g, bob, admin, domain2

I can getPermissionsForRoleInDomain by this code

func getPermissionsForRoleInDomain(e *casbin.Enforcer, role string, domain string) []string {
	ps := e.GetPermissionsForUserInDomain(role, domain)
	var p []string
	for i := range ps {
		p = append(p, ps[i][2])
	}
	return p
}

permissions := getPermissionsForRoleInDomain(e, "admin", "domain1")  // output :  [data1 data1-1]
permissions = getPermissionsForRoleInDomain(e, "admin2", "domain1")  // output :  [data2 data2-1 data2-2]
permissions = getPermissionsForRoleInDomain(e, "admin", "domain2")  // output :  [data3 data3-1 data3-2 data3-3]

and getAllRolesInDomain by this code

func getRolesInDomain(e *casbin.Enforcer, domain string) []string {
	rs := e.GetFilteredPolicy(0, "", domain, "")
	var r []string
	mapRoles := map[string]struct{}{}
	for i := range rs {
		roleNames := rs[i][0]
		if _, ok := mapRoles[roleNames]; !ok {
			mapRoles[roleNames] = struct{}{}
			r = append(r, roleNames)
		}
	}
	return r
}

roles := getRolesInDomain(e, "domain1")  // output :  [admin admin2]
roles = getRolesInDomain(e, "domain2")  // output :  [admin]

@hsluoyz
Copy link
Member

hsluoyz commented Jun 23, 2022

@JalinWang @imp2002

/cc @tangyang9464

@JalinWang
Copy link
Member

JalinWang commented Jun 24, 2022

I think it is reasonable to add the getAllRolesInDomain api. There is GetAllRoles() already for models except RBAC with domain.

However, for getAllPermissionsByRoleInDomain , I didn't get the point. What does it use for?
BTW, your code for getRolesInDomain() is a limitated implementation, i.e., when the policy_definition goes as

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

, the permissions should be be like [ [data1, read], [data1-1, read]].

@hsluoyz
Copy link
Member

hsluoyz commented Jun 24, 2022

@panapol-p

@panapol-p
Copy link
Author

panapol-p commented Jun 25, 2022

thanks, @JalinWang, and @hsluoyz
I designed my application to support multi-tenant/domain
when we go to the user management module, we need to have a list of roles in this domain that use to assign new use or replace new role for an old user (my user management module depend on casbin policy only)

do you think this is good or not? thanks for your suggestion again :)

Edited
I forgot to explain about getAllPermissionByRoleInDomain,for getAllPermissionByRoleInDomain, when admin pick some role for change permission, need to show a list of permission assigned in this role (in this domain)

@panapol-p panapol-p closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants