Skip to content

Commit 65d3a26

Browse files
committed
feat: add named addMatchingFunc
Signed-off-by: Zxilly <zhouxinyu1001@gmail.com>
1 parent 5953411 commit 65d3a26

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

.prettierrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"semi": true,
33
"singleQuote": true,
4-
"printWidth": 140
4+
"printWidth": 140,
5+
"endOfLine": "auto"
56
}

src/coreEnforcer.ts

+27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Adapter, FilteredAdapter, Watcher, BatchAdapter, UpdatableAdapter } fro
2020
import { DefaultRoleManager, RoleManager } from './rbac';
2121
import { escapeAssertion, generateGFunction, getEvalValue, hasEval, replaceEval, generatorRunSync, generatorRunAsync } from './util';
2222
import { getLogger, logPrint } from './log';
23+
import { MatchingFunc } from './rbac';
2324

2425
type Matcher = ((context: any) => Promise<any>) | ((context: any) => any);
2526

@@ -262,6 +263,32 @@ export class CoreEnforcer {
262263
this.autoBuildRoleLinks = autoBuildRoleLinks;
263264
}
264265

266+
/**
267+
* add matching function to RoleManager by ptype
268+
* @param ptype g
269+
* @param fn the function will be added
270+
*/
271+
public async addNamedMatchingFunc(ptype: string, fn: MatchingFunc): Promise<void> {
272+
const rm = this.rmMap.get(ptype);
273+
if (rm) {
274+
return await (<DefaultRoleManager>rm).addMatchingFunc(fn);
275+
}
276+
277+
throw Error('Target ptype not found.');
278+
}
279+
280+
/**
281+
* add domain matching function to RoleManager by ptype
282+
* @param ptype g
283+
* @param fn the function will be added
284+
*/
285+
public async addNamedDomainMatchingFunc(ptype: string, fn: MatchingFunc): Promise<void> {
286+
const rm = this.rmMap.get(ptype);
287+
if (rm) {
288+
return await (<DefaultRoleManager>rm).addDomainMatchingFunc(fn);
289+
}
290+
}
291+
265292
/**
266293
* buildRoleLinks manually rebuild the role inheritance relations.
267294
*/

src/rbac/defaultRoleManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import { RoleManager } from './roleManager';
1616
import { getLogger, logPrint } from '../log';
1717

18-
type MatchingFunc = (arg1: string, arg2: string) => boolean;
18+
export type MatchingFunc = (arg1: string, arg2: string) => boolean;
1919

2020
// DEFAULT_DOMAIN defines the default domain space.
2121
const DEFAULT_DOMAIN = 'casbin::default';

0 commit comments

Comments
 (0)