all files / src/storages/ Mongoose.js

75% Statements 117/156
60.92% Branches 53/87
81.08% Functions 30/37
73.28% Lines 96/131
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 302 303          10×                                                               37×       37× 14× 23× 23× 23×       23×                                                                                                                                                                   26×   26× 26×       26×     21×     26×                                                                                     11×   11×   11× 11×       11×       11×         11×       11× 16×     11×       11×         70×                    
'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 _values = require('lodash/values');
 
var _values2 = _interopRequireDefault(_values);
 
var _index = require('./index');
 
var _index2 = _interopRequireDefault(_index);
 
var _Permission = require('../Permission');
 
var _Permission2 = _interopRequireDefault(_Permission);
 
var _Role = require('../Role');
 
var _Role2 = _interopRequireDefault(_Role);
 
var _keymirror = require('keymirror');
 
var _keymirror2 = _interopRequireDefault(_keymirror);
 
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"); } }
 
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
var Type = (0, _keymirror2.default)({
  PERMISSION: null,
  ROLE: null
});
 
function createSchema(Schema) {
  var schema = new Schema({
    name: { type: String, required: true, unique: true },
    type: { type: String, 'enum': (0, _values2.default)(Type), required: true },
    grants: [String]
  });
 
  return schema;
}
 
function getType(item) {
  if (item instanceof _Role2.default) {
    return Type.ROLE;
  } else Eif (item instanceof _Permission2.default) {
    return Type.PERMISSION;
  }
 
  return null;
}
 
function convertToInstance(rbac, record) {
  Iif (!record) {
    throw new Error('Record is undefined');
  }
 
  if (record.type === Type.ROLE) {
    return rbac.createRole(record.name, false, function () {});
  } else Eif (record.type === Type.PERMISSION) {
    var decoded = _Permission2.default.decodeName(record.name);
    Iif (!decoded) {
      throw new Error('Bad permission name');
    }
 
    return rbac.createPermission(decoded.action, decoded.resource, false, function () {});
  }
 
  throw new Error('Type is undefined');
}
 
var MongooseStorage = function (_Storage) {
  _inherits(MongooseStorage, _Storage);
 
  function MongooseStorage() {
    var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
 
    _classCallCheck(this, MongooseStorage);
 
    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(MongooseStorage).call(this));
 
    var connection = options.connection;
    Iif (!connection) {
      throw new Error('Parameter connection is undefined use your current mongoose connection.');
    }
 
    options.modelName = options.modelName || 'rbac';
    options.Schema = options.Schema || connection.Schema;
 
    _this._options = options;
 
    _this._model = connection.model(options.modelName, createSchema(options.Schema));
    return _this;
  }
 
  _createClass(MongooseStorage, [{
    key: 'add',
    value: function add(item, cb) {
      this.model.create({
        name: item.name,
        type: getType(item)
      }, function (err, obj) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (!obj) {
          return cb(new Error('Item is undefined'));
        }
 
        cb(null, item);
      });
 
      return this;
    }
  }, {
    key: 'remove',
    value: function remove(item, cb) {
      var _this2 = this;
 
      var name = item.name;
 
      this.model.update({ grants: name }, {
        $pull: {
          grants: name
        }
      }, { multi: true }, function (err) {
        Iif (err) {
          return cb(err);
        }
 
        _this2.model.remove({ name: name }, function (err2) {
          Iif (err2) {
            return cb(err2);
          }
 
          cb(null, true);
        });
      });
 
      return this;
    }
  }, {
    key: 'grant',
    value: function grant(role, child, cb) {
      var name = role.name;
      var childName = child.name;
 
      Iif (!role instanceof _Role2.default) {
        return cb(new Error('Role is not instance of Role'));
      }
 
      Iif (name === childName) {
        return cb(new Error('You can grant yourself'));
      }
 
      this.model.update({ name: name, type: Type.ROLE }, { $addToSet: { grants: childName } }, function (err) {
        Iif (err) {
          return cb(err);
        }
 
        cb(null, true);
      });
 
      return this;
    }
  }, {
    key: 'revoke',
    value: function revoke(role, child, cb) {
      var name = role.name;
      var childName = child.name;
 
      this.model.update({ name: name, type: Type.ROLE }, { $pull: { grants: childName } }, function (err, num) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (num === 0) {
          return cb(new Error('Item is not associated to this item'));
        }
 
        return cb(null, true);
      });
 
      return this;
    }
  }, {
    key: 'get',
    value: function get(name, cb) {
      var rbac = this.rbac;
 
      this.model.findOne({ name: name }, function (err, record) {
        Iif (err) {
          return cb(err);
        }
 
        if (!record) {
          return cb(null, null);
        }
 
        cb(null, convertToInstance(rbac, record));
      });
 
      return this;
    }
  }, {
    key: 'getRoles',
    value: function getRoles(cb) {
      var rbac = this.rbac;
 
      this.model.find({ type: Type.ROLE }, function (err, records) {
        if (err) {
          return cb(err);
        }
 
        var instances = records.map(function (r) {
          return convertToInstance(rbac, r);
        });
 
        cb(null, instances);
      });
 
      return this;
    }
  }, {
    key: 'getPermissions',
    value: function getPermissions(cb) {
      var rbac = this.rbac;
 
      this.model.find({ type: Type.PERMISSION }, function (err, records) {
        if (err) {
          return cb(err);
        }
 
        var instances = records.map(function (r) {
          return convertToInstance(rbac, r);
        });
 
        cb(null, instances);
      });
 
      return this;
    }
  }, {
    key: 'getGrants',
    value: function getGrants(role, cb) {
      var _this3 = this;
 
      var rbac = this.rbac;
 
      this.model.findOne({ name: role, type: Type.ROLE }, function (err, record) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (!record || !record.grants.length) {
          return cb(null, []);
        }
 
        _this3.model.find({
          name: {
            $in: record.grants
          }
        }, function (err2, records) {
          Iif (err2) {
            return cb(err2);
          }
 
          var instances = records.map(function (r) {
            return convertToInstance(rbac, r);
          });
 
          cb(null, instances);
        });
      });
 
      return this;
    }
  }, {
    key: 'model',
    get: function get() {
      return this._model;
    }
  }, {
    key: 'options',
    get: function get() {
      return this._options;
    }
  }]);
 
  return MongooseStorage;
}(_index2.default);
 
exports.default = MongooseStorage;