Skip to content

Commit

Permalink
feat: multiple section types
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-yadav13 committed Oct 11, 2022
1 parent 87f08d6 commit 04bb86f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
34 changes: 21 additions & 13 deletions src/coreEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,11 @@ export class CoreEnforcer {
* @return whether to allow the request.
*/
public enforceSync(...rvals: any[]): boolean {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunSync(this.privateEnforce(false, false, enforceContext, ...rvals));
if (rvals.length) {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunSync(this.privateEnforce(false, false, enforceContext, ...rvals));
}
}
return generatorRunSync(this.privateEnforce(false, false, this.defaultEnforceContext, ...rvals));
}
Expand All @@ -590,9 +592,11 @@ export class CoreEnforcer {
* @return whether to allow the request and the reason rule.
*/
public enforceExSync(...rvals: any[]): [boolean, string[]] {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunSync(this.privateEnforce(false, true, enforceContext, ...rvals));
if (rvals.length) {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunSync(this.privateEnforce(false, true, enforceContext, ...rvals));
}
}
return generatorRunSync(this.privateEnforce(false, true, this.defaultEnforceContext, ...rvals));
}
Expand All @@ -613,11 +617,13 @@ export class CoreEnforcer {
* @return whether to allow the request.
*/
public async enforce(...rvals: any[]): Promise<boolean> {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunAsync(this.privateEnforce(true, false, enforceContext, ...rvals));
if (rvals.length) {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunAsync(this.privateEnforce(true, false, enforceContext, ...rvals));
}
}
return generatorRunAsync(this.privateEnforce(true, false, this.defaultEnforceContext, ...rvals));
return generatorRunAsync(this.privateEnforce(true, false, this.defaultEnforceContext, ...rvals));
}

/**
Expand All @@ -629,9 +635,11 @@ export class CoreEnforcer {
* @return whether to allow the request and the reason rule.
*/
public async enforceEx(...rvals: any[]): Promise<[boolean, string[]]> {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunAsync(this.privateEnforce(true, true, enforceContext, ...rvals));
if (rvals.length) {
if (rvals[0] instanceof EnforceContext) {
const enforceContext: EnforceContext = rvals.shift();
return generatorRunAsync(this.privateEnforce(true, true, enforceContext, ...rvals));
}
}
return generatorRunAsync(this.privateEnforce(true, true, this.defaultEnforceContext, ...rvals));
}
Expand Down
12 changes: 6 additions & 6 deletions test/enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ test('TestInitWithAdapter', async () => {

test('TestInitWithStringAdapter', async () => {
const policy = readFileSync('examples/basic_policy.csv').toString();
const adapter = new MemoryAdapter(policy);
const adapter = new StringAdapter(policy);
const e = await newEnforcer('examples/basic_model.conf', adapter);

await testEnforce(e, 'alice', 'data1', 'read', true);
Expand Down Expand Up @@ -456,7 +456,7 @@ test('TestSetAdapterFromString', async () => {

const policy = readFileSync('examples/basic_policy.csv').toString();

const a = new MemoryAdapter(policy);
const a = new StringAdapter(policy);
e.setAdapter(a);
await e.loadPolicy();

Expand Down Expand Up @@ -491,7 +491,7 @@ test('TestInitEmpty with String Adapter', async () => {
m.addDef('m', 'm', 'r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)');

const policy = readFileSync('examples/keymatch_policy.csv').toString();
const a = new MemoryAdapter(policy);
const a = new StringAdapter(policy);

e.setModel(m);
e.setAdapter(a);
Expand Down Expand Up @@ -524,11 +524,11 @@ describe('Unimplemented File Adapter methods', () => {

describe('Unimplemented String Adapter methods', () => {
let e = {} as Enforcer;
let a = {} as MemoryAdapter;
let a = {} as StringAdapter;

beforeEach(async () => {
const policy = readFileSync('examples/basic_policy.csv').toString();
a = new MemoryAdapter(policy);
const a = new StringAdapter(policy);
e = await newEnforcer('examples/basic_model.conf', a);
});

Expand Down Expand Up @@ -579,7 +579,7 @@ test('test ABAC multiple eval()', async () => {
m.addDef('e', 'e', 'some(where (p.eft == allow))');
m.addDef('m', 'm', 'eval(p.sub_rule_1) && eval(p.sub_rule_2) && r.act == p.act');

const policy = new MemoryAdapter(
const policy = new StringAdapter(
`
p, r.sub > 50, r.obj > 50, read
`
Expand Down

0 comments on commit 04bb86f

Please sign in to comment.