Skip to content

Commit

Permalink
Merge 07780cb into 3a1dded
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Nov 9, 2021
2 parents 3a1dded + 07780cb commit b4d35b3
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 deletions.
3 changes: 3 additions & 0 deletions dist/main.d.ts
Expand Up @@ -215,7 +215,9 @@ declare class ActionBasedPolicy<T extends object> extends Policy<T, ActionBasedT
getStatements(this: ActionBasedPolicy<T>): ActionBasedType[];
evaluate(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
can(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
whyCan(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): ActionBasedType[];
cannot(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): boolean;
whyCannot(this: ActionBasedPolicy<T>, { action, context }: EvaluateActionBasedInterface<T>): ActionBasedType[];
generateProxy<U extends object>(this: ActionBasedPolicy<T>, obj: U, options?: ProxyOptions): U;
}

Expand All @@ -233,6 +235,7 @@ declare class IdentityBasedPolicy<T extends object> extends Policy<T, IdentityBa
getStatements(this: IdentityBasedPolicy<T>): IdentityBasedType[];
evaluate(this: IdentityBasedPolicy<T>, { action, resource, context }: EvaluateIdentityBasedInterface<T>): boolean;
can(this: IdentityBasedPolicy<T>, { action, resource, context }: EvaluateIdentityBasedInterface<T>): boolean;
whyCan(this: IdentityBasedPolicy<T>, { action, resource, context }: EvaluateIdentityBasedInterface<T>): IdentityBasedType[];
cannot(this: IdentityBasedPolicy<T>, { action, resource, context }: EvaluateIdentityBasedInterface<T>): boolean;
}

Expand Down
40 changes: 40 additions & 0 deletions dist/main.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main.es.js.map

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/ActionBasedPolicy.test.ts
Expand Up @@ -328,6 +328,10 @@ describe('ActionBasedPolicy Class', () => {
'getUser/${user.id}',
'updateUser/${user.id}'
]
},
{
effect: 'allow',
action: ['createAccount']
}
]
});
Expand Down Expand Up @@ -376,6 +380,10 @@ describe('ActionBasedPolicy Class', () => {
'getUser/${user.id}',
'updateUser/${user.id}'
]
},
{
effect: 'deny',
action: ['createAccount']
}
]
});
Expand Down
20 changes: 19 additions & 1 deletion src/IdentityBasedPolicy.test.ts
Expand Up @@ -366,7 +366,7 @@ describe('IdentityBasedPolicy Class', () => {
policy.evaluate({
action: 'read',
resource: 'secrets:123:ultra',
context: { user: { } }
context: { user: {} }
})
).toBe(true);
expect(
Expand Down Expand Up @@ -401,6 +401,11 @@ describe('IdentityBasedPolicy Class', () => {
effect: 'allow',
resource: ['posts:${user.id}:*'],
action: ['write', 'read', 'update']
},
{
effect: 'allow',
resource: ['projects:${user.id}:*'],
action: ['write', 'read']
}
]
});
Expand All @@ -412,6 +417,19 @@ describe('IdentityBasedPolicy Class', () => {
context: { user: { id: 123 } }
})
).toBe(true);
expect(
policy.whyCan({
action: 'read',
resource: 'posts:123:sshhh',
context: { user: { id: 123 } }
})
).toMatchObject([
{
effect: 'allow',
resource: ['posts:${user.id}:*'],
action: ['write', 'read', 'update']
}
]);
expect(
policy.can({
action: 'read',
Expand Down
18 changes: 18 additions & 0 deletions src/IdentityBasedPolicy.ts
Expand Up @@ -77,6 +77,24 @@ export class IdentityBasedPolicy<T extends object> extends Policy<
);
}

whyCan(
this: IdentityBasedPolicy<T>,
{ action, resource, context }: EvaluateIdentityBasedInterface<T>
): IdentityBasedType[] {
return this.allowStatements.reduce((statements, currentStatement) => {
const matches = currentStatement.matches({
action,
resource,
context: context || this.context,
conditionResolver: this.conditionResolver
});
if (matches) {
return [...statements, currentStatement.getStatement()];
}
return statements;
}, [] as IdentityBasedType[]);
}

cannot(
this: IdentityBasedPolicy<T>,
{ action, resource, context }: EvaluateIdentityBasedInterface<T>
Expand Down

0 comments on commit b4d35b3

Please sign in to comment.