all files / src/storages/ index.js

77.5% Statements 62/80
70% Branches 28/40
70.37% Functions 19/27
74.6% Lines 47/63
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301          14×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               37×                    
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _Permission = require('../Permission');
 
var _Permission2 = _interopRequireDefault(_Permission);
 
var _Role = require('../Role');
 
var _Role2 = _interopRequireDefault(_Role);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var Storage = function () {
  /**
   * Storage constructor
   * @constructor Storage
   */
 
  function Storage() {
    _classCallCheck(this, Storage);
 
    this._rbac = null;
  }
 
  /**
   * Get instance of RBAC
   * @member Storage#rbac {RBAC|null} Instance of RBAC
   */
 
 
  _createClass(Storage, [{
    key: 'add',
 
 
    /**
     * Add permission or role
     * @method Storage#add
     * @param {Base}   item    Instance of role or permission
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
    value: function add(item, cb) {
      cb(new Error('Storage method add is not implemented'));
    }
 
    /**
     * Remove permission or role
     * @method Storage#remove
     * @param {Base}   item    Instance of role or permission
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'remove',
    value: function remove(item, cb) {
      cb(new Error('Storage method remove is not implemented'));
    }
 
    /**
     * Add (grant) permission or role to hierarchy of actual role
     * @method Storage#grant
     * @param  {Role}   role  Instance of role
     * @param  {Base}   child Instance of role or permission
     * @param  {Function} cb    Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'grant',
    value: function grant(role, child, cb) {
      cb(new Error('Storage method grant is not implemented'));
    }
 
    /**
     * Remove (revoke) permission or role from hierarchy of actual role
     * @method Storage#revoke
     * @param  {Role}   role  Instance of role
     * @param  {Base}   child Instance of role or permission
     * @param  {Function} cb    Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'revoke',
    value: function revoke(role, child, cb) {
      cb(new Error('Storage method revoke is not implemented'));
    }
 
    /**
     * Get instance of permission or role by his name
     * @method Storage#get
     * @param  {String}   name Name of role or permission
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'get',
    value: function get(name, cb) {
      cb(new Error('Storage method get is not implemented'));
    }
 
    /**
     * Get all instances of Roles
     * @method Storage#getRoles
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'getRoles',
    value: function getRoles(cb) {
      cb(new Error('Storage method getRoles is not implemented'));
    }
 
    /**
     * Get all instances of Permissions
     * @method Storage#getPermissions
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'getPermissions',
    value: function getPermissions(cb) {
      cb(new Error('Storage method getPermissions is not implemented'));
    }
 
    /**
     * Get instances of Roles and Permissions assigned to role
     * @method Storage#getGrants
     * @param  {String}   role Name of role
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'getGrants',
    value: function getGrants(role, cb) {
      cb(new Error('Storage method getGrants is not implemented'));
    }
 
    /**
     * Get instance of role by his name
     * @method Storage#getRole
     * @param  {String}   name Name of role
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'getRole',
    value: function getRole(name, cb) {
      this.get(name, function (err, item) {
        Iif (err || !item) {
          return cb(err, item);
        }
 
        if (item instanceof _Role2.default) {
          return cb(null, item);
        }
 
        cb(null, null);
      });
 
      return this;
    }
 
    /**
     * Get instance of permission by his name
     * @method Storage#getPermission
     * @param  {String}   action   Name of action
     * @param  {String}   resource Name of resource
     * @param  {Function} cb       Callback function
     * @return {Storage}           Instance of actual storage
     */
 
  }, {
    key: 'getPermission',
    value: function getPermission(action, resource, cb) {
      var name = _Permission2.default.createName(action, resource);
 
      this.get(name, function (err, item) {
        if (err || !item) {
          return cb(err, item);
        }
 
        Eif (item instanceof _Permission2.default) {
          return cb(null, item);
        }
 
        cb(null, null);
      });
 
      return this;
    }
 
    /**
     * Return true with callback if role or permission exists
     * @method Storage#exists
     * @param  {String}   name Name of role or permission
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'exists',
    value: function exists(name, cb) {
      this.get(name, function (err, item) {
        Iif (err) {
          return cb(err);
        }
 
        if (!item) {
          return cb(null, false);
        }
 
        return cb(null, true);
      });
 
      return this;
    }
 
    /**
     * Return true with callback if role exists
     * @method Storage#existsRole
     * @param  {String}   name Name of role
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'existsRole',
    value: function existsRole(name, cb) {
      this.getRole(name, function (err, item) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (!item) {
          return cb(null, false);
        }
 
        return cb(null, true);
      });
 
      return this;
    }
 
    /**
     * Return true with callback if permission exists
     * @method Storage#existsPermission
     * @param  {String}   name Name of permission
     * @param  {Function} cb   Callback function
     * @return {Storage}       Instance of actual storage
     */
 
  }, {
    key: 'existsPermission',
    value: function existsPermission(action, resource, cb) {
      this.getPermission(action, resource, function (err, item) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (!item) {
          return cb(null, false);
        }
 
        return cb(null, true);
      });
 
      return this;
    }
  }, {
    key: 'rbac',
    get: function get() {
      return this._rbac;
    },
    set: function set(rbac) {
      Iif (this._rbac) {
        throw new Error('RBAC is already setted');
      }
 
      this._rbac = rbac;
    }
  }]);
 
  return Storage;
}();
 
exports.default = Storage;