Add Permission.js
authorMatthias Schmidt <gravatronics@live.com>
Wed, 23 Sep 2015 15:49:18 +0000 (17:49 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 23 Sep 2015 15:49:18 +0000 (17:49 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Permission.js [new file with mode: 0644]

diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Permission.js b/wcfsetup/install/files/js/WoltLab/WCF/Permission.js
new file mode 100644 (file)
index 0000000..7ac2227
--- /dev/null
@@ -0,0 +1,63 @@
+/**
+ * Manages user permissions.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/Permission
+ */
+define(['Dictionary'], function(Dictionary) {
+       "use strict";
+       
+       var _permissions = new Dictionary();
+       
+       /**
+        * @exports     WoltLab/WCF/Permission
+        */
+       var Permission = {
+               /**
+                * Adds a single permission to the store.
+                * 
+                * @param       {string}        permission      permission name
+                * @param       {boolean}       value           permission value
+                */
+               add: function(permission, value) {
+                       if (typeof value !== "boolean") {
+                               throw new TypeError("Permission value has to be boolean.");
+                       }
+                       
+                       _permissions.set(permission, value);
+               },
+               
+               /**
+                * Adds all the permissions in the given object to the store.
+                * 
+                * @param       {Object.<string, boolean>}      object          permission list
+                */
+               addObject: function(object) {
+                       for (var key in object) {
+                               if (object.hasOwnProperty(key)) {
+                                       this.addPermission(key, object[key]);
+                               }
+                       }
+               },
+               
+               /**
+                * Returns the value of a permission.
+                * 
+                * If the permission is unknown, false is returned.
+                * 
+                * @param       {string}        permission      permission name
+                * @return      {boolean}       permission value
+                */
+               get: function(permission) {
+                       if (_permissions.has(permission)) {
+                               return _permissions.get(permission);
+                       }
+                       
+                       return false;
+               }
+       };
+       
+       return Permission;
+});