Skip to content

Commit

Permalink
Merge pull request #65 from casbin/casbin-next
Browse files Browse the repository at this point in the history
Make all function to async function in rolemanager interface
  • Loading branch information
hsluoyz authored Apr 29, 2019
2 parents 70d4c57 + 08538f4 commit 157f0b5
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 112 deletions.
100 changes: 0 additions & 100 deletions src/casbin.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import { compileAsync } from 'expression-eval';
import * as _ from 'lodash';

import { DefaultEffector, Effect, Effector } from './effect';
import { FunctionMap, Model } from './model';
import { FunctionMap, Model, newModel } from './model';
import { Adapter, Filter, FilteredAdapter, Watcher } from './persist';
import { DefaultRoleManager, RoleManager } from './rbac';
import { generateGFunction } from './util';
import { newModel } from './casbin';
import { getLogger, logPrint } from './log';

/**
Expand Down Expand Up @@ -242,8 +241,7 @@ export class CoreEnforcer {
* role inheritance relations.
*/
public async buildRoleLinks(): Promise<void> {
// error intentionally ignored
this.rm.clear();
await this.rm.clear();
await this.model.buildRoleLinks(this.rm);
}

Expand Down
62 changes: 60 additions & 2 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// limitations under the License.

import { ManagementEnforcer } from './managementEnforcer';
import { FunctionMap, Model } from './model';
import { FunctionMap, Model, newModel } from './model';
import { Adapter, FileAdapter } from './persist';
import { newModel } from './casbin';
import { getLogger } from './log';

/**
* Enforcer = ManagementEnforcer + RBAC API.
Expand Down Expand Up @@ -303,3 +303,61 @@ export class Enforcer extends ManagementEnforcer {
return res;
}
}

/**
* newEnforcer creates an enforcer via file or DB.
*
* File:
* ```js
* const e = new Enforcer('path/to/basic_model.conf', 'path/to/basic_policy.csv');
* ```
*
* MySQL DB:
* ```js
* const a = new MySQLAdapter('mysql', 'mysql_username:mysql_password@tcp(127.0.0.1:3306)/');
* const e = new Enforcer('path/to/basic_model.conf', a);
* ```
*
* @param params
*/
export async function newEnforcer(...params: any[]): Promise<Enforcer> {
const e = new Enforcer();

let parsedParamLen = 0;
if (params.length >= 1) {
const enableLog = params[params.length - 1];
if (typeof enableLog === 'boolean') {
getLogger().enableLog(enableLog);
parsedParamLen++;
}
}

if (params.length - parsedParamLen === 2) {
if (typeof params[0] === 'string') {
if (typeof params[1] === 'string') {
await e.initWithFile(params[0].toString(), params[1].toString());
} else {
await e.initWithAdapter(params[0].toString(), params[1]);
}
} else {
if (typeof params[1] === 'string') {
throw new Error('Invalid parameters for enforcer.');
} else {
await e.initWithModelAndAdapter(params[0], params[1]);
}
}
} else if (params.length - parsedParamLen === 1) {
if (typeof params[0] === 'string') {
await e.initWithFile(params[0], '');
} else {
// @ts-ignore
await e.initWithModelAndAdapter(params[0], null);
}
} else if (params.length === parsedParamLen) {
await e.initWithFile('', '');
} else {
throw new Error('Invalid parameters for enforcer.');
}

return e;
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ export * from './effect';
export * from './model';
export * from './persist';
export * from './rbac';
export * from './casbin';
export * from './log';
export { Util };
2 changes: 1 addition & 1 deletion src/model/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ export class Assertion {
}

logPrint(`Role links for: ${this.key}`);
this.rm.printRoles();
await this.rm.printRoles();
}
}
19 changes: 19 additions & 0 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,22 @@ export class Model {
});
}
}

/**
* newModel creates a model.
*/
export function newModel(...text: string[]): Model {
const m = new Model();

if (text.length === 2) {
if (text[0] !== '') {
m.loadModel(text[0]);
}
} else if (text.length === 1) {
m.loadModelFromText(text[0]);
} else if (text.length !== 0) {
throw new Error('Invalid parameters for model.');
}

return m;
}
4 changes: 2 additions & 2 deletions src/rbac/defaultRoleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class DefaultRoleManager implements RoleManager {
/**
* clear clears all stored data and resets the role manager to the initial state.
*/
public clear(): void {
public async clear() {
this.allRoles = new Map<string, Role>();
}

Expand Down Expand Up @@ -153,7 +153,7 @@ export class DefaultRoleManager implements RoleManager {
/**
* printRoles prints all the roles to log.
*/
public printRoles(): void {
public async printRoles() {
[...this.allRoles.values()].map(n => {
logPrint(n.toString());
});
Expand Down
4 changes: 2 additions & 2 deletions src/rbac/roleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// RoleManager provides interface to define the operations for managing roles.
export interface RoleManager {
// Clear clears all stored data and resets the role manager to the initial state.
clear(): void;
clear(): Promise<void>;
// AddLink adds the inheritance link between two roles. role: name1 and role: name2.
// domain is a prefix to the roles (can be used for other purposes).
addLink(name1: string, name2: string, ...domain: string[]): Promise<void>;
Expand All @@ -32,5 +32,5 @@ export interface RoleManager {
// domain is a prefix to the users (can be used for other purposes).
getUsers(name: string, ...domain: string[]): Promise<string[]>;
// PrintRoles prints all the roles to log.
printRoles(): void;
printRoles(): Promise<void>;
}

0 comments on commit 157f0b5

Please sign in to comment.