Skip to content

Commit

Permalink
Merge pull request #7 from cobuildlab/hotfix/main/createRules-error
Browse files Browse the repository at this point in the history
fix assign diferents permissions to same role
  • Loading branch information
jesusrodrz authored Jun 3, 2021
2 parents 577b14e + eab0f12 commit fd0078e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cobuildlab/rbac",
"version": "0.2.0",
"version": "0.2.1",
"description": "",
"main": "lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test('Test static rules', () => {
}
enum Permissions {
DASHBOARD = 'DASHBOARD',
SETTINGS = 'SETTINGS',
}

const staticRules = new RBAC<Roles, Permissions>(Roles.ADMIN);
Expand All @@ -18,6 +19,12 @@ test('Test static rules', () => {
true,
'Access granted',
);
staticRules.createRule(
Roles.ADMIN,
Permissions.SETTINGS,
true,
'Access granted',
);
staticRules.createRule(
Roles.MANAGER,
Permissions.DASHBOARD,
Expand All @@ -28,6 +35,10 @@ test('Test static rules', () => {
true,
'Access granted',
]);
expect(staticRules.check(Roles.ADMIN, Permissions.SETTINGS)).toStrictEqual([
true,
'Access granted',
]);
expect(
staticRules.check(Roles.MANAGER, Permissions.DASHBOARD),
).toStrictEqual([false, 'Access denied']);
Expand Down
18 changes: 14 additions & 4 deletions src/rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ export class RBAC<
test: boolean | ValidatorFunctionType<D[P]>,
message?: string,
): void {
Object.assign(this.rules, {
[roleName]: {
if (this.rules[roleName]) {
Object.assign(this.rules[roleName], {
[permissionName]: {
can: typeof test === 'boolean' ? test : undefined,
dynamic: typeof test === 'function' ? test : undefined,
message,
},
},
});
});
} else {
Object.assign(this.rules, {
[roleName]: {
[permissionName]: {
can: typeof test === 'boolean' ? test : undefined,
dynamic: typeof test === 'function' ? test : undefined,
message,
},
},
});
}
}

/**
Expand Down

0 comments on commit fd0078e

Please sign in to comment.