all files / src/ Role.js

77.78% Statements 49/63
57.14% Branches 20/35
81.25% Functions 13/16
73.68% Lines 28/38
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                      38×   38×                           38×   38×         38×           38×                                                                                     12× 12×                                                                                                                        
'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; Eif ("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 _Base2 = require('./Base');
 
var _Base3 = _interopRequireDefault(_Base2);
 
var _Permission = require('./Permission');
 
var _Permission2 = _interopRequireDefault(_Permission);
 
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 Role = function (_Base) {
  _inherits(Role, _Base);
 
  /**
   * Role constructor
   * @constructor Role
   * @extends {Base}
   * @param  {RBAC}     rbac       Instance of the RBAC
   * @param  {String}   name       Name of the role
   * @param  {Boolean}  [add=true] True if you need to save it to storage
   * @param  {Function} cb         Callback function after add
   */
 
  function Role(rbac, name, add, cb) {
    _classCallCheck(this, Role);
 
    Iif (typeof add === 'function') {
      cb = add;
      add = true;
    }
 
    Iif (!_Permission2.default.isValidName(name)) {
      var _ret;
 
      return _ret = cb(new Error('Role has no valid name')), _possibleConstructorReturn(_this, _ret);
    }
 
    return _possibleConstructorReturn(this, Object.getPrototypeOf(Role).call(this, rbac, name, add, cb));
  }
 
  /**
   * Add role or permission to current role
   * @method Role#grant
   * @param  {Role|Permission} item Instance of role or permission
   * @param  {Function} cb          Callback function
   * @return {Role}                 Return current instance of role
   */
 
 
  _createClass(Role, [{
    key: 'grant',
    value: function grant(item, cb) {
      this.rbac.grant(this, item, cb);
      return this;
    }
 
    /**
     * Remove role or permission from current role
     * @method Role#revoke
     * @param  {Role|Permission} item Instance of role or permission
     * @param  {Function} cb          Callback function
     * @return {Role}                 Return current instance of role
     */
 
  }, {
    key: 'revoke',
    value: function revoke(item, cb) {
      this.rbac.revoke(this, item, cb);
      return this;
    }
 
    /**
     * Return true if contains permission
     * @method Role#can
     * @param  {String}  action   Name of action
     * @param  {String}  resource Name of resource
     * @param  {Function} cb      Callback function
     * @return {Role}             Return current instance of role
     */
 
  }, {
    key: 'can',
    value: function can(action, resource, cb) {
      this.rbac.can(this.name, action, resource, cb);
      return this;
    }
 
    /**
     * Check if the role has any of the given permissions
     * @method Role#canAny
     * @param  {Array} permissions  List of permissions. Each has structure (String action, String resource)
     * @param  {Function} cb      Callback function
     * @return {Role}               Return current instance of role
     */
 
  }, {
    key: 'canAny',
    value: function canAny(permissions, cb) {
      this.rbac.canAny(this.name, permissions, cb);
      return this;
    }
 
    /**
     * Check if the model has all of the given permissions
     * @method Role#canAll
     * @param  {Array}  permissions List of permissions. Each has structure (String action, String resource)
     * @param  {Function} cb      Callback function
     * @return {Role}               Return current instance of role
     */
 
  }, {
    key: 'canAll',
    value: function canAll(permissions, cb) {
      this.rbac.canAll(this.name, permissions, cb);
      return this;
    }
 
    /**
     * Return true if the current role contains the specified role name
     * @method Role#hasRole
     * @param  {String} roleChildName Name of role
     * @param  {Function} cb          Callback function
     * @return {Role}                 Return current instance of role
     */
 
  }, {
    key: 'hasRole',
    value: function hasRole(roleChildName, cb) {
      this.rbac.hasRole(this.name, roleChildName, cb);
      return this;
    }
 
    /**
     * Return array of permission assigned to actual role
     * @method Role#getScope
     * @param  {Function} cb  Callback function
     * @return {Role}         Return current instance of role
     */
 
  }, {
    key: 'getScope',
    value: function getScope(cb) {
      this.rbac.getScope(this.name, cb);
      return this;
    }
  }]);
 
  return Role;
}(_Base3.default);
 
exports.default = Role;