Skip to content

Commit

Permalink
Adjust export file and bump to v1.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
nodece committed Dec 18, 2018
1 parent 4996d61 commit 7bc9a30
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 98 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "casbin",
"version": "1.1.7",
"version": "1.1.8",
"description": "An authorization library that supports access control models like ACL, RBAC, ABAC in Node.JS",
"main": "lib/casbin.js",
"typings": "lib/casbin.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"precommit": "lint-staged",
"prepublish": "yarn run lint && yarn run test && yarn build",
Expand Down
54 changes: 12 additions & 42 deletions src/casbin.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import * as Util from './util';
import { Enforcer } from './enforcer';
import { setEnableLog } from './util';
import { Model } from './model';

export * from './enforcer';
export * from './effect';
export * from './model';
export * from './persist';
export * from './rbac';
export { Util };

/**
* newModel creates a model.
*/
export function newModel(...text: string[]): Model {
function newModel(...text: string[]): Model {
const m = new Model();

if (text.length === 2) {
Expand Down Expand Up @@ -46,7 +38,7 @@ export function newModel(...text: string[]): Model {
* @param params
*/

export function newEnforcer(...params: any[]): Promise<Enforcer> {
async function newEnforcer(...params: any[]): Promise<Enforcer> {
const e = new Enforcer();

let parsedParamLen = 0;
Expand All @@ -61,56 +53,34 @@ export function newEnforcer(...params: any[]): Promise<Enforcer> {
if (params.length - parsedParamLen === 2) {
if (typeof params[0] === 'string') {
if (typeof params[1] === 'string') {
return new Promise<Enforcer>((resolve, reject) => {
e.initWithFile(params[0].toString(), params[1].toString()).then(() => {
return resolve(e);
});
});
await e.initWithFile(params[0].toString(), params[1].toString());
} else {
return new Promise<Enforcer>((resolve) => {
e.initWithAdapter(params[0].toString(), params[1]).then(() => {
return resolve(e);
});
});
await e.initWithAdapter(params[0].toString(), params[1]);
}
} else {
if (typeof params[1] === 'string') {
throw new Error('Invalid parameters for enforcer.');
} else {
return new Promise<Enforcer>((resolve) => {
e.initWithModelAndAdapter(params[0], params[1]).then(() => {
return resolve(e);
});
});
await e.initWithModelAndAdapter(params[0], params[1]);
}
}
} else if (params.length - parsedParamLen === 1) {
if (typeof params[0] === 'string') {
return new Promise<Enforcer>((resolve) => {
e.initWithFile(params[0], '').then(() => {
return resolve(e);
});
});
await e.initWithFile(params[0], '');
} else {
return new Promise<Enforcer>((resolve) => {
// @ts-ignore
e.initWithModelAndAdapter(params[0], null).then(() => {
return resolve(e);
});
});
// @ts-ignore
await e.initWithModelAndAdapter(params[0], null);
}
} else if (params.length === parsedParamLen) {
return new Promise<Enforcer>((resolve) => {
e.initWithFile('', '').then(() => {
return resolve(e);
});
});
await e.initWithFile('', '');
} else {
throw new Error('Invalid parameters for enforcer.');
}

return e;
}

export default {
export {
newEnforcer,
newModel
};
4 changes: 2 additions & 2 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FunctionMap, Model } from './model';
import { Adapter, Filter, FilteredAdapter, Watcher } from './persist';
import { DefaultRoleManager, RoleManager } from './rbac';
import { generateGFunction, getEnableLog, logPrint, setEnableLog } from './util';
import casbin from '../src/casbin';
import { newModel } from './casbin';

/**
* CoreEnforcer defines the core functionality of an enforcer.
Expand Down Expand Up @@ -61,7 +61,7 @@ export class CoreEnforcer {
console.warn(
'[node-casbin] Enforce.newModel() is deprecated, please use casbin.newModel().'
);
return casbin.newModel(...text);
return newModel(...text);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { ManagementEnforcer } from './managementEnforcer';
import { FunctionMap, Model } from './model';
import { Adapter, FileAdapter } from './persist';
import casbin from '../src/casbin';
import { newEnforcer, newModel } from './casbin';

/**
* Enforcer = ManagementEnforcer + RBAC API.
Expand All @@ -42,7 +42,7 @@ export class Enforcer extends ManagementEnforcer {
console.warn(
'[node-casbin] Enforce.newEnforcer() is deprecated, please use casbin.newEnforcer().'
);
return casbin.newEnforcer(...params);
return newEnforcer(...params);
}

/**
Expand All @@ -61,7 +61,7 @@ export class Enforcer extends ManagementEnforcer {
* @param adapter current adapter instance
*/
public async initWithAdapter(modelPath: string, adapter: Adapter): Promise<void> {
const m = casbin.newModel(modelPath, '');
const m = newModel(modelPath, '');
await this.initWithModelAndAdapter(m, adapter);

this.modelPath = modelPath;
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Util from './util';

export * from './enforcer';
export * from './effect';
export * from './model';
export * from './persist';
export * from './rbac';
export * from './casbin';
export { Util };
62 changes: 31 additions & 31 deletions test/enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import casbin, { Enforcer, FileAdapter, Util } from '../src/casbin';
import { newModel, newEnforcer, Enforcer, FileAdapter, Util } from '../src';

function testEnforce(e: Enforcer, sub: string, obj: string, act: string, res: boolean): void {
expect(e.enforce(sub, obj, act)).toBe(res);
Expand All @@ -26,15 +26,15 @@ function testGetPolicy(e: Enforcer, res: string[][]) {
}

test('TestKeyMatchModelInMemory', async () => {
const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)');

const a = new FileAdapter('examples/keymatch_policy.csv');

let e = await casbin.newEnforcer(m, a);
let e = await newEnforcer(m, a);

testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true);
testEnforce(e, 'alice', '/alice_data/resource1', 'POST', true);
Expand All @@ -58,7 +58,7 @@ test('TestKeyMatchModelInMemory', async () => {
testEnforce(e, 'cathy', '/cathy_data', 'POST', true);
testEnforce(e, 'cathy', '/cathy_data', 'DELETE', false);

e = await casbin.newEnforcer(m);
e = await newEnforcer(m);
await a.loadPolicy(e.getModel());

testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true);
Expand All @@ -85,43 +85,43 @@ test('TestKeyMatchModelInMemory', async () => {
});

test('TestKeyMatchModelInMemoryDeny', async () => {
const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('e', 'e', '!some(where (p.eft == deny))');
m.addDef('m', 'm', 'r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)');

const a = new FileAdapter('examples/keymatch_policy.csv');

const e = await casbin.newEnforcer(m, a);
const e = await newEnforcer(m, a);

testEnforce(e, 'alice', '/alice_data/resource2', 'POST', true);
});

test('TestRBACModelInMemoryIndeterminate', async () => {
const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('g', 'g', '_, _');
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

const e = await casbin.newEnforcer(m);
const e = await newEnforcer(m);

await e.addPermissionForUser('alice', 'data1', 'invalid');

testEnforce(e, 'alice', 'data1', 'read', false);
});

test('TestRBACModelInMemory', async () => {
const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('g', 'g', '_, _');
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

const e = await casbin.newEnforcer(m);
const e = await newEnforcer(m);

await e.addPermissionForUser('alice', 'data1', 'read');
await e.addPermissionForUser('bob', 'data2', 'write');
Expand Down Expand Up @@ -157,12 +157,12 @@ e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
`;
const m = casbin.newModel(text);
const m = newModel(text);
// The above is the same as:
// const m = casbin.newModel();
// const m = newModel();
// m.loadModelFromText(text);

const e = await casbin.newEnforcer(m);
const e = await newEnforcer(m);

await e.addPermissionForUser('alice', 'data1', 'read');
await e.addPermissionForUser('bob', 'data2', 'write');
Expand All @@ -181,14 +181,14 @@ m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
});

test('TestNotUsedRBACModelInMemory', async () => {
const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('g', 'g', '_, _');
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act');

const e = await casbin.newEnforcer(m);
const e = await newEnforcer(m);

await e.addPermissionForUser('alice', 'data1', 'read');
await e.addPermissionForUser('bob', 'data2', 'write');
Expand All @@ -204,26 +204,26 @@ test('TestNotUsedRBACModelInMemory', async () => {
});

test('TestReloadPolicy', async () => {
const e = await casbin.newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');

await e.loadPolicy();
testGetPolicy(e, [['alice', 'data1', 'read'], ['bob', 'data2', 'write'], ['data2_admin', 'data2', 'read'], ['data2_admin', 'data2', 'write']]);
});

test('TestSavePolicy', async () => {
const e = await casbin.newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');

await e.savePolicy();
});

test('TestClearPolicy', async () => {
const e = await casbin.newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');
const e = await newEnforcer('examples/rbac_model.conf', 'examples/rbac_policy.csv');

e.clearPolicy();
});

test('TestEnableEnforce', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');

e.enableEnforce(false);
testEnforce(e, 'alice', 'data1', 'read', true);
Expand All @@ -247,9 +247,9 @@ test('TestEnableEnforce', async () => {
});

test('TestEnableLog', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv', true);
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv', true);
// The log is enabled by default, so the above is the same with:
// const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
// const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');

testEnforce(e, 'alice', 'data1', 'read', true);
testEnforce(e, 'alice', 'data1', 'write', false);
Expand All @@ -273,7 +273,7 @@ test('TestEnableLog', async () => {
});

test('TestEnableAutoSave', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');

e.enableAutoSave(false);
// Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
Expand Down Expand Up @@ -311,7 +311,7 @@ test('TestEnableAutoSave', async () => {

test('TestInitWithAdapter', async () => {
const adapter = new FileAdapter('examples/basic_policy.csv');
const e = await casbin.newEnforcer('examples/basic_model.conf', adapter);
const e = await newEnforcer('examples/basic_model.conf', adapter);

testEnforce(e, 'alice', 'data1', 'read', true);
testEnforce(e, 'alice', 'data1', 'write', false);
Expand All @@ -324,15 +324,15 @@ test('TestInitWithAdapter', async () => {
});

test('TestRoleLinks', async () => {
const e = await casbin.newEnforcer('examples/rbac_model.conf');
const e = await newEnforcer('examples/rbac_model.conf');
e.enableAutoBuildRoleLinks(false);
e.buildRoleLinks();
e.enforce('user501', 'data9', 'read');
});

test('TestGetAndSetModel', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e2 = await casbin.newEnforcer('examples/basic_with_root_model.conf', 'examples/basic_policy.csv');
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e2 = await newEnforcer('examples/basic_with_root_model.conf', 'examples/basic_policy.csv');

testEnforce(e, 'root', 'data1', 'read', false);

Expand All @@ -342,8 +342,8 @@ test('TestGetAndSetModel', async () => {
});

test('TestGetAndSetAdapterInMem', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e2 = await casbin.newEnforcer('examples/basic_model.conf', 'examples/basic_inverse_policy.csv');
const e = await newEnforcer('examples/basic_model.conf', 'examples/basic_policy.csv');
const e2 = await newEnforcer('examples/basic_model.conf', 'examples/basic_inverse_policy.csv');

testEnforce(e, 'alice', 'data1', 'read', true);
testEnforce(e, 'alice', 'data1', 'write', false);
Expand All @@ -357,7 +357,7 @@ test('TestGetAndSetAdapterInMem', async () => {
});

test('TestSetAdapterFromFile', async () => {
const e = await casbin.newEnforcer('examples/basic_model.conf');
const e = await newEnforcer('examples/basic_model.conf');

testEnforce(e, 'alice', 'data1', 'read', false);

Expand All @@ -369,9 +369,9 @@ test('TestSetAdapterFromFile', async () => {
});

test('TestInitEmpty', async () => {
const e = await casbin.newEnforcer();
const e = await newEnforcer();

const m = casbin.newModel();
const m = newModel();
m.addDef('r', 'r', 'sub, obj, act');
m.addDef('p', 'p', 'sub, obj, act');
m.addDef('e', 'e', 'some(where (p.eft == allow))');
Expand Down
Loading

0 comments on commit 7bc9a30

Please sign in to comment.