From a415838a514706af8a9399c899959bdb069619d4 Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Wed, 12 Aug 2020 01:57:54 +0800 Subject: [PATCH] fix: add check if the adapter implements BatchAdapter Signed-off-by: Zixuan Liu --- src/internalEnforcer.ts | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/internalEnforcer.ts b/src/internalEnforcer.ts index e2e801c..6e5b213 100644 --- a/src/internalEnforcer.ts +++ b/src/internalEnforcer.ts @@ -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'); } } @@ -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'); } }