diff --git a/src/enforcer.ts b/src/enforcer.ts index 67cb36a..fd0d280 100644 --- a/src/enforcer.ts +++ b/src/enforcer.ts @@ -26,30 +26,33 @@ export class Enforcer extends ManagementEnforcer { * initWithFile initializes an enforcer with a model file and a policy file. * @param modelPath model file path * @param policyPath policy file path + * @param lazyLoad lazyLoad whether to load policy at initial time */ - public async initWithFile(modelPath: string, policyPath: string): Promise { + public async initWithFile(modelPath: string, policyPath: string, lazyLoad = false): Promise { const a = new FileAdapter(policyPath); - await this.initWithAdapter(modelPath, a); + await this.initWithAdapter(modelPath, a, lazyLoad); } /** * initWithFile initializes an enforcer with a model file and a policy file. * @param modelPath model file path * @param policyString policy CSV string + * @param lazyLoad whether to load policy at initial time */ - public async initWithString(modelPath: string, policyString: string): Promise { + public async initWithString(modelPath: string, policyString: string, lazyLoad = false): Promise { const a = new StringAdapter(policyString); - await this.initWithAdapter(modelPath, a); + await this.initWithAdapter(modelPath, a, lazyLoad); } /** * initWithAdapter initializes an enforcer with a database adapter. * @param modelPath model file path * @param adapter current adapter instance + * @param lazyLoad whether to load policy at initial time */ - public async initWithAdapter(modelPath: string, adapter: Adapter): Promise { + public async initWithAdapter(modelPath: string, adapter: Adapter, lazyLoad = false): Promise { const m = newModel(modelPath, ''); - await this.initWithModelAndAdapter(m, adapter); + await this.initWithModelAndAdapter(m, adapter, lazyLoad); this.modelPath = modelPath; } @@ -58,8 +61,9 @@ export class Enforcer extends ManagementEnforcer { * initWithModelAndAdapter initializes an enforcer with a model and a database adapter. * @param m model instance * @param adapter current adapter instance + * @param lazyLoad whether to load policy at initial time */ - public async initWithModelAndAdapter(m: Model, adapter?: Adapter): Promise { + public async initWithModelAndAdapter(m: Model, adapter?: Adapter, lazyLoad = false): Promise { if (adapter) { this.adapter = adapter; } @@ -67,7 +71,7 @@ export class Enforcer extends ManagementEnforcer { this.model = m; this.model.printModel(); - if (this.adapter) { + if (!lazyLoad && this.adapter) { await this.loadPolicy(); } }