Skip to content

Commit

Permalink
fix: add check if the adapter implements BatchAdapter
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
  • Loading branch information
nodece committed Aug 11, 2020
1 parent 25f8d58 commit a415838
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/internalEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ export class InternalEnforcer extends CoreEnforcer {
}
}

const batchAdapter = this.adapter as BatchAdapter;
if (batchAdapter && this.autoSave) {
try {
await batchAdapter.addPolicies(sec, ptype, rules);
} catch (e) {
if (e.message !== 'not implemented') {
throw e;
if (this.autoSave) {
if ('addPolicies' in this.adapter) {
try {
await this.adapter.addPolicies(sec, ptype, rules);
} catch (e) {
if (e.message !== 'not implemented') {
throw e;
}
}
} else {
throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
}
}

Expand Down Expand Up @@ -120,14 +123,17 @@ export class InternalEnforcer extends CoreEnforcer {
}
}

const batchAdapter = this.adapter as BatchAdapter;
if (batchAdapter && this.autoSave) {
try {
await batchAdapter.removePolicies(sec, ptype, rules);
} catch (e) {
if (e.message !== 'not implemented') {
throw e;
if (this.autoSave) {
if ('removePolicies' in this.adapter) {
try {
await this.adapter.removePolicies(sec, ptype, rules);
} catch (e) {
if (e.message !== 'not implemented') {
throw e;
}
}
} else {
throw new Error('cannot to save policy, the adapter does not implement the BatchAdapter');
}
}

Expand Down

0 comments on commit a415838

Please sign in to comment.