Skip to content

Commit

Permalink
fix: Deal with absence truth-test function
Browse files Browse the repository at this point in the history
  • Loading branch information
mblarsen committed May 4, 2018
1 parent df6334d commit 5dda25f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

export const GlobalRule = 'GLOBAL_RULE'

const isNameless = fn => typeof fn === 'function' && fn.name === ''
const assumeGlobal = sub =>
typeof sub === 'boolean' ||
typeof sub === 'undefined' ||
(typeof sub === 'function' && sub.name === '')

/**
* Simple ACL library for the browser inspired by Laravel's guards and policies.
Expand Down Expand Up @@ -48,8 +51,8 @@ export default class Acl {
* @returns {Acl}
*/
rule(verbs, subject, test = true) {
if (isNameless(subject)) {
test = subject
if (assumeGlobal(subject)) {
test = typeof subject === 'undefined' ? true : subject
subject = GlobalRule
}
const subjectName = this.subjectMapper(subject)
Expand Down
8 changes: 7 additions & 1 deletion test/Acl.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Acl from '../index.js'
import Acl, {GlobalRule} from '../index.js'

class User {}
class Apple {}
Expand All @@ -21,6 +21,12 @@ describe('The basics', () => {
acl.rule('purgeInactive', user => user.isAdmin)
expect(acl.can({isAdmin: true}, 'purgeInactive')).toBe(true)
expect(acl.can({isAdmin: false}, 'purgeInactive')).toBe(false)
acl.rule('contact')
expect(acl.can({}, 'contact')).toBe(true)
acl.rule('contact', false)
expect(acl.can({}, 'contact')).toBe(false)
acl.rule('pillage', false)
expect(acl.can({}, 'pillage')).toBe(false)
})

test('Cannot eat apples (no rule)', () => {
Expand Down

0 comments on commit 5dda25f

Please sign in to comment.