Skip to content

Commit

Permalink
feat: update strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
palmcivet committed Jul 7, 2024
1 parent 9e5c3fe commit 0f9c485
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/spectra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export class Spectra {
(p) => p.getEffect() === 'DENY'
);

const shouldDeny = denyPolicies.every((p) => {
const shouldDeny = denyPolicies.some((p) => {
return Evaluator.evaluate(relatedData, p.getFilter());
});

if (shouldDeny) {
return false;
}

return allowPolicies.every((p) => {
return allowPolicies.some((p) => {
return Evaluator.evaluate(relatedData, p.getFilter());
});
}
Expand Down Expand Up @@ -65,13 +65,13 @@ export class Spectra {
(p) => p.getEffect() === 'DENY'
);

const shouldDeny = denyPolicies.every((p) => {
const shouldDeny = denyPolicies.some((p) => {
return Evaluator.evaluate(relatedData, p.getFilter());
});

const applied = shouldDeny
? false
: allowPolicies.every((p) => {
: allowPolicies.some((p) => {
return Evaluator.evaluate(relatedData, p.getFilter());
});

Expand Down
4 changes: 4 additions & 0 deletions src/utils/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export function isValidOrExpressionDefinition(
return false;
}

if (expression.or.length <= 1) {
return false;
}

return true;
}

Expand Down
27 changes: 27 additions & 0 deletions test/debugger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, test } from '@jest/globals';
import { Spectra, Policy, BinaryExpression, and } from '@/index';
import debug_and from './fixtures/debug_and.json';

describe('Debugger', () => {
test('basic usage', () => {
const allowPolicy = new Policy({
filter: and([debug_and as any]),
permissions: ['EDIT_FILE'],
effect: 'ALLOW',
});

const denyPolicy = new Policy({
filter: new BinaryExpression('user.id', '=', 2),
permissions: ['EDIT_FILE'],
effect: 'DENY',
});

const reporter = Spectra.debug(
[allowPolicy, denyPolicy],
() => ({ 'user.id': 1 }),
'EDIT_FILE'
);

expect(reporter).toBeTruthy();
});
});
18 changes: 18 additions & 0 deletions test/fixtures/debug_and.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"and": [
["user.id", "<>", null],
{ "not": ["user.name", "!=", null] },
{
"or": [
["team.name", "<>", null],
["team.remark", "!=", null]
]
},
{
"and": [
["file.size", ">", 0],
["file.name", "<>", null]
]
}
]
}
49 changes: 27 additions & 22 deletions test/policies.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import { describe, expect, test } from '@jest/globals';
import { Policy, BinaryExpression, Spectra } from '@/index';
import { Policy, BinaryExpression, not, and, AndExpression } from '@/index';

describe('Policies', () => {
test('getter', () => {
const allowPolicy = new Policy({
filter: new BinaryExpression('user.id', '=', 1),
permissions: ['EDIT_FILE'],
effect: 'ALLOW',
test('get properties', () => {
const policy = new Policy({
description: 'Readonly policy',
permissions: ['READ_ONLY'],
effect: 'DENY',
filter: new BinaryExpression('user.id', '=', 2),
});

expect(allowPolicy.getEffect()).toBe('ALLOW');
expect(policy.getEffect()).toBe('DENY');
expect(policy.getPermissions()).toEqual(['READ_ONLY']);
expect(policy.getDescription()).toEqual('Readonly policy');
expect(policy.getFilter().getFields()).toEqual(['user.id']);
});

test('complex policies', () => {
const allowPolicy = new Policy({
filter: new BinaryExpression('user.id', '=', 1),
permissions: ['EDIT_FILE'],
filter: and([
['user.id', '<>', 0],
{
or: [
['team.name', '<>', null],
['team.alias', '!=', { ref: 'team.name' }],
],
},
not(['user.name', '=', null]),
new AndExpression([
new BinaryExpression('file.id', '!=', 0),
new BinaryExpression('file.name', '<>', null),
]),
]),
permissions: ['READ_ONLY'],
effect: 'ALLOW',
});

const denyPolicy = new Policy({
filter: new BinaryExpression('user.id', '=', 2),
permissions: ['EDIT_FILE'],
effect: 'DENY',
});

const result = Spectra.validate(
[allowPolicy, denyPolicy],
{ load: () => ({ 'user.id': 1 }) },
'EDIT_FILE'
);

expect(result).toBe(true);
expect(allowPolicy.getFilter()).toBeTruthy();
});
});

0 comments on commit 0f9c485

Please sign in to comment.