1 //require <jquery.packed.js> 2 //require <xatajax.core.js> 3 (function(){ 4 5 var $ = jQuery; 6 var xataface = XataJax.load('xataface'); 7 xataface.Permissions = Permissions; 8 9 10 11 /** 12 * @class 13 * @name Permissions 14 * @memberOf xataface 15 * 16 * @description Class to help obtain permissions information about a specific record. 17 * @param {Object} o The config parameters. 18 * @param {Object} o.query The query parameters following Xataface URL conventions. 19 * @example 20 * //require <xataface/Permissions.js> 21 * var Permissions = XataJax.load('xataface.Permissions'); 22 * var myperms = new Permissions({ 23 * query: { 24 * '-table': 'users', 25 * 'user_id': 10 26 * } 27 * }); 28 * myperms.ready(function(){ 29 * // We can't call any thing until the permissions are ready 30 * if ( myperms.checkPermission('view') ){ 31 * alert('We have view permission'); 32 * } 33 * 34 * }); 35 */ 36 function Permissions(/**Object*/ o){ 37 this.query = null; 38 this.permissions = null; 39 if ( typeof(o) == 'undefined' ) o = {}; 40 $.extend(this, o); 41 42 43 44 45 46 } 47 48 49 $.extend(Permissions.prototype, { 50 51 load: load, 52 ready: ready, 53 checkPermission: checkPermission, 54 getPermissions: getPermissions, 55 setQuery: setQuery, 56 getQuery: getQuery 57 58 }); 59 60 61 /** 62 * @function 63 * @name load 64 * @memberOf xataface.Permissions# 65 * 66 * @description Loads the permissions for the current query from the server. 67 * @param {Function} callback Callback function called when loading is complete. 68 * This function is run in the context of this Permissions object. 69 * @see xataface.Permissions#ready 70 */ 71 function load(callback){ 72 if ( !this.query ){ 73 throw "No query provided for permissions"; 74 75 } 76 77 this.query['-action'] = 'ajax_get_permissions'; 78 79 if ( !callback ) callback = function(){}; 80 var self = this; 81 $.get(DATAFACE_SITE_HREF, this.query, function(res){ 82 self.permissions = res; 83 84 callback.call(self); 85 }); 86 87 } 88 89 90 /** 91 * @function 92 * @name ready 93 * @memberOf xataface.Permissions# 94 * 95 * @description Runs a callback function after the permissions object is ready. All methods 96 * for checking permissions should be run inside this object. 97 * 98 */ 99 function ready(callback){ 100 101 if ( typeof(callback) == 'undefined' ) callback = function(){}; 102 103 if ( this.permissions != null ){ 104 callback.call(this); 105 106 } else { 107 this.load(callback); 108 } 109 } 110 111 112 /** 113 * @function 114 * @name checkPermission 115 * @memberOf xataface.Permissions# 116 * @description Checks to see if the given permission is granted. This method 117 * should be run after the Permissions object is ready. 118 * @see xataface.Permissions#ready 119 * 120 * @param {String} perm The permission to check. 121 * @returns {Boolean} True if the permission is granted. False otherwise. 122 * 123 */ 124 function checkPermission(perm){ 125 if ( this.permissions != null ){ 126 return this.permissions[perm] ? true:false; 127 } 128 return false; 129 } 130 131 /** 132 * @function 133 * @name getPermissions 134 * @memberOf xataface.Permissions# 135 * @description Returns an associative array of granted permissions. 136 * This should only be run after the Permissions object is ready. 137 * @see xataface.Permissions#ready 138 * @returns {Object} 139 */ 140 function getPermissions(){ 141 return this.permissions; 142 } 143 144 145 /** 146 * @function 147 * @name setQuery 148 * @memberOf xataface.Permissions# 149 * @description Sets the query to be used. This will clear out the current 150 * permissions cache and cause the object to need to reload the permissions. 151 * @returns {void} 152 * @param {Object} q The query. 153 * @see xataface.Permissions#getQuery 154 */ 155 function setQuery(q){ 156 this.query = q; 157 this.permissions = null; 158 159 } 160 161 /** 162 * @function 163 * @name getQuery 164 * @memberOf xataface.Permissions# 165 * @description Gets the query that was used to get these permissions. 166 * @returns {Object} 167 * @see xataface.Permissions#setQuery 168 */ 169 function getQuery(){ 170 return this.query; 171 } 172 173 174 175 })();