Skip to content

Commit

Permalink
feat: Add lazyload option at enforcer init method (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-403 <[email protected]>
  • Loading branch information
Gabriel-403 authored Jun 18, 2021
1 parent 8e3dd5d commit e858dcb
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
public async initWithFile(modelPath: string, policyPath: string, lazyLoad = false): Promise<void> {
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<void> {
public async initWithString(modelPath: string, policyString: string, lazyLoad = false): Promise<void> {
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<void> {
public async initWithAdapter(modelPath: string, adapter: Adapter, lazyLoad = false): Promise<void> {
const m = newModel(modelPath, '');
await this.initWithModelAndAdapter(m, adapter);
await this.initWithModelAndAdapter(m, adapter, lazyLoad);

this.modelPath = modelPath;
}
Expand All @@ -58,16 +61,17 @@ 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<void> {
public async initWithModelAndAdapter(m: Model, adapter?: Adapter, lazyLoad = false): Promise<void> {
if (adapter) {
this.adapter = adapter;
}

this.model = m;
this.model.printModel();

if (this.adapter) {
if (!lazyLoad && this.adapter) {
await this.loadPolicy();
}
}
Expand Down

0 comments on commit e858dcb

Please sign in to comment.