Skip to content

Commit 1b1f1bb

Browse files
committed
feat: Support multiple policy definitions (p, p2) (#518)
1 parent 7801732 commit 1b1f1bb

7 files changed

+96
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
r2 = sub, obj, act
4+
5+
[policy_definition]
6+
p = sub, obj, act
7+
p2= sub_rule, obj, act, eft
8+
9+
[role_definition]
10+
g = _, _
11+
12+
[policy_effect]
13+
e = some(where (p.eft == allow))
14+
15+
[matchers]
16+
#RABC
17+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
18+
#ABAC
19+
m2 = eval(p2.sub_rule) && r2.obj == p2.obj && r2.act == p2.act
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
p, data2_admin, data2, read
2+
p2, r2.sub.Age > 18 && r2.sub.Age < 60, /data1, read, allow
3+
p2, r2.sub.Age > 60 && r2.sub.Age < 100, /data1, read, deny
4+
5+
g, alice, data2_admin
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[request_definition]
2+
r = user, thing, action
3+
4+
[policy_definition]
5+
p = role, thing, action
6+
p2 = role, action
7+
8+
[policy_effect]
9+
e = some(where (p.eft == allow))
10+
11+
[matchers]
12+
m = g(r.user, p.role) && r.thing == p.thing && r.action == p.action
13+
m2 = g(r.user, p2.role) && r.action == p.action
14+
15+
[role_definition]
16+
g = _,_
17+
g2 = _,_
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
p, user, /data, GET
2+
p, admin, /data, POST
3+
4+
p2, user, view
5+
p2, admin, create
6+
7+
g, admin, user
8+
g, alice, admin
9+
g2, alice, user

src/coreEnforcer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ export class CoreEnforcer {
479479

480480
const effectStream = this.eft.newStream(effectExpr);
481481

482-
if (policyLen && policyLen !== 0) {
482+
if (policyLen && policyLen !== 0 && expString.includes(`${enforceContext.pType}_`)) {
483483
for (let i = 0; i < policyLen; i++) {
484484
const parameters: { [key: string]: any } = {};
485485

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2018 The Casbin Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { newEnforcer, newEnforceContext } from '../src';
16+
17+
test('TestMultiplePolicyDefinitions', async () => {
18+
const e = await newEnforcer('examples/multiple_policy_definitions_model.conf', 'examples/multiple_policy_definitions_policy.csv');
19+
const enforceContext = newEnforceContext('2');
20+
enforceContext.eType = 'e';
21+
22+
// Test with default context (r, p, e, m)
23+
await expect(e.enforce('alice', 'data2', 'read')).resolves.toBe(true);
24+
25+
// Test with EnforceContext for r2, p2, e, m2
26+
await expect(e.enforce(enforceContext, { Age: 70 }, '/data1', 'read')).resolves.toBe(false);
27+
await expect(e.enforce(enforceContext, { Age: 30 }, '/data1', 'read')).resolves.toBe(true);
28+
});

test/rbacAPI.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,20 @@ test('test getImplicitUsersForRole', async () => {
202202
expect(await e.getImplicitUsersForRole('admin')).toEqual(['alice']);
203203
expect(await e.getImplicitUsersForRole('data1_admin')).toEqual(['admin', 'alice']);
204204
});
205+
206+
test('test rbac with multiple policy definitions', async () => {
207+
const e = await newEnforcer('examples/rbac_with_multiple_policy_model.conf', 'examples/rbac_with_multiple_policy_policy.csv');
208+
209+
// Test getting named policies for different policy types
210+
const pPolicies = await e.getNamedPolicy('p');
211+
expect(pPolicies).toEqual([
212+
['user', '/data', 'GET'],
213+
['admin', '/data', 'POST'],
214+
]);
215+
216+
const p2Policies = await e.getNamedPolicy('p2');
217+
expect(p2Policies).toEqual([
218+
['user', 'view'],
219+
['admin', 'create'],
220+
]);
221+
});

0 commit comments

Comments
 (0)