Skip to content

Commit 30ae126

Browse files
committed
feat: add casbinJsGetPermissionForUser
Signed-off-by: kingiw <kingiw@hotmail.com>
1 parent aa7271d commit 30ae126

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/frontend.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 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 { Enforcer } from './enforcer';
16+
17+
/**
18+
* getPermissionForCasbinJs returns a string describing the permission of a given user.
19+
* You can pass the returned string to the frontend and manage your webpage widgets and APIs with Casbin.js.
20+
* The returned permission depends on `getImplicitPermissionsForUser`.
21+
* In other words, getPermissionForCasbinJs will load all of the explicit and implicit permission (role's permission).
22+
* @param e the initialized enforcer
23+
* @param user the user
24+
*/
25+
export async function casbinJsGetPermissionForUser(e: Enforcer, user: string): Promise<string> {
26+
const policies = await e.getImplicitPermissionsForUser(user);
27+
const permission: { [key: string]: string[] } = {};
28+
policies.forEach(policy => {
29+
if (!(policy[2] in permission)) {
30+
permission[policy[2]] = [];
31+
}
32+
if (permission[policy[2]].indexOf(policy[1]) == -1) {
33+
permission[policy[2]].push(policy[1]);
34+
}
35+
});
36+
const permString = JSON.stringify(permission);
37+
return permString;
38+
}

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export * from './model';
2323
export * from './persist';
2424
export * from './rbac';
2525
export * from './log';
26+
export * from './frontend';
2627
export { Util };

test/frontend.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020 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 } from '../src/index';
16+
import { casbinJsGetPermissionForUser } from '../src/frontend';
17+
18+
test('TestCasbinJsGetPermissionForUser', async () => {
19+
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_with_hierarchy_policy.csv');
20+
let permStr = await casbinJsGetPermissionForUser(e, 'alice');
21+
let perm = JSON.parse(permStr);
22+
expect(perm['read']).toContain('data1');
23+
expect(perm['write']).toContain('data1');
24+
expect(perm['read']).toContain('data2');
25+
expect(perm['write']).toContain('data2');
26+
27+
permStr = await casbinJsGetPermissionForUser(e, 'bob');
28+
perm = JSON.parse(permStr);
29+
expect(perm['write']).toContain('data2');
30+
expect(perm['write']).not.toContain('data1');
31+
expect(perm['read']).not.toBeNull;
32+
expect(perm['rm_rf']).toBeNull;
33+
});

0 commit comments

Comments
 (0)