Permalink
Browse files

added update() method for easy handling of ctxs

- tabified the code
  • Loading branch information...
1 parent 3b06bb1 commit 5334c4d9dd65ddabf214dea86a2e86ec7a1ac85a Christopher Klein committed Mar 18, 2013
Showing with 138 additions and 73 deletions.
  1. +3 −0 Readme.md
  2. +109 −71 jquery.inherited-permissions.js
  3. +1 −1 tests/index.html
  4. +25 −1 tests/tests.js
View
@@ -24,6 +24,9 @@ The usage is very simple:
// current context will be set to root context again
if (perms.currentContext().permissions[0] == "READ") { alert("OK!") }
+
+ // create a new context under context "A" and switch to new context
+ perms.update(["PERM_1", "PERM_2"], "B", "A")
License
=======
@@ -4,22 +4,22 @@
* @license BSD license
*/
jQuery.inheritedPermissions = function () {
- var self = this
+ var self = this
// top context is "root"
- self.contextHierarchy = {
- root: {
- permissions: [],
- childs: {},
- parent: null
- }
- }
-
- self.contexts = {
- "root": self.contextHierarchy.root
- }
-
- self._currentContext = self.contexts["root"];
+ self.contextHierarchy = {
+ root: {
+ permissions: [],
+ childs: {},
+ parent: null
+ }
+ }
+
+ self.contexts = {
+ "root": self.contextHierarchy.root
+ }
+
+ self._currentContext = self.contexts["root"];
self._currentContextName = "root";
/**
@@ -28,107 +28,145 @@ jQuery.inheritedPermissions = function () {
* @param string optional; context to check, if not defined: current context
* @returns true if permission is in context available.
*/
- self.available = function (permission, context) {
+ self.available = function (permission, context) {
var allPerms = self.all(context)
- var r = jQuery.inArray(permission, allPerms)
+ var r = jQuery.inArray(permission, allPerms)
return r !== -1;
- };
+ };
/**
* Returns all permissions
* @param string optional; name of context; if not given, current context is chosen
* @return array of strings
*/
- self.all = function (context) {
- var _context = (typeof context === "undefined") ? self.currentContext() : self.get(context)
+ self.all = function (context) {
+ var _context = (typeof context === "undefined") ? self.currentContext() : self.get(context)
- var perms = []
+ var perms = []
+
+ do {
+ $.merge(perms, _context.permissions)
+ _context = _context.parent
+ } while (_context !== null)
+
+ return perms
+ }
- do {
- $.merge(perms, _context.permissions)
- _context = _context.parent
- } while (_context !== null)
+ /**
+ * Updates an existing context or creates a new context
+ * @param string permissions for this context; any existing permissions in this context are revoked
+ * @param string optional name of context to be updated or added
+ * @param string optional parent context name; if the context already exists, this parameter has no effect
+ */
+ self.update = function(permissions, contextName, parentContext) {
+ var contextName = (typeof contextName === "undefined") ? _currentContextName : contextName
- return perms
- }
+ if (self.exists(contextName)) {
+ self.clear(contextName)
+ self.grant(permissions, contextName)
+ }
+ else {
+ self.addContext(contextName, permissions, parentContext)
+ }
+
+ self.setCurrentContext(contextName)
+ }
+
+ /**
+ * Removes every permission from the given context
+ * @param string optional name of context or current context
+ */
+ self.clear = function(contextName) {
+ var _context = (!contextName) ? self.currentContext() : self.get(contextName)
+ _context.permissions = []
+ }
/**
* Grants permission
* @param string|[array of strings] permission name(s)
* @param string optional; name of context to check or optional
*/
- self.grant = function (permissions, contextName) {
- var _context = (!contextName) ? self.currentContext() : self.get(contextName)
- var _permissions = permissions
+ self.grant = function (permissions, contextName) {
+ var _context = (!contextName) ? self.currentContext() : self.get(contextName)
+ var _permissions = permissions
- if (!jQuery.isArray(permissions)) {
- _permissions = [permissions]
- }
+ if (!jQuery.isArray(permissions)) {
+ _permissions = [permissions]
+ }
- jQuery.merge(_context.permissions, _permissions)
- };
+ jQuery.merge(_context.permissions, _permissions)
+ }
/**
* Revoke permission
* @param string|[array of strings] permission name(s)
* @param string optional; name of context which contains the permission; if missing the current context is used
*/
- self.revoke = function (permissions, contextName) {
- var _context = (!contextName) ? self.currentContext() : self.get(contextName)
- var _permissions = permissions
+ self.revoke = function (permissions, contextName) {
+ var _context = (!contextName) ? self.currentContext() : self.get(contextName)
+ var _permissions = permissions
- if (!jQuery.isArray(permissions)) {
- _permissions = [permissions]
- }
+ if (!jQuery.isArray(permissions)) {
+ _permissions = [permissions]
+ }
- var _newPerms = jQuery.grep(_context.permissions, function (key) {
- return jQuery.inArray(_permissions, key) !== -1
- });
+ var _newPerms = jQuery.grep(_context.permissions, function (key) {
+ return jQuery.inArray(_permissions, key) !== -1
+ });
- _context.permissions = _newPerms
- };
+ _context.permissions = _newPerms
+ }
/**
* Returns the given context
* @param string name of context
* @throws exception, of context name does not exist
* @return object
*/
- self.get = function (contextName) {
- if (!self.contexts[contextName]) {
- throw "Context " + contextName + " does not exist"
- }
+ self.get = function (contextName) {
+ if (!self.exists(contextName)) {
+ throw "Context " + contextName + " does not exist"
+ }
- return self.contexts[contextName]
- }
+ return self.contexts[contextName]
+ }
+
+ /**
+ * Returns whether the context name exists or not
+ * @param string name of context to check
+ * @return boolean
+ */
+ self.exists = function(contextName) {
+ return (typeof self.contexts[contextName] !== "undefined")
+ }
/**
* Returns the current context
* @return object
*/
- self.currentContext = function () {
- return self._currentContext;
- }
+ self.currentContext = function () {
+ return self._currentContext;
+ }
/**
* Removes the given context; if current context is somewhere inside the deleted hierarchy of the context, it will set to the top context
* @param string name of context to delete
* @throws exception if root context should be removed
*/
- self.removeContext = function (contextName, parentName /** internal, must be provided for recursive deletion */) {
- if (contextName == "root") {
- throw "Root context can not be removed"
- }
+ self.removeContext = function (contextName, parentName /** internal, must be provided for recursive deletion */) {
+ if (contextName == "root") {
+ throw "Root context can not be removed"
+ }
- var _context = self.get(contextName)
+ var _context = self.get(contextName)
- // remove all child elements recursively
- for (var _contextName in _context.childs) {
- self.removeContext(_contextName, contextName)
- }
+ // remove all child elements recursively
+ for (var _contextName in _context.childs) {
+ self.removeContext(_contextName, contextName)
+ }
- delete _context.childs[contextName]
+ delete _context.childs[contextName]
delete self.contexts[contextName]
// delete parent
@@ -155,7 +193,7 @@ jQuery.inheritedPermissions = function () {
* @param string|array permission(s) for this context
* @param string optional; parent context. If none is specified, this results in more than one root context
*/
- self.addContext = function (name, permissions, parent) {
+ self.addContext = function (name, permissions, parent) {
var _parent = null
if (typeof parent !== "undefined") {
@@ -166,13 +204,13 @@ jQuery.inheritedPermissions = function () {
_parent = self.get(parent)
}
- var _permissions = permissions || []
+ var _permissions = permissions || []
if (!jQuery.isArray(permissions)) {
_permissions = [permissions];
}
- self.contexts[name] = {
+ self.contexts[name] = {
permissions: _permissions,
childs: {},
parent: _parent
@@ -181,16 +219,16 @@ jQuery.inheritedPermissions = function () {
if (_parent !== null) {
_parent.childs[name] = self.contexts[name]
}
- }
+ }
/**
* Sets the the pointer for the current context
* @param string name of context
*/
- self.setCurrentContext = function (name) {
- var _context = self.get(name)
+ self.setCurrentContext = function (name) {
+ var _context = self.get(name)
- self._currentContext = _context
+ self._currentContext = _context
self._currentContextName = name
}
View
@@ -10,7 +10,7 @@
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
- <script src="../jquery.auth.js"></script>
+ <script src="../jquery.inherited-permissions.js"></script>
<script src="tests.js"></script>
</body>
</html>
View
@@ -6,7 +6,7 @@ test("default context is root", function() {
})
test("get context by name", function() {
- var plugin = $.inheritedPermissions()
+ var plugin = new $.inheritedPermissions()
ok(plugin.get("root") !== null)
ok(plugin.get("root").permissions.length == 0)
})
@@ -152,3 +152,27 @@ test("assert that current context will be resetted to root if deleted context is
ok(r.permissions[0] == "ROOT_PERM")
})
+
+test("on update, the current context must be reset and every permission in an existent context must be resetted", function() {
+ var plugin = $.inheritedPermissions()
+
+ plugin.addContext("sub_kontext", ["PERM1", "PERM2"])
+ plugin.update(["PERM3"], "sub_kontext")
+
+ var r = plugin.currentContext()
+
+ ok(r.permissions.length == 1)
+ ok(r.permissions[0] == "PERM3")
+})
+
+test("on update, a new context must be correctly added", function() {
+ var plugin = $.inheritedPermissions()
+
+ plugin.addContext("ctx_a", ["PERM1"])
+ plugin.update(["PERM2"], "ctx_b", "ctx_a")
+
+ var r = plugin.currentContext()
+ ok(r.permissions[0] == "PERM2")
+ ok(plugin.available("PERM1"))
+ ok(plugin.available("PERM2"))
+})

0 comments on commit 5334c4d

Please sign in to comment.