Skip to content

Commit

Permalink
feat: support an array of operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Nov 29, 2023
1 parent 9ea3412 commit ad2dd22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,15 @@ test('it should memoize the can function', async () => {
expect(await can('user', 'user:create')).toBe(true)
expect(count).toBe(1)
})

test('should take an array of operatoins', async () => {
const {can} = canCant({
user: {
can: ['login', 'dashboard', 'logout']
}
})

expect(await can('user', ['login', 'dashboard'])).toBe(true)
expect(await can('user', ['login', 'user:update'])).toBe(false)
expect(await can('user', ['user:create', 'user:update'])).toBe(false)
})
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,23 @@ export const canCant = <Roles extends string>(data: ACL<Roles>) => {

const canFn = async (
role: string,
operation: string,
operation: string | string[],
params?: object
): Promise<boolean> => {
if (typeof operation !== 'string') {
const subCans = operation.map(op => can(role, op, params))

const subResults = await Promise.all(subCans)

return subResults.reduce((c, v) => {
if (!c) {
return false
}

return v
}, true)
}

if (!acl[role]) {
return false
}
Expand Down

0 comments on commit ad2dd22

Please sign in to comment.