From da608aea8f40945f072c8ee2cdb1db031cd65ef9 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Sat, 6 Feb 2021 22:13:19 +0800 Subject: [PATCH] refactor: rename the built-in BatchFileAdapter to FileAdapter (#236) * refactor: refactor BatchFileAdapter Rename the built-in BatchFileAdapter Signed-off-by: Zxilly * feat: add BatchFileAdapter add BatchFileAdapter to keep backward compatibility, and add JSDoc comment @deprecated Signed-off-by: Zxilly * fix: format comment Signed-off-by: Zxilly --- src/persist/batchFileAdapter.ts | 17 +++-------------- src/persist/fileAdapter.ts | 23 +++++++++++++++++++++++ src/persist/index.ts | 1 - test/managementAPI.test.ts | 18 +++++++++--------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/persist/batchFileAdapter.ts b/src/persist/batchFileAdapter.ts index 8a8d0fd..d764f18 100644 --- a/src/persist/batchFileAdapter.ts +++ b/src/persist/batchFileAdapter.ts @@ -2,8 +2,9 @@ import { FileAdapter } from './fileAdapter'; import { BatchAdapter } from './batchAdapter'; /** - * FileAdapter is the file adapter for Casbin. - * It can load policy from file or save policy to file. + * BatchFileAdapter is the file adapter for Casbin. + * It can add policies and remove policies. + * @deprecated The class should not be used, you should use FileAdapter. */ export class BatchFileAdapter extends FileAdapter implements BatchAdapter { /** @@ -13,16 +14,4 @@ export class BatchFileAdapter extends FileAdapter implements BatchAdapter { constructor(filePath: string) { super(filePath); } - - // addPolicies adds policy rules to the storage. - // This is part of the Auto-Save feature. - public async addPolicies(sec: string, ptype: string, rules: string[][]): Promise { - throw new Error('not implemented'); - } - - // removePolicies removes policy rules from the storage. - // This is part of the Auto-Save feature. - public async removePolicies(sec: string, ptype: string, rules: string[][]): Promise { - throw new Error('not implemented'); - } } diff --git a/src/persist/fileAdapter.ts b/src/persist/fileAdapter.ts index 11c88ca..64dc345 100644 --- a/src/persist/fileAdapter.ts +++ b/src/persist/fileAdapter.ts @@ -86,6 +86,21 @@ export class FileAdapter implements Adapter { public async addPolicy(sec: string, ptype: string, rule: string[]): Promise { throw new Error('not implemented'); } + /** + * addPolicies adds policy rules to the storage. + This is part of the Auto-Save feature. + */ + public async addPolicies(sec: string, ptype: string, rules: string[][]): Promise { + throw new Error('not implemented'); + } + + /** + * UpdatePolicy updates a policy rule from storage. + * This is part of the Auto-Save feature. + */ + updatePolicy(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise { + throw new Error('not implemented'); + } /** * removePolicy removes a policy rule from the storage. @@ -94,6 +109,14 @@ export class FileAdapter implements Adapter { throw new Error('not implemented'); } + /** + * removePolicies removes policy rules from the storage. + * This is part of the Auto-Save feature. + */ + public async removePolicies(sec: string, ptype: string, rules: string[][]): Promise { + throw new Error('not implemented'); + } + /** * removeFilteredPolicy removes policy rules that match the filter from the storage. */ diff --git a/src/persist/index.ts b/src/persist/index.ts index 03b1906..201462a 100644 --- a/src/persist/index.ts +++ b/src/persist/index.ts @@ -6,4 +6,3 @@ export * from './watcher'; export * from './filteredAdapter'; export * from './defaultFilteredAdapter'; export * from './batchAdapter'; -export * from './batchFileAdapter'; diff --git a/test/managementAPI.test.ts b/test/managementAPI.test.ts index 5cdfbb9..755acc3 100644 --- a/test/managementAPI.test.ts +++ b/test/managementAPI.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { newEnforcer, Enforcer, Util } from '../src'; -import { BatchFileAdapter } from '../src/persist'; +import { FileAdapter } from '../src'; let e = {} as Enforcer; @@ -147,7 +147,7 @@ test('addPolicy', async () => { }); test('addPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const rules = [ ['jack', 'data4', 'read'], @@ -170,7 +170,7 @@ test('addNamedPolicy', async () => { }); test('addNamedPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const rules = [ ['jack', 'data4', 'read'], @@ -193,7 +193,7 @@ test('removePolicy', async () => { }); test('removePolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const rules = [ ['jack', 'data4', 'read'], @@ -225,7 +225,7 @@ test('removeNamedPolicy', async () => { }); test('removeNamedPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const rules = [ ['jack', 'data4', 'read'], @@ -265,7 +265,7 @@ test('addGroupingPolicy', async () => { }); test('addGroupingPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const groupingRules = [ ['ham', 'data4_admin'], @@ -281,7 +281,7 @@ test('addNamedGroupingPolicy', async () => { }); test('addNamedGroupingPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const groupingRules = [ ['ham', 'data4_admin'], @@ -297,7 +297,7 @@ test('removeGroupingPolicy', async () => { }); test('removeGroupingPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const groupingRules = [ ['ham', 'data4_admin'], @@ -320,7 +320,7 @@ test('removeFilteredNamedGroupingPolicy', async () => { }); test('removeNamedGroupingPolicies', async () => { - const a = new BatchFileAdapter('examples/rbac_policy.csv'); + const a = new FileAdapter('examples/rbac_policy.csv'); e.setAdapter(a); const groupingRules = [ ['ham', 'data4_admin'],