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

fix: update casbinJsGetPermissionForUser for v0.1.0 Casbin.js #186

Merged
merged 1 commit into from
Aug 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { Enforcer } from './enforcer';

/**
* Experiment!
* getPermissionForCasbinJs returns a string describing the permission of a given user.
* You can pass the returned string to the frontend and manage your webpage widgets and APIs with Casbin.js.
* The returned permission depends on `getImplicitPermissionsForUser`.
Expand All @@ -23,16 +24,39 @@ import { Enforcer } from './enforcer';
* @param user the user
*/
export async function casbinJsGetPermissionForUser(e: Enforcer, user: string): Promise<string> {
const policies = await e.getImplicitPermissionsForUser(user);
const permission: { [key: string]: string[] } = {};
policies.forEach(policy => {
if (!(policy[2] in permission)) {
permission[policy[2]] = [];
}
if (permission[policy[2]].indexOf(policy[1]) == -1) {
permission[policy[2]].push(policy[1]);
}
});
const permString = JSON.stringify(permission);
return permString;
const obj: any = {};

const m = e.getModel().model;
let s = '';
s += '[request_definition]\n';
s += `r = ${m
.get('r')
?.get('r')
?.value.replace(/_/g, '.')}\n`;
s += '[policy_definition]\n';
s += `p = ${m
.get('p')
?.get('p')
?.value.replace(/_/g, '.')}\n`;
if (m.get('g')?.get('g') !== undefined) {
s += '[role_definition]\n';
s += `g = ${m.get('g')?.get('g')?.value}\n`;
}
s += '[policy_effect]\n';
s += `e = ${m
.get('e')
?.get('e')
?.value.replace(/_/g, '.')}\n`;
s += '[matchers]\n';
s += `m = ${m
.get('m')
?.get('m')
?.value.replace(/_/g, '.')}`;
obj['m'] = s;
obj['p'] = await e.getPolicy();
for (const arr of obj['p']) {
arr.splice(0, 0, 'p');
}

return JSON.stringify(obj);
}
26 changes: 13 additions & 13 deletions test/frontend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { readFileSync } from 'fs';
import { newEnforcer } from '../src/index';
import { casbinJsGetPermissionForUser } from '../src/frontend';

test('TestCasbinJsGetPermissionForUser', async () => {
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_with_hierarchy_policy.csv');
let permStr = await casbinJsGetPermissionForUser(e, 'alice');
let perm = JSON.parse(permStr);
expect(perm['read']).toContain('data1');
expect(perm['write']).toContain('data1');
expect(perm['read']).toContain('data2');
expect(perm['write']).toContain('data2');

permStr = await casbinJsGetPermissionForUser(e, 'bob');
perm = JSON.parse(permStr);
expect(perm['write']).toContain('data2');
expect(perm['write']).not.toContain('data1');
expect(perm['read']).not.toBeNull;
expect(perm['rm_rf']).toBeNull;
const received = JSON.parse(await casbinJsGetPermissionForUser(e, 'alice'));
const expectedModelStr = readFileSync('examples/rbac_model.conf').toString();
expect(received['m']).toBe(expectedModelStr.replace(/\n\n/g, '\n'));
const expectedPoliciesStr = readFileSync('examples/rbac_with_hierarchy_policy.csv').toString();
const expectedPolicyItem = expectedPoliciesStr.split(RegExp(',|\n'));
let i = 0;
for (const sArr of received['p']) {
for (const s of sArr) {
expect(s.trim()).toEqual(expectedPolicyItem[i].trim());
i = i + 1;
}
}
});