rbac 4.0.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/HISTORY.md +21 -3
  2. package/LICENSE +21 -0
  3. package/README.md +43 -104
  4. package/dist/Base.js +31 -65
  5. package/dist/Base.js.map +1 -0
  6. package/dist/Memory.js +139 -0
  7. package/dist/Memory.js.map +1 -0
  8. package/dist/Permission.js +99 -137
  9. package/dist/Permission.js.map +1 -0
  10. package/dist/RBAC.js +420 -798
  11. package/dist/RBAC.js.map +1 -0
  12. package/dist/Role.js +67 -142
  13. package/dist/Role.js.map +1 -0
  14. package/dist/Storage.js +160 -0
  15. package/dist/Storage.js.map +1 -0
  16. package/dist/index.js +45 -35
  17. package/dist/index.js.map +1 -0
  18. package/package.json +22 -45
  19. package/.babelrc +0 -9
  20. package/.eslintignore +0 -7
  21. package/.eslintrc +0 -5
  22. package/.npmignore +0 -4
  23. package/.travis.yml +0 -14
  24. package/__tests__/role.spec.js +0 -410
  25. package/controllers/express.js +0 -72
  26. package/coverage/clover.xml +0 -526
  27. package/coverage/coverage-final.json +0 -9
  28. package/coverage/lcov-report/base.css +0 -212
  29. package/coverage/lcov-report/index.html +0 -106
  30. package/coverage/lcov-report/prettify.css +0 -1
  31. package/coverage/lcov-report/prettify.js +0 -1
  32. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  33. package/coverage/lcov-report/sorter.js +0 -158
  34. package/coverage/lcov-report/tests/index.html +0 -93
  35. package/coverage/lcov-report/tests/role.spec.js.html +0 -1316
  36. package/coverage/lcov.info +0 -1115
  37. package/dist/storages/Memory.js +0 -219
  38. package/dist/storages/Mongoose.js +0 -303
  39. package/dist/storages/index.js +0 -300
  40. package/example/simple.js +0 -25
  41. package/gulpfile.js +0 -16
  42. package/jsdocConfig.js +0 -20
@@ -1,164 +1,126 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.DELIMITER = undefined;
7
-
8
- 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) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9
-
10
- var _Base2 = require('./Base');
11
-
12
- var _Base3 = _interopRequireDefault(_Base2);
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
-
18
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
19
-
20
- function _inherits(subClass, superClass) { if (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 } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
21
-
22
- var DELIMITER = exports.DELIMITER = '_';
23
-
24
- var Permission = function (_Base) {
25
- _inherits(Permission, _Base);
6
+ exports.default = void 0;
7
+ var _Base = _interopRequireDefault(require("./Base"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ class Permission extends _Base.default {
10
+ /**
11
+ * Compute name of permission from action and resource
12
+ * @function createName
13
+ * @memberof Permission
14
+ * @param {String} action Name of permission
15
+ * @param {String} resource Resource of permission
16
+ * @param {String} delimiter delimiter
17
+ * @return {String} Computed name of permission
18
+ * @static
19
+ */
20
+ static createName(action, resource, delimiter) {
21
+ if (!delimiter) {
22
+ throw new Error('Delimiter is not defined');
23
+ }
24
+ if (!action) {
25
+ throw new Error('Action is not defined');
26
+ }
27
+ if (!resource) {
28
+ throw new Error('Resource is not defined');
29
+ }
30
+ return `${action}${delimiter}${resource}`;
31
+ }
32
+ static decodeName(name, delimiter) {
33
+ if (!delimiter) {
34
+ throw new Error('delimiter is required');
35
+ }
36
+ if (!name) {
37
+ throw new Error('Name is required');
38
+ }
39
+ const pos = name.indexOf(delimiter);
40
+ if (pos === -1) {
41
+ throw new Error('Wrong name');
42
+ }
43
+ return {
44
+ action: name.substring(0, pos),
45
+ resource: name.substring(pos + 1)
46
+ };
47
+ }
26
48
 
27
49
  /**
28
50
  * Permission constructor
29
51
  * @constructor Permission
30
52
  * @extends {Base}
31
- * @param {RBAC} rbac Instance of the RBAC
32
- * @param {String} action Name of the action
33
- * @param {String} resource Name of the resource
34
- * @param {Boolean} [add=true] True if you need to save it to storage
35
- * @param {Function} cb Callback function after add
53
+ * @param {RBAC} rbac Instance of the RBAC
54
+ * @param {string} action Name of the action
55
+ * @param {string} resource Name of the resource
36
56
  */
37
- function Permission(rbac, action, resource, add, cb) {
38
- _classCallCheck(this, Permission);
39
-
40
- if (typeof add === 'function') {
41
- cb = add;
42
- add = true;
43
- }
44
-
57
+ constructor(rbac, action, resource) {
45
58
  if (!action || !resource) {
46
- var _ret;
47
-
48
- return _ret = cb(new Error('One of parameters is undefined')), _possibleConstructorReturn(_this, _ret);
59
+ throw new Error('One of parameters is undefined');
49
60
  }
50
-
51
- if (!Permission.isValidName(action) || !Permission.isValidName(resource)) {
52
- var _ret2;
53
-
54
- return _ret2 = cb(new Error('Action or resource has no valid name')), _possibleConstructorReturn(_this, _ret2);
61
+ if (!Permission.isValidName(action, rbac.options.delimiter) || !Permission.isValidName(resource, rbac.options.delimiter)) {
62
+ throw new Error('Action or resource has no valid name');
55
63
  }
56
-
57
- return _possibleConstructorReturn(this, (Permission.__proto__ || Object.getPrototypeOf(Permission)).call(this, rbac, Permission.createName(action, resource), add, cb));
64
+ super(rbac, Permission.createName(action, resource, rbac.options.delimiter));
58
65
  }
59
66
 
60
67
  /**
61
68
  * Get action name of actual permission
62
69
  * @member Permission#action {String} Action of permission
63
70
  */
64
-
65
-
66
- _createClass(Permission, [{
67
- key: 'can',
68
-
69
-
70
- /**
71
- * Return true if has same action and resource
72
- * @method Permission#can
73
- * @param {String} action Name of action
74
- * @param {String} resource Name of resource
75
- * @return {Boolean}
76
- */
77
- value: function can(action, resource) {
78
- return this.action === action && this.resource === resource;
79
- }
80
-
81
- /**
82
- * Compute name of permission from action and resource
83
- * @function createName
84
- * @memberof Permission
85
- * @param {String} action Name of permission
86
- * @param {String} resource Resource of permission
87
- * @return {String} Computed name of permission
88
- * @static
89
- */
90
-
91
- }, {
92
- key: 'action',
93
- get: function get() {
94
- if (!this._action) {
95
- var decoded = Permission.decodeName(this.name);
96
- if (!decoded) {
97
- throw new Error('Action is null');
98
- }
99
-
100
- this._action = decoded.action;
71
+ get action() {
72
+ if (!this._action) {
73
+ const decoded = Permission.decodeName(this.name, this.rbac.options.delimiter);
74
+ if (!decoded) {
75
+ throw new Error('Action is null');
101
76
  }
102
-
103
- return this._action;
77
+ this._action = decoded.action;
104
78
  }
79
+ return this._action;
80
+ }
105
81
 
106
- /**
107
- * Get resource name of actual permission
108
- * @member Permission#resource {String} Resource of permission
109
- */
110
-
111
- }, {
112
- key: 'resource',
113
- get: function get() {
114
- if (!this._resource) {
115
- var decoded = Permission.decodeName(this.name);
116
- if (!decoded) {
117
- throw new Error('Resource is null');
118
- }
119
-
120
- this._resource = decoded.resource;
121
- }
122
-
123
- return this._resource;
124
- }
125
- }], [{
126
- key: 'createName',
127
- value: function createName(action, resource) {
128
- return action + DELIMITER + resource;
129
- }
130
- }, {
131
- key: 'decodeName',
132
- value: function decodeName(name) {
133
- var pos = name.indexOf(DELIMITER);
134
- if (pos === -1) {
135
- return null;
82
+ /**
83
+ * Get resource name of actual permission
84
+ * @member Permission#resource {String} Resource of permission
85
+ */
86
+ get resource() {
87
+ if (!this._resource) {
88
+ const decoded = Permission.decodeName(this.name, this.rbac.options.delimiter);
89
+ if (!decoded) {
90
+ throw new Error('Resource is null');
136
91
  }
137
-
138
- return {
139
- action: name.substr(0, pos),
140
- resource: name.substr(pos + 1)
141
- };
92
+ this._resource = decoded.resource;
142
93
  }
94
+ return this._resource;
95
+ }
143
96
 
144
- /**
145
- * Correct name can not contain whitespace or underscores.
146
- * @function isValidName
147
- * @memberof Permission
148
- * @param {String} name Name
149
- * @return {Boolean}
150
- * @static
151
- */
97
+ /**
98
+ * Return true if has same action and resource
99
+ * @method Permission#can
100
+ * @param {String} action Name of action
101
+ * @param {String} resource Name of resource
102
+ * @return {Boolean}
103
+ */
104
+ can(action, resource) {
105
+ return this.action === action && this.resource === resource;
106
+ }
152
107
 
153
- }, {
154
- key: 'isValidName',
155
- value: function isValidName(name) {
156
- var exp = new RegExp('^[^' + DELIMITER + '\\s]+$');
157
- return exp.test(name);
108
+ /**
109
+ * Correct name can not contain whitespace or underscores.
110
+ * @function isValidName
111
+ * @memberof Permission
112
+ * @param {String} name Name
113
+ * @param {String} delimiter Delimiter
114
+ * @return {Boolean}
115
+ * @static
116
+ */
117
+ static isValidName(name, delimiter) {
118
+ if (!delimiter) {
119
+ throw new Error('Delimeter is not defined');
158
120
  }
159
- }]);
160
-
161
- return Permission;
162
- }(_Base3.default);
163
-
164
- exports.default = Permission;
121
+ const exp = new RegExp(`^[^${delimiter}\\s]+$`);
122
+ return exp.test(name);
123
+ }
124
+ }
125
+ exports.default = Permission;
126
+ //# sourceMappingURL=Permission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Permission.js","names":["_Base","_interopRequireDefault","require","e","__esModule","default","Permission","Base","createName","action","resource","delimiter","Error","decodeName","name","pos","indexOf","substring","constructor","rbac","isValidName","options","_action","decoded","_resource","can","exp","RegExp","test","exports"],"sources":["../src/Permission.js"],"sourcesContent":["// @flow\nimport Base from './Base';\n\nexport default class Permission extends Base {\n /**\n * Compute name of permission from action and resource\n * @function createName\n * @memberof Permission\n * @param {String} action Name of permission\n * @param {String} resource Resource of permission\n * @param {String} delimiter delimiter\n * @return {String} Computed name of permission\n * @static\n */\n static createName(action: string, resource: string, delimiter: string): string {\n if (!delimiter) {\n throw new Error('Delimiter is not defined');\n }\n\n if (!action) {\n throw new Error('Action is not defined');\n }\n\n if (!resource) {\n throw new Error('Resource is not defined');\n }\n\n return `${action}${delimiter}${resource}`;\n }\n\n static decodeName(name: string, delimiter: string): Object {\n if (!delimiter) {\n throw new Error('delimiter is required');\n }\n\n if (!name) {\n throw new Error('Name is required');\n }\n\n const pos = name.indexOf(delimiter);\n if (pos === -1) {\n throw new Error('Wrong name');\n }\n\n return {\n action: name.substring(0, pos),\n resource: name.substring(pos + 1),\n };\n }\n\n /**\n * Permission constructor\n * @constructor Permission\n * @extends {Base}\n * @param {RBAC} rbac Instance of the RBAC\n * @param {string} action Name of the action\n * @param {string} resource Name of the resource\n */\n constructor(rbac: RBAC, action: string, resource: string) {\n if (!action || !resource) {\n throw new Error('One of parameters is undefined');\n }\n\n if (!Permission.isValidName(action, rbac.options.delimiter) || !Permission.isValidName(resource, rbac.options.delimiter)) {\n throw new Error('Action or resource has no valid name');\n }\n\n super(rbac, Permission.createName(action, resource, rbac.options.delimiter));\n }\n\n /**\n * Get action name of actual permission\n * @member Permission#action {String} Action of permission\n */\n get action(): string {\n if (!this._action) {\n const decoded = Permission.decodeName(this.name, this.rbac.options.delimiter);\n if (!decoded) {\n throw new Error('Action is null');\n }\n\n this._action = decoded.action;\n }\n\n return this._action;\n }\n\n /**\n * Get resource name of actual permission\n * @member Permission#resource {String} Resource of permission\n */\n get resource(): string {\n if (!this._resource) {\n const decoded = Permission.decodeName(this.name, this.rbac.options.delimiter);\n if (!decoded) {\n throw new Error('Resource is null');\n }\n\n this._resource = decoded.resource;\n }\n\n return this._resource;\n }\n\n /**\n * Return true if has same action and resource\n * @method Permission#can\n * @param {String} action Name of action\n * @param {String} resource Name of resource\n * @return {Boolean}\n */\n can(action: string, resource: string): boolean {\n return this.action === action && this.resource === resource;\n }\n\n /**\n * Correct name can not contain whitespace or underscores.\n * @function isValidName\n * @memberof Permission\n * @param {String} name Name\n * @param {String} delimiter Delimiter\n * @return {Boolean}\n * @static\n */\n static isValidName(name: string, delimiter: string): boolean {\n if (!delimiter) {\n throw new Error('Delimeter is not defined');\n }\n\n const exp = new RegExp(`^[^${delimiter}\\\\s]+$`);\n return exp.test(name);\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,KAAA,GAAAC,sBAAA,CAAAC,OAAA;AAA0B,SAAAD,uBAAAE,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAEX,MAAMG,UAAU,SAASC,aAAI,CAAC;EAC3C;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACE,OAAOC,UAAUA,CAACC,MAAc,EAAEC,QAAgB,EAAEC,SAAiB,EAAU;IAC7E,IAAI,CAACA,SAAS,EAAE;MACd,MAAM,IAAIC,KAAK,CAAC,0BAA0B,CAAC;IAC7C;IAEA,IAAI,CAACH,MAAM,EAAE;MACX,MAAM,IAAIG,KAAK,CAAC,uBAAuB,CAAC;IAC1C;IAEA,IAAI,CAACF,QAAQ,EAAE;MACb,MAAM,IAAIE,KAAK,CAAC,yBAAyB,CAAC;IAC5C;IAEA,OAAO,GAAGH,MAAM,GAAGE,SAAS,GAAGD,QAAQ,EAAE;EAC3C;EAEA,OAAOG,UAAUA,CAACC,IAAY,EAAEH,SAAiB,EAAU;IACzD,IAAI,CAACA,SAAS,EAAE;MACd,MAAM,IAAIC,KAAK,CAAC,uBAAuB,CAAC;IAC1C;IAEA,IAAI,CAACE,IAAI,EAAE;MACT,MAAM,IAAIF,KAAK,CAAC,kBAAkB,CAAC;IACrC;IAEA,MAAMG,GAAG,GAAGD,IAAI,CAACE,OAAO,CAACL,SAAS,CAAC;IAC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