Skip to content

Commit

Permalink
Merge pull request #10 from digitalsadhu/refactor/multple_admittance_…
Browse files Browse the repository at this point in the history
…instances#5

Refactor to allow multiple instances of admittance
  • Loading branch information
digitalsadhu committed Mar 22, 2014
2 parents 483ee56 + 264bbe9 commit b108950
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 157 deletions.
85 changes: 27 additions & 58 deletions admittance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,75 @@

var util = require('util')

var permissions = {}
, assignments = {}

var getDirectChildren = function (parent) {

var getDirectChildren = function (permissions, parent) {
var perm = permissions[parent]

if (typeof perm === 'undefined' || perm === null) perm = []

if (!util.isArray(perm)) perm = [perm]

return perm
}

var getUserPermissions = function (userid) {

var getUserPermissions = function (assignments, userid) {
var assigns = assignments[userid]

if (typeof assigns === 'undefined' || assigns === null) assigns = []

if (!util.isArray(assigns)) assigns = [assigns]

return assigns

}

var getDirectPermissionChildren = function (parentPermission) {

return getDirectChildren(parentPermission)

var getDirectPermissionChildren = function (permissions, parentPermission) {
return getDirectChildren(permissions, parentPermission)
}

var getAllChildren = function (parentPermission) {

var directChildren = getDirectPermissionChildren(parentPermission)

var getAllChildren = function (permissions, parentPermission) {
var directChildren = getDirectPermissionChildren(permissions, parentPermission)
if (directChildren.length === 0) return []

var childrensChildren = directChildren.reduce(
function (prevDirectChild, currDirectChild) {
return prevDirectChild.concat(getAllChildren(currDirectChild))
return prevDirectChild.concat(getAllChildren(permissions, currDirectChild))
},
[]
)

return directChildren.concat(childrensChildren)

}

var checkIsParent = function (parentPermission, childPermission) {

var checkIsParent = function (permissions, parentPermission, childPermission) {
if (parentPermission === childPermission)
return false

var children = getAllChildren(parentPermission)

var children = getAllChildren(permissions, parentPermission)
for (var i = 0; i < children.length; i++) {
if (children[i] === childPermission)
return true
}

return false
}

var checkAccess = function (userid, permission) {

var userPermissions = getUserPermissions(userid)

var checkAccess = function (permissions, assignments, userid, permission) {
var userPermissions = getUserPermissions(assignments, userid)
for (var i = 0; i < userPermissions.length; i++) {

if (userPermissions[i] === permission)
return true

if (checkIsParent(userPermissions[i], permission))
if (checkIsParent(permissions, userPermissions[i], permission))
return true
}

return false
}

var admittance = function (userid) {
return {
is: function (permission) {
return checkAccess(userid, permission)
},
isnt: function (permission) {
return !checkAccess(userid, permission)
},
can: function (permission) {
return checkAccess(userid, permission)
},
cant: function (permission) {
return !checkAccess(userid, permission)
var admittance = function (permissions, assignments) {
return function (userid) {
return {
is: function (permission) {
return checkAccess(permissions, assignments, userid, permission)
},
isnt: function (permission) {
return !checkAccess(permissions, assignments, userid, permission)
},
can: function (permission) {
return checkAccess(permissions, assignments, userid, permission)
},
cant: function (permission) {
return !checkAccess(permissions, assignments, userid, permission)
}
}
}
}

admittance.load = function (permissionData, assignmentData) {
assignments = assignmentData
permissions = permissionData
}

module.exports = admittance
Loading

0 comments on commit b108950

Please sign in to comment.