Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 157f0b5

Browse files
authored
Merge pull request apache#65 from casbin/casbin-next
Make all function to async function in rolemanager interface
2 parents 70d4c57 + 08538f4 commit 157f0b5

8 files changed

Lines changed: 86 additions & 112 deletions

File tree

src/casbin.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/coreEnforcer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ import { compileAsync } from 'expression-eval';
1616
import * as _ from 'lodash';
1717

1818
import { DefaultEffector, Effect, Effector } from './effect';
19-
import { FunctionMap, Model } from './model';
19+
import { FunctionMap, Model, newModel } from './model';
2020
import { Adapter, Filter, FilteredAdapter, Watcher } from './persist';
2121
import { DefaultRoleManager, RoleManager } from './rbac';
2222
import { generateGFunction } from './util';
23-
import { newModel } from './casbin';
2423
import { getLogger, logPrint } from './log';
2524

2625
/**
@@ -242,8 +241,7 @@ export class CoreEnforcer {
242241
* role inheritance relations.
243242
*/
244243
public async buildRoleLinks(): Promise<void> {
245-
// error intentionally ignored
246-
this.rm.clear();
244+
await this.rm.clear();
247245
await this.model.buildRoleLinks(this.rm);
248246
}
249247

src/enforcer.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// limitations under the License.
1414

1515
import { ManagementEnforcer } from './managementEnforcer';
16-
import { FunctionMap, Model } from './model';
16+
import { FunctionMap, Model, newModel } from './model';
1717
import { Adapter, FileAdapter } from './persist';
18-
import { newModel } from './casbin';
18+
import { getLogger } from './log';
1919

2020
/**
2121
* Enforcer = ManagementEnforcer + RBAC API.
@@ -303,3 +303,61 @@ export class Enforcer extends ManagementEnforcer {
303303
return res;
304304
}
305305
}
306+
307+
/**
308+
* newEnforcer creates an enforcer via file or DB.
309+
*
310+
* File:
311+
* ```js
312+
* const e = new Enforcer('path/to/basic_model.conf', 'path/to/basic_policy.csv');
313+
* ```
314+
*
315+
* MySQL DB:
316+
* ```js
317+
* const a = new MySQLAdapter('mysql', 'mysql_username:mysql_password@tcp(127.0.0.1:3306)/');
318+
* const e = new Enforcer('path/to/basic_model.conf', a);
319+
* ```
320+
*
321+
* @param params
322+
*/
323+
export async function newEnforcer(...params: any[]): Promise<Enforcer> {
324+
const e = new Enforcer();
325+
326+
let parsedParamLen = 0;
327+
if (params.length >= 1) {
328+
const enableLog = params[params.length - 1];
329+
if (typeof enableLog === 'boolean') {
330+
getLogger().enableLog(enableLog);
331+
parsedParamLen++;
332+
}
333+
}
334+
335+
if (params.length - parsedParamLen === 2) {
336+
if (typeof params[0] === 'string') {
337+
if (typeof params[1] === 'string') {
338+
await e.initWithFile(params[0].toString(), params[1].toString());
339+
} else {
340+
await e.initWithAdapter(params[0].toString(), params[1]);
341+
}
342+
} else {
343+
if (typeof params[1] === 'string') {
344+
throw new Error('Invalid parameters for enforcer.');
345+
} else {
346+
await e.initWithModelAndAdapter(params[0], params[1]);
347+
}
348+
}
349+
} else if (params.length - parsedParamLen === 1) {
350+
if (typeof params[0] === 'string') {
351+
await e.initWithFile(params[0], '');
352+
} else {
353+
// @ts-ignore
354+
await e.initWithModelAndAdapter(params[0], null);
355+
}
356+
} else if (params.length === parsedParamLen) {
357+
await e.initWithFile('', '');
358+
} else {
359+
throw new Error('Invalid parameters for enforcer.');
360+
}
361+
362+
return e;
363+
}

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ export * from './effect';
2020
export * from './model';
2121
export * from './persist';
2222
export * from './rbac';
23-
export * from './casbin';
2423
export * from './log';
2524
export { Util };

src/model/assertion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ export class Assertion {
6363
}
6464

6565
logPrint(`Role links for: ${this.key}`);
66-
this.rm.printRoles();
66+
await this.rm.printRoles();
6767
}
6868
}

src/model/model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,22 @@ export class Model {
278278
});
279279
}
280280
}
281+
282+
/**
283+
* newModel creates a model.
284+
*/
285+
export function newModel(...text: string[]): Model {
286+
const m = new Model();
287+
288+
if (text.length === 2) {
289+
if (text[0] !== '') {
290+
m.loadModel(text[0]);
291+
}
292+
} else if (text.length === 1) {
293+
m.loadModelFromText(text[0]);
294+
} else if (text.length !== 0) {
295+
throw new Error('Invalid parameters for model.');
296+
}
297+
298+
return m;
299+
}

src/rbac/defaultRoleManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class DefaultRoleManager implements RoleManager {
5252
/**
5353
* clear clears all stored data and resets the role manager to the initial state.
5454
*/
55-
public clear(): void {
55+
public async clear() {
5656
this.allRoles = new Map<string, Role>();
5757
}
5858

@@ -153,7 +153,7 @@ export class DefaultRoleManager implements RoleManager {
153153
/**
154154
* printRoles prints all the roles to log.
155155
*/
156-
public printRoles(): void {
156+
public async printRoles() {
157157
[...this.allRoles.values()].map(n => {
158158
logPrint(n.toString());
159159
});

src/rbac/roleManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// RoleManager provides interface to define the operations for managing roles.
1616
export interface RoleManager {
1717
// Clear clears all stored data and resets the role manager to the initial state.
18-
clear(): void;
18+
clear(): Promise<void>;
1919
// AddLink adds the inheritance link between two roles. role: name1 and role: name2.
2020
// domain is a prefix to the roles (can be used for other purposes).
2121
addLink(name1: string, name2: string, ...domain: string[]): Promise<void>;
@@ -32,5 +32,5 @@ export interface RoleManager {
3232
// domain is a prefix to the users (can be used for other purposes).
3333
getUsers(name: string, ...domain: string[]): Promise<string[]>;
3434
// PrintRoles prints all the roles to log.
35-
printRoles(): void;
35+
printRoles(): Promise<void>;
3636
}

0 commit comments

Comments
 (0)