Last active
August 27, 2016 16:32
-
-
Save roboncode/b8c22b636d04ba754d2559e155a8ac48 to your computer and use it in GitHub Desktop.
Permissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rules = Permissions.createRules(['view', 'modify', 'create', 'delete']); | |
var permissions = Permissions.createPermissions([rules.modify, rules.delete]); | |
console.log('#view', Permissions.checkForPermission(permissions, rules.view)); // false | |
console.log('#modify', Permissions.checkForPermission(permissions, rules.modify)); // true | |
console.log('#create', Permissions.checkForPermission(permissions, rules.create)); // false | |
console.log('#delete', Permissions.checkForPermission(permissions, rules.delete)); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Permissions() { | |
} | |
Permissions.createRules = function(options) { | |
var permissions = {}; | |
var num = 1; | |
var len = options.length; | |
for(var i=0;i<len;i++) { | |
if(i === 0) { | |
permissions[options[i]] = num; | |
} else { | |
num *= 2; | |
permissions[options[i]] = num; | |
} | |
} | |
return permissions; | |
} | |
Permissions.createPermissions = function (rules) { | |
var result = 0; | |
var len = rules.length; | |
for(var i=0;i<len;i++) { | |
result ^= rules[i]; | |
} | |
return result; | |
} | |
Permissions.checkForPermission = function(permissions, rule) { | |
return Boolean(permissions & rule); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment