From c9e4fcba1792e912e4b1b1f2457dc1fe0f5d0c43 Mon Sep 17 00:00:00 2001 From: chen <23054115+cwkang1998@users.noreply.github.com> Date: Fri, 11 Mar 2022 04:41:22 +0800 Subject: [PATCH] feat(rbac): implement `getUsersForRoleInDomain` & `getRolesForUserInDomain` as alias to rbac Documentation indicates that there is existance of `getUsersForRoleInDomain` and `getRolesForUserInDomain` api, but it does not yet exists. This commit implements these functions, by aliasing them to existing rbac function that had already cater for domain apis, to increase and improve dev experience. re #304 --- src/enforcer.ts | 24 ++++++++++++++++++++++++ test/rbacwDomainAPI.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 test/rbacwDomainAPI.test.ts diff --git a/src/enforcer.ts b/src/enforcer.ts index 639e91b3..99307b08 100644 --- a/src/enforcer.ts +++ b/src/enforcer.ts @@ -371,6 +371,30 @@ export class Enforcer extends ManagementEnforcer { return Array.from(res); } + /** + * getRolesForUserInDomain gets the roles that a user has inside a domain + * An alias for getRolesForUser with the domain params. + * + * @param name the user. + * @param domain the domain. + * @return the roles that the user has. + */ + public async getRolesForUserInDomain(name: string, domain: string): Promise { + return this.getRolesForUser(name, domain); + } + + /** + * getUsersForRoleInFomain gets the users that has a role inside a domain + * An alias for getUsesForRole with the domain params. + * + * @param name the role. + * @param domain the domain. + * @return the users that has the role. + */ + public async getUsersForRoleInDomain(name: string, domain: string): Promise { + return this.getUsersForRole(name, domain); + } + /** * getImplicitUsersForPermission gets implicit users for a permission. * For example: diff --git a/test/rbacwDomainAPI.test.ts b/test/rbacwDomainAPI.test.ts new file mode 100644 index 00000000..c8b8855e --- /dev/null +++ b/test/rbacwDomainAPI.test.ts @@ -0,0 +1,31 @@ +// Copyright 2019 The Casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { newEnforcer } from '../src'; + +test('test getRolesForUserInDomain', async () => { + const e = await newEnforcer('examples/rbac_with_domains_model.conf', 'examples/rbac_with_domains_policy.csv'); + expect(await e.getRolesForUserInDomain('alice', 'domain1')).toEqual(['admin']); + expect(await e.getRolesForUserInDomain('alice', 'domain2')).toEqual([]); + expect(await e.getRolesForUserInDomain('bob', 'domain1')).toEqual([]); + expect(await e.getRolesForUserInDomain('bob', 'domain2')).toEqual(['admin']); +}); + +test('test getUsersForRoleInDomain', async () => { + const e = await newEnforcer('examples/rbac_with_domains_model.conf', 'examples/rbac_with_domains_policy.csv'); + expect(await e.getUsersForRoleInDomain('admin', 'domain1')).toEqual(['alice']); + expect(await e.getUsersForRoleInDomain('admin', 'domain2')).toEqual(['bob']); + expect(await e.getUsersForRoleInDomain('superadmin', 'domain1')).toEqual([]); + expect(await e.getUsersForRoleInDomain('superadmin', 'domain2')).toEqual([]); +});