Skip to content

Commit a3ab9d3

Browse files
authored
feat: Update to latest node-casbin version and improve API support (#169)
* Updates to latest node-casbin version Improves Casbin API support * fix typos in JSDoc comments
1 parent 44f9626 commit a3ab9d3

File tree

4 files changed

+1857
-1390
lines changed

4 files changed

+1857
-1390
lines changed

Diff for: jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ module.exports = {
6868
moduleFileExtensions: ['ts', 'tsx', 'js'],
6969

7070
// A map from regular expressions to module names that allow to stub out resources with a single module
71-
// moduleNameMapper: {},
71+
moduleNameMapper: {
72+
'csv-parse': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs',
73+
},
7274

7375
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
7476
// modulePathIgnorePatterns: [],

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"author": "dreamdevil00",
2828
"license": "MIT",
2929
"dependencies": {
30-
"casbin": "^5.11.1"
30+
"casbin": "^5.30.0"
3131
},
3232
"devDependencies": {
3333
"@nestjs/common": "^9.0.3",

Diff for: src/services/authz-management.service.ts

+91
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,66 @@ export class AuthZManagementService {
1212
public readonly enforcer: casbin.Enforcer
1313
) {}
1414

15+
/**
16+
* enforce decides whether a "subject" can access a "object" with the operation "action"
17+
*
18+
* @param params the request parameters, usually (sub, obj, act)
19+
*
20+
* @return whether or not the request is allowed
21+
*/
22+
enforce(...params: string[]): Promise<boolean> {
23+
return this.enforcer.enforce(params);
24+
}
25+
26+
/**
27+
* enforceWithMatcher uses a custom matcher to decides whether a "subject" can access a "object" with the operation "action"
28+
*
29+
* @param matcher the matcher statement to use
30+
* @param params the request parameters, usually (sub, obj, act)
31+
*
32+
* @return whether or not the request is allowed
33+
*/
34+
enforceWithMatcher(matcher: string, ...params: string[]): Promise<boolean> {
35+
return this.enforcer.enforceWithMatcher(matcher, params);
36+
}
37+
38+
/**
39+
* enforceEx explains enforcement by returning matched rules.
40+
*
41+
* @param params the request parameters, usually (sub, obj, act)
42+
*
43+
* @return whether or not the request is allowed, and what policy caused that decision
44+
*/
45+
enforceEx(...params: string[]): Promise<[boolean, string[]]> {
46+
return this.enforcer.enforceEx(params);
47+
}
48+
49+
/**
50+
* enforceExWithMatcher uses a custom matcher and explains enforcement by returning matched rules.
51+
*
52+
* @param matcher the matcher statement to use
53+
* @param params the request parameters, usually (sub, obj, act)
54+
*
55+
* @return whether or not the request is allowed, and what policy caused that decision
56+
*/
57+
enforceExWithMatcher(
58+
matcher: string,
59+
...params: string[]
60+
): Promise<[boolean, string[]]> {
61+
return this.enforcer.enforceExWithMatcher(matcher, params);
62+
}
63+
64+
/**
65+
* batchEnforce enforces each request and returns result in a bool array
66+
*
67+
* @param params the request parameters, usually (sub, obj, act)
68+
*
69+
* @return an array with the enforcement results for each given request
70+
*/
71+
batchEnforce(params: string[][]): Promise<boolean[]> {
72+
return this.enforcer.batchEnforce(params);
73+
}
74+
1575
/**
1676
* getAllSubjects gets the list of subjects that show up in the current policy.
1777
*
@@ -537,4 +597,35 @@ export class AuthZManagementService {
537597
loadPolicy(): Promise<void> {
538598
return this.enforcer.loadPolicy();
539599
}
600+
601+
/**
602+
* updateGroupingPolicy updates a role inheritance rule from the current policy.
603+
* If the rule not exists, the function returns false.
604+
* Otherwise the function returns true by changing it to the new rule.
605+
*
606+
* @param oldRule the role inheritance rule will be remove
607+
* @param newRule the role inheritance rule will be added
608+
* @return succeeds or not.
609+
*/
610+
updateGroupingPolicy(oldRule: string[], newRule: string[]): Promise<boolean> {
611+
return this.enforcer.updateGroupingPolicy(oldRule, newRule);
612+
}
613+
614+
/**
615+
* updateNamedGroupingPolicy updates a named role inheritance rule from the current policy.
616+
* If the rule not exists, the function returns false.
617+
* Otherwise the function returns true by changing it to the new rule.
618+
*
619+
* @param ptype the policy type, can be "g", "g2", "g3", ..
620+
* @param oldRule the role inheritance rule will be remove
621+
* @param newRule the role inheritance rule will be added
622+
* @return succeeds or not.
623+
*/
624+
updateNamedGroupingPolicy(
625+
ptype: string,
626+
oldRule: string[],
627+
newRule: string[]
628+
): Promise<boolean> {
629+
return this.enforcer.updateNamedGroupingPolicy(ptype, oldRule, newRule);
630+
}
540631
}

0 commit comments

Comments
 (0)