Skip to content

Commit

Permalink
implements save method with test
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Walker committed Jul 7, 2013
1 parent 438a9f8 commit d5559db
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 62 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Admittance

Role based access control module for node. The interface is based off the Yii php framework's RBAC interface. The implementation is written in coffee script and is entirely original.

This module is under heavy development at the moment and some several key features are not yet implemented... ehh hmm, persisting permissions for example... Anyway, don't use it yet. Do contribute though! Accepting pull requests!
This module is under heavy development at the moment and well anyway, you probably shouldn't use it beyond testing it out yet. Do contribute though! Accepting pull requests!

I wrote this module in coffeescript for the main reason of trying coffeescript out. I will most likely re-write a version in javascript at some point as well.

Expand Down Expand Up @@ -64,20 +64,56 @@ am.checkAccess('editPosts', 43) // true
## Other methods

### clearAll

Clears all permissions, you need to call save after to persist changes

### clearAuthAssignments

Clears all auth assignments, you need to call save after to persist changes

### executeBizRule

Business rules not yet implemented

### getAuthAssignment

Gets a Auth assignment object

### getAuthAssignments

gets all auth assignments for a user

### getAuthItem

gets the object that represents an auth item

### getAuthItems

gets all auth items for a user

### hasItemChild

Checks if an auth item has the specified child

### isAssigned

Checks if a user has a certain auth item assigned

### removeAuthItem

Removes an auth item

### removeItemChild

Removes the reference between a parent and child auth item

### revoke

Revokes access for a certain auth item to a user

### save
### saveAuthAssignment
### saveAuthItem

Persists any changes

## Events

Expand Down
4 changes: 2 additions & 2 deletions admittance.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class Admittance extends EventEmitter
assignments[userId][itemName] = null

save: (cb) ->
@adaptor.save @items, @assignments, @children, ->
# @emit "save"
@adaptor.save items, assignments, children, () ->
_this.emit "save"
cb()

# saveAuthAssignment: (assignment, cb) ->
Expand Down
3 changes: 2 additions & 1 deletion admittance.js

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

2 changes: 1 addition & 1 deletion file-adaptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FileAdaptor

data = JSON.stringify json
#when save is done call callback
fs.writeFile @fileName, 'utf8', data, (err, data) ->
fs.writeFile @fileName, data, (err, data) ->
if err then cb err else cb()

# saveAuthItem: (item, oldName, cb) ->
Expand Down
2 changes: 1 addition & 1 deletion file-adaptor.js

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

53 changes: 53 additions & 0 deletions tests/rights-src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"assignments": {
"501": {
"admin": {
"itemName": "admin",
"id": 501,
"bizRule": null,
"data": "N;"
},
"tmc": {
"itemName": "tmc",
"id": 501,
"bizRule": null,
"data": "N;"
}
},
"12": {
"tmc": {
"itemName": "tmc",
"id": 12,
"bizRule": null,
"data": "N;"
}
}
},
"items": {
"admin": {
"name": "admin",
"type": 2,
"description": "Admin user",
"bizRule": null,
"data": "N;"
},
"tmc": {
"name": "tmc",
"type": 2,
"description": "TMC user",
"bizRule": null,
"data": "N;"
},
"acceptTMP": {
"name": "acceptTMP",
"type": 0,
"description": "Accept TMPs",
"bizRule": null,
"data": "N;"
}
},
"children": {
"admin": ["acceptTMP", "tmc"],
"tmc": ["acceptTMP"]
}
}
54 changes: 1 addition & 53 deletions tests/rights.json
Original file line number Diff line number Diff line change
@@ -1,53 +1 @@
{
"assignments": {
"501": {
"admin": {
"itemName": "admin",
"id": 501,
"bizRule": null,
"data": "N;"
},
"tmc": {
"itemName": "tmc",
"id": 501,
"bizRule": null,
"data": "N;"
}
},
"12": {
"tmc": {
"itemName": "tmc",
"id": 12,
"bizRule": null,
"data": "N;"
}
}
},
"items": {
"admin": {
"name": "admin",
"type": 2,
"description": "Admin user",
"bizRule": null,
"data": "N;"
},
"tmc": {
"name": "tmc",
"type": 2,
"description": "TMC user",
"bizRule": null,
"data": "N;"
},
"acceptTMP": {
"name": "acceptTMP",
"type": 0,
"description": "Accept TMPs",
"bizRule": null,
"data": "N;"
}
},
"children": {
"admin": ["acceptTMP", "tmc"],
"tmc": ["acceptTMP"]
}
}
{"items":{"admin":{"name":"admin","type":2,"description":"Admin user","bizRule":null,"data":"N;"},"tmc":{"name":"tmc","type":2,"description":"TMC user","bizRule":null,"data":"N;"},"acceptTMP":{"name":"acceptTMP","type":0,"description":"Accept TMPs","bizRule":null,"data":"N;"},"my-test":{"name":"my-test","type":2,"description":"this is a test"}},"assignments":{"12":{"tmc":{"itemName":"tmc","id":12,"bizRule":null,"data":"N;"}},"501":{"admin":{"itemName":"admin","id":501,"bizRule":null,"data":"N;"},"tmc":{"itemName":"tmc","id":501,"bizRule":null,"data":"N;"}}},"children":{"admin":["acceptTMP","tmc"],"tmc":["acceptTMP"]}}
14 changes: 13 additions & 1 deletion tests/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ chai.should()
AdmittanceModule = require '../index'
Admittance = AdmittanceModule.Admittance
FileAdaptor = AdmittanceModule.FileAdaptor
fs = require 'fs'


am = new Admittance(new FileAdaptor('tests/rights.json'))
Expand Down Expand Up @@ -99,9 +100,11 @@ describe 'The FileAdaptor class', ->

afterEach (done) ->
am.clearAll()
fs.readFile 'tests/rights-src.json', 'utf8', (err, data) ->
fs.writeFile 'tests/rights.json', data, () ->
done()

describe 'load method', (done) ->
describe 'load method', () ->

it 'should load existing rights file', (done) ->
am.on 'load', () ->
Expand All @@ -110,5 +113,14 @@ describe 'The FileAdaptor class', ->
done()

describe 'save method', ->
it 'should save rights to file', (done) ->
am.on 'load', () ->
am.createAuthItem 'my-test', 2, 'this is a test'
am.save () ->
aam = new Admittance(new FileAdaptor('tests/rights.json'))
aam.getAuthItem('my-test').should.be.an('object')
done()




138 changes: 138 additions & 0 deletions tests/test.js

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

0 comments on commit d5559db

Please sign in to comment.