all files / src/ RBAC.js

82.8% Statements 207/250
73.83% Branches 110/149
92.54% Functions 62/67
81.97% Lines 191/233
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865          33×                                                                                                           48×       48×       48× 48×                           84× 84×                                                                                                             40×       40×       40×       40× 40×                                                                                                                       34×   34×   34×     34×     34×       34×     34×                           38×                               47×                                                                                                                                                                                                                                                           16×   16×             16× 24× 24× 24×                                     16×   16×       24× 24×                                           16× 32× 32×                                       10×   10×                                                                 32×   32× 32×   32× 32×   32× 32×   32×       32× 51× 51×   51×     51× 12×       20× 12×         32×                               12×   21×         21×     17×         12×                                                                                               12×         12×     10×                                                                                                                         28×         28×     22× 18×                                   234×                     234×           18× 18×             51×         91×          
'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); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _isPlainObject = require('lodash/isPlainObject');
 
var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
 
var _async = require('async');
 
var _Role = require('./Role');
 
var _Role2 = _interopRequireDefault(_Role);
 
var _Permission = require('./Permission');
 
var _Permission2 = _interopRequireDefault(_Permission);
 
var _Memory = require('./storages/Memory');
 
var _Memory2 = _interopRequireDefault(_Memory);
 
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 RBAC = function () {
  /**
   * RBAC constructor
   * @constructor RBAC
   * @param  {Object} options             Options for RBAC
   * @param  {Storage}  [options.storage]  Storage of grants
   * @param  {Array}    [options.roles]            List of role names (String)
   * @param  {Object}   [options.permissions]      List of permissions
   * @param  {Object}   [options.grants]           List of grants
   * @param  {Function} [callback]         Callback function
   */
 
  function RBAC() {
    var _this = this;
 
    var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
    var callback = arguments.length <= 1 || arguments[1] === undefined ? function () {} : arguments[1];
 
    _classCallCheck(this, RBAC);
 
    options.storage = options.storage || new _Memory2.default();
 
    this._options = options;
 
    this.storage.rbac = this;
 
    var permissions = options.permissions || {};
    var roles = options.roles || [];
    var grants = options.grants || {};
 
    this.create(roles, permissions, grants, function (err) {
      Iif (err) {
        return callback(err);
      }
 
      return callback(null, _this);
    });
  }
 
  /**
   * The RBAC's options.
   * @member RBAC#options {Object}
   */
 
 
  _createClass(RBAC, [{
    key: 'add',
 
 
    /**
     * Register role or permission to actual RBAC instance
     * @method RBAC#add
     * @param  {Role|Permission}     item Instance of Base
     * @param  {Function} cb   Callback function
     * @return {RBAC}          Return actual instance
     */
    value: function add(item, cb) {
      Iif (!item) {
        return cb(new Error('Item is undefined'));
      }
 
      Iif (item.rbac !== this) {
        return cb(new Error('Item is associated to another RBAC instance'));
      }
 
      this.storage.add(item, cb);
      return this;
    }
 
    /**
     * Get instance of Role or Permission by his name
     * @method RBAC#get
     * @param  {String}   name  Name of item
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'get',
    value: function get(name, cb) {
      this.storage.get(name, cb);
      return this;
    }
 
    /**
     * Remove role or permission from RBAC
     * @method RBAC#remove
     * @param  {Role|Permission} item Instance of role or permission
     * @param  {Function}        cb   Callback function
     * @return {RBAC}                 Current instance
     */
 
  }, {
    key: 'remove',
    value: function remove(item, cb) {
      Iif (!item) {
        return cb(new Error('Item is undefined'));
      }
 
      Iif (item.rbac !== this) {
        return cb(new Error('Item is associated to another RBAC instance'));
      }
 
      this.storage.remove(item, cb);
      return this;
    }
 
    /**
     * Remove role or permission from RBAC
     * @method RBAC#removeByName
     * @param  {String}   name Name of role or permission
     * @param  {Function} cb   Callback function
     * @return {RBAC}          Current instance
     */
 
  }, {
    key: 'removeByName',
    value: function removeByName(name, cb) {
      this.get(name, function (err, item) {
        Iif (err) {
          return cb(err);
        }
 
        Iif (!item) {
          return cb(null, false);
        }
 
        item.remove(cb);
      });
 
      return this;
    }
 
    /**
     * Grant permission or role to the role
     * @method RBAC#grant
     * @param  {Role}            role  Instance of the role
     * @param  {Role|Permission} child Instance of the role or permission
     * @param  {Function}        cb    Callback function
     * @return {RBAC}                  Current instance
     */
 
  }, {
    key: 'grant',
    value: function grant(role, child, cb) {
      Iif (!role || !child) {
        return cb(new Error('One of item is undefined'));
      }
 
      Iif (role.rbac !== this || child.rbac !== this) {
        return cb(new Error('Item is associated to another RBAC instance'));
      }
 
      Iif (!RBAC.isRole(role)) {
        return cb(new Error('Role is not instance of Role'));
      }
 
      this.storage.grant(role, child, cb);
      return this;
    }
 
    /**
     * Revoke permission or role from the role
     * @method RBAC#revoke
     * @param  {Role}            role   Instance of the role
     * @param  {Role|Permission} child  Instance of the role or permission
     * @param  {Function}        cb     Callback function
     * @return {RBAC}                   Current instance
     */
 
  }, {
    key: 'revoke',
    value: function revoke(role, child, cb) {
      Iif (!role || !child) {
        return cb(new Error('One of item is undefined'));
      }
 
      Iif (role.rbac !== this || child.rbac !== this) {
        return cb(new Error('Item is associated to another RBAC instance'));
      }
 
      this.storage.revoke(role, child, cb);
      return this;
    }
 
    /**
     * Revoke permission or role from the role by names
     * @method RBAC#revokeByName
     * @param  {String}   roleName  Instance of the role
     * @param  {String}   childName Instance of the role or permission
     * @param  {Function} cb        Callback function
     * @return {RBAC}               Current instance
     */
 
  }, {
    key: 'revokeByName',
    value: function revokeByName(roleName, childName, cb) {
      var _this2 = this;
 
      (0, _async.parallel)({
        role: function role(callback) {
          return _this2.get(roleName, callback);
        },
        child: function child(callback) {
          return _this2.get(childName, callback);
        }
      }, function (err, results) {
        Iif (err) {
          return cb(err);
        }
 
        _this2.revoke(results.role, results.child, cb);
      });
 
      return this;
    }
 
    /**
     * Grant permission or role from the role by names
     * @method RBAC#grantByName
     * @param  {String}   roleName  Instance of the role
     * @param  {String}   childName Instance of the role or permission
     * @param  {Function} cb        Callback function
     * @return {RBAC}               Current instance
     */
 
  }, {
    key: 'grantByName',
    value: function grantByName(roleName, childName, cb) {
      var _this3 = this;
 
      (0, _async.parallel)({
        role: function role(callback) {
          return _this3.get(roleName, callback);
        },
        child: function child(callback) {
          return _this3.get(childName, callback);
        }
      }, function (err, results) {
        Iif (err) {
          return cb(err);
        }
 
        _this3.grant(results.role, results.child, cb);
      });
 
      return this;
    }
 
    /**
     * Create a new role assigned to actual instance of RBAC
     * @method RBAC#createRole
     * @param  {String}  roleName Name of new Role
     * @param  {Boolean} [add=true]    True if you need to add it to the storage
     * @return {Role}    Instance of the Role
     */
 
  }, {
    key: 'createRole',
    value: function createRole(roleName, add, cb) {
      return new _Role2.default(this, roleName, add, cb);
    }
 
    /**
     * Create a new permission assigned to actual instance of RBAC
     * @method RBAC#createPermission
     * @param  {String} action   Name of action
     * @param  {String} resource Name of resource
     * @param  {Boolean} [add=true]   True if you need to add it to the storage
     * @param  {Function} cb     Callback function
     * @return {Permission}      Instance of the Permission
     */
 
  }, {
    key: 'createPermission',
    value: function createPermission(action, resource, add, cb) {
      return new _Permission2.default(this, action, resource, add, cb);
    }
 
    /**
     * Callback returns true if role or permission exists
     * @method RBAC#exists
     * @param  {String}   name  Name of item
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'exists',
    value: function exists(name, cb) {
      this.storage.exists(name, cb);
      return this;
    }
 
    /**
     * Callback returns true if role exists
     * @method RBAC#existsRole
     * @param  {String}   name  Name of item
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'existsRole',
    value: function existsRole(name, cb) {
      this.storage.existsRole(name, cb);
      return this;
    }
 
    /**
     * Callback returns true if permission exists
     * @method RBAC#existsPermission
     * @param  {String}   action  Name of action
     * @param  {String}   resource  Name of resource
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'existsPermission',
    value: function existsPermission(action, resource, cb) {
      this.storage.existsPermission(action, resource, cb);
      return this;
    }
 
    /**
     * Return instance of Role by his name
     * @method RBAC#getRole
     * @param  {String}   name  Name of role
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'getRole',
    value: function getRole(name, cb) {
      this.storage.getRole(name, cb);
      return this;
    }
 
    /**
     * Return all instances of Role
     * @method RBAC#getRoles
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'getRoles',
    value: function getRoles(cb) {
      this.storage.getRoles(cb);
      return this;
    }
 
    /**
     * Return instance of Permission by his action and resource
     * @method RBAC#getPermission
     * @param  {String} action    Name of action
     * @param  {String} resource  Name of resource
     * @param  {Function} cb      Callback function
     * @return {RBAC}             Return instance of actual RBAC
     */
 
  }, {
    key: 'getPermission',
    value: function getPermission(action, resource, cb) {
      this.storage.getPermission(action, resource, cb);
      return this;
    }
 
    /**
     * Return instance of Permission by his name
     * @method RBAC#getPermission
     * @param  {String} name      Name of permission
     * @param  {Function} cb      Callback function
     * @return {RBAC}             Return instance of actual RBAC
     */
 
  }, {
    key: 'getPermissionByName',
    value: function getPermissionByName(name, cb) {
      var data = _Permission2.default.decodeName(name);
      this.storage.getPermission(data.action, data.resource, cb);
      return this;
    }
 
    /**
     * Return all instances of Permission
     * @method RBAC#getPermissions
     * @param  {Function} cb    Callback function
     * @return {RBAC}           Return instance of actual RBAC
     */
 
  }, {
    key: 'getPermissions',
    value: function getPermissions(cb) {
      this.storage.getPermissions(cb);
      return this;
    }
 
    /**
     * Create multiple permissions in one step
     * @method RBAC#createPermissions
     * @param  {Object}   permissions Object of permissions
     * @param  {Boolean} [add=true]   True if you need to add it to the storage
     * @param  {Function} cb          Callbck function
     * @return {RBAC}                 Instance of actual RBAC
     */
 
  }, {
    key: 'createPermissions',
    value: function createPermissions(resources, add, cb) {
      var _this4 = this;
 
      if (typeof add === 'function') {
        return this.createPermissions(resources, true, add);
      }
 
      var tasks = {};
 
      Iif (!(0, _isPlainObject2.default)(resources)) {
        return cb(new Error('Resources is not a plain object'));
      }
 
      Object.keys(resources).forEach(function (resource) {
        resources[resource].forEach(function (action) {
          var name = _Permission2.default.createName(action, resource);
          tasks[name] = function (callback) {
            return _this4.createPermission(action, resource, add, callback);
          };
        }, _this4);
      }, this);
 
      (0, _async.parallel)(tasks, cb);
      return this;
    }
 
    /**
     * Create multiple roles in one step assigned to actual instance of RBAC
     * @method RBAC#createRoles
     * @param  {Array}    roleNames  Array of role names
     * @param  {Boolean} [add=true]   True if you need to add it to the storage
     * @param  {Function} cb         Callback function
     * @return {RBAC}                Current instance
     */
 
  }, {
    key: 'createRoles',
    value: function createRoles(roleNames, add, cb) {
      var _this5 = this;
 
      if (typeof add === 'function') {
        return this.createRoles(roleNames, true, add);
      }
 
      var tasks = {};
 
      roleNames.forEach(function (roleName) {
        tasks[roleName] = function (callback) {
          return _this5.createRole(roleName, add, callback);
        };
      }, this);
 
      (0, _async.parallel)(tasks, cb);
      return this;
    }
 
    /**
     * Grant multiple items in one function
     * @method RBAC#grants
     * @param  {Object}       List of roles
     * @param  {Function} cb  Callback function
     * @return {RBAC}         Current instance
     */
 
  }, {
    key: 'grants',
    value: function grants(roles, cb) {
      var _this6 = this;
 
      Iif (!(0, _isPlainObject2.default)(roles)) {
        return cb(new Error('Grants is not a plain object'));
      }
 
      var tasks = [];
 
      Object.keys(roles).forEach(function (role) {
        roles[role].forEach(function (grant) {
          tasks.push(function (callback) {
            return _this6.grantByName(role, grant, callback);
          });
        }, _this6);
      }, this);
 
      (0, _async.parallel)(tasks, cb);
      return this;
    }
 
    /**
     * Create multiple permissions and roles in one step
     * @method RBAC#create
     * @param  {Array}   roleNames       List of role names
     * @param  {Object}  permissionNames List of permission names
     * @param  {Object}  [grants]        List of grants
     * @param  {Array}   cb              Callback function
     * @return {RBAC}                    Instance of actual RBAC
     */
 
  }, {
    key: 'create',
    value: function create(roleNames, permissionNames, grants, cb) {
      var _this7 = this;
 
      if (typeof grants === 'function') {
        return this.create(roleNames, permissionNames, null, grants);
      }
 
      var tasks = {
        permissions: function permissions(callback) {
          return _this7.createPermissions(permissionNames, callback);
        },
        roles: function roles(callback) {
          return _this7.createRoles(roleNames, callback);
        }
      };
 
      (0, _async.parallel)(tasks, function (err, result) {
        if (err || !grants) {
          return cb(err, result);
        }
 
        // add grants to roles
        _this7.grants(grants, function (err2) {
          Iif (err2) {
            return cb(err2);
          }
 
          cb(null, result);
        });
      });
 
      return this;
    }
 
    /**
     * Traverse hierarchy of roles.
     * Callback function returns as second parameter item from hierarchy or null if we are on the end of hierarchy.
     * @method RBAC#_traverseGrants
     * @param  {String}   roleName  Name of role
     * @param  {Function} cb        Callback function
     * @return {RBAC}               Return instance of actual RBAC
     * @private
     */
 
  }, {
    key: '_traverseGrants',
    value: function _traverseGrants(roleName, cb) {
      var _this8 = this;
 
      var next = arguments.length <= 2 || arguments[2] === undefined ? [roleName] : arguments[2];
      var used = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
 
      var actualRole = next.shift();
      used[actualRole] = true;
 
      this.storage.getGrants(actualRole, function (err) {
        var items = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
 
        Iif (err) {
          return cb(err);
        }
 
        for (var i = 0; i < items.length; i++) {
          var item = items[i];
          var name = item.name;
 
          if (RBAC.isRole(item) && !used[name]) {
            used[name] = true;
            next.push(name);
          }
 
          if (cb(null, item) === false) {
            return void 0;
          }
        }
 
        if (next.length === 0) {
          return cb(null, null);
        }
 
        _this8._traverseGrants(null, cb, next, used);
      });
 
      return this;
    }
 
    /**
     * Return true if role has allowed permission
     * @method RBAC#can
     * @param  {String}  roleName Name of role
     * @param  {String}  action   Name of action
     * @param  {String}  resource Name of resource
     * @param  {Function} cb        Callback function
     * @return {RBAC}             Current instance
     */
 
  }, {
    key: 'can',
    value: function can(roleName, action, resource, cb) {
      this._traverseGrants(roleName, function (err, item) {
        // if there is a error
        Iif (err) {
          return cb(err);
        }
 
        // this is last item
        if (!item) {
          return cb(null, false);
        }
 
        if (RBAC.isPermission(item) && item.can(action, resource) === true) {
          cb(null, true);
          // end up actual traversing
          return false;
        }
      });
 
      return this;
    }
 
    /**
     * Check if the role has any of the given permissions.
     * @method RBAC#canAny
     * @param  {String} roleName     Name of role
     * @param  {Array}  permissions  Array (String action, String resource)
     * @param  {Function} cb        Callback function
     * @return {RBAC}                Current instance
     */
 
  }, {
    key: 'canAny',
    value: function canAny(roleName, permissions, cb) {
      // prepare the names of permissions
      var permissionNames = RBAC.getPermissionNames(permissions);
 
      // traverse hierarchy
      this._traverseGrants(roleName, function (err, item) {
        // if there is a error
        Iif (err) {
          return cb(err);
        }
 
        // this is last item
        Iif (!item) {
          return cb(null, false);
        }
 
        Eif (RBAC.isPermission(item) && permissionNames.indexOf(item.name) !== -1) {
          cb(null, true);
          // end up actual traversing
          return false;
        }
      });
 
      return this;
    }
 
    /**
     * Check if the model has all of the given permissions.
     * @method RBAC#canAll
     * @param  {String} roleName     Name of role
     * @param  {Array}  permissions  Array (String action, String resource)
     * @param  {Function} cb        Callback function
     * @return {RBAC}                Current instance
     */
 
  }, {
    key: 'canAll',
    value: function canAll(roleName, permissions, cb) {
      // prepare the names of permissions
      var permissionNames = RBAC.getPermissionNames(permissions);
      var founded = {};
      var foundedCount = 0;
 
      // traverse hierarchy
      this._traverseGrants(roleName, function (err, item) {
        // if there is a error
        Iif (err) {
          return cb(err);
        }
 
        // this is last item
        if (!item) {
          return cb(null, false);
        }
 
        if (RBAC.isPermission(item) && permissionNames.indexOf(item.name) !== -1 && !founded[item.name]) {
          founded[item.name] = true;
          foundedCount++;
 
          if (foundedCount === permissionNames.length) {
            cb(null, true);
            // end up actual traversing
            return false;
          }
        }
      });
 
      return this;
    }
 
    /**
     * Return true if role has allowed permission
     * @method RBAC#hasRole
     * @param  {String}   roleName        Name of role
     * @param  {String}   roleChildName   Name of child role
     * @param  {Function} cb              Callback function
     * @return {RBAC}                     Current instance
     */
 
  }, {
    key: 'hasRole',
    value: function hasRole(roleName, roleChildName, cb) {
      if (roleName === roleChildName) {
        cb(null, true);
        return this;
      }
 
      this._traverseGrants(roleName, function (err, item) {
        // if there is a error
        if (err) {
          return cb(err);
        }
 
        // this is last item
        if (!item) {
          return cb(null, false);
        }
 
        if (RBAC.isRole(item) && item.name === roleChildName) {
          cb(null, true);
          // end up actual traversing
          return false;
        }
      });
 
      return this;
    }
 
    /**
     * Return array of all permission assigned to role of RBAC
     * @method RBAC#getScope
     * @param  {String} roleName   Name of role
     * @param  {Function} cb       Callback function
     * @return {RBAC}              Current instance
     */
 
  }, {
    key: 'getScope',
    value: function getScope(roleName, cb) {
      var scope = [];
 
      // traverse hierarchy
      this._traverseGrants(roleName, function (err, item) {
        // if there is a error
        Iif (err) {
          return cb(err);
        }
 
        // this is last item
        if (!item) {
          return cb(null, scope);
        }
 
        if (RBAC.isPermission(item) && scope.indexOf(item.name) === -1) {
          scope.push(item.name);
        }
      });
 
      return this;
    }
 
    /**
     * Convert Array of permissions to permission name
     * @function getPermissionNames
     * @memberof RBAC
     * @param  {Array} permissions List of array items of permission names. It contan action and resource
     * @return {Array}             List of permission names
     * @static
     */
 
  }, {
    key: 'options',
    get: function get() {
      return this._options;
    }
 
    /**
     * The RBAC's storage.
     * @member RBAC#storage {Storage}
     */
 
  }, {
    key: 'storage',
    get: function get() {
      return this.options.storage;
    }
  }], [{
    key: 'getPermissionNames',
    value: function getPermissionNames(permissions) {
      var permissionNames = [];
 
      for (var i = 0; i < permissions.length; i++) {
        var permission = permissions[i];
        permissionNames.push(_Permission2.default.createName(permission[0], permission[1]));
      }
 
      return permissionNames;
    }
  }, {
    key: 'isPermission',
    value: function isPermission(item) {
      return item instanceof _Permission2.default;
    }
  }, {
    key: 'isRole',
    value: function isRole(item) {
      return item instanceof _Role2.default;
    }
  }]);
 
  return RBAC;
}();
 
exports.default = RBAC;