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

chore: sync with main branch #360

Merged
merged 4 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/basic_keyget2_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj

[policy_definition]
p = sub, obj

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

[matchers]
m = keyGet2(r.obj, p.obj, 'id')
1 change: 1 addition & 0 deletions examples/basic_keyget2_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p, alice, /data/:id
9 changes: 8 additions & 1 deletion src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,15 @@ export class CoreEnforcer {
eftRes = result;
}
break;
case 'string':
if (result === '') {
eftRes = Effect.Indeterminate;
} else {
eftRes = Effect.Allow;
}
break;
default:
throw new Error('matcher result should be boolean or number');
throw new Error('matcher result should only be of type boolean, number, or string');
}

const eft = parameters['p_eft'];
Expand Down
24 changes: 24 additions & 0 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,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<string[]> {
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<string[]> {
return this.getUsersForRole(name, domain);
}

/**
* getImplicitUsersForPermission gets implicit users for a permission.
* For example:
Expand Down
1 change: 1 addition & 0 deletions src/model/functionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class FunctionMap {
fm.addFunction('keyGet2', util.keyGet2Func);
fm.addFunction('keyMatch3', util.keyMatch3Func);
fm.addFunction('keyMatch4', util.keyMatch4Func);
fm.addFunction('keyMatch5', util.keyMatch5Func);
fm.addFunction('regexMatch', util.regexMatchFunc);
fm.addFunction('ipMatch', util.ipMatchFunc);
fm.addFunction('globMatch', util.globMatch);
Expand Down
21 changes: 21 additions & 0 deletions src/util/builtinOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ function keyMatch4Func(...args: any[]): boolean {
return keyMatch4(name1, name2);
}

// KeyMatch determines whether key1 matches the pattern of key2 and ignores the parameters in key2.
// For example, "/foo/bar?status=1&type=2" matches "/foo/bar"
function KeyMatch5(key1: string, key2: string): boolean {
const i: number = key1.indexOf('?');
if (i === -1) {
return key1 === key2;
}

return key1.slice(0, i) === key2;
}

// keyMatch5Func is the wrapper for KeyMatch5.
function keyMatch5Func(...args: any[]): boolean {
const [arg0, arg1] = args;
const name1: string = (arg0 || '').toString();
const name2: string = (arg1 || '').toString();

return KeyMatch5(name1, name2);
}

// regexMatchFunc is the wrapper for regexMatch.
function regexMatchFunc(...args: any[]): boolean {
const [arg0, arg1] = args;
Expand Down Expand Up @@ -332,5 +352,6 @@ export {
ipMatchFunc,
generateGFunction,
keyMatch4Func,
keyMatch5Func,
globMatch,
};
32 changes: 32 additions & 0 deletions test/rbacwDomainAPI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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';
import { getEnforcerWithPath } from './utils';

test('test getRolesForUserInDomain', async () => {
const e = await getEnforcerWithPath('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 getEnforcerWithPath('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([]);
});
9 changes: 9 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ test('test keyMatch4Func', () => {
expect(util.keyMatch4Func('/parent/123/child/456', '/parent/{id}/child/{id}/book/{id}')).toEqual(false);
});

test('test keyMatch5Func', () => {
expect(util.keyMatch5Func('/parent/child?status=1&type=2', '/parent/child')).toEqual(true);
expect(util.keyMatch5Func('/parent?status=1&type=2', '/parent/child')).toEqual(false);

expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child/')).toEqual(true);
expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child')).toEqual(false);
expect(util.keyMatch5Func('/parent/child?status=1&type=2', '/parent/child/')).toEqual(false);
});

test('test ipMatchFunc', () => {
expect(util.ipMatchFunc('::1', '::0:1')).toEqual(true);
expect(util.ipMatchFunc('192.168.1.1', '192.168.1.1')).toEqual(true);
Expand Down