rbac 5.0.3 → 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.
package/HISTORY.md CHANGED
@@ -1,4 +1,22 @@
1
- # Change/History Log
1
+ # v6.0.0
2
2
 
3
- This project adheres to [Semantic Versioning](http://semver.org/).
4
- Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/seeden/rbac/releases) page.
3
+ - Zero production dependencies (removed lodash, keymirror)
4
+ - Minimum Node.js version: 18.0.0
5
+ - Updated build toolchain (Babel 7.26, Jest 29)
6
+ - Cleaned up development dependencies (48 to 7)
7
+ - Fixed LICENSE copyright
8
+ - Removed deprecated API usage (String.prototype.substr)
9
+ - Removed dead references to Mongoose, DynamoDB, Express middleware
10
+
11
+ # v5.0.3
12
+
13
+ - Added DynamoDB support via dynamoose
14
+
15
+ # v5.0.2
16
+
17
+ - Bug fix
18
+
19
+ # v5.0.1
20
+
21
+ - Async RBAC
22
+ - Updated tests
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014-2026 Zlatko Fedor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,39 +1,9 @@
1
1
  # RBAC
2
- (Hierarchical Role Based Access Control)
3
2
 
4
- [![NPM version][npm-image]][npm-url]
5
- [![build status][travis-image]][travis-url]
6
- [![Test coverage][coveralls-image]][coveralls-url]
7
- [![Gitter chat](https://badges.gitter.im/seeden/rbac.png)](https://gitter.im/seeden/rbac)
3
+ Hierarchical Role Based Access Control for Node.js
8
4
 
9
- [npm-image]: https://img.shields.io/npm/v/rbac.svg?style=flat-square
10
- [npm-url]: https://www.npmjs.com/rbac
11
- [travis-image]: https://img.shields.io/travis/seeden/rbac/master.svg?style=flat-square
12
- [travis-url]: https://travis-ci.org/seeden/rbac
13
- [coveralls-image]: https://img.shields.io/coveralls/seeden/rbac/master.svg?style=flat-square
14
- [coveralls-url]: https://coveralls.io/r/seeden/rbac?branch=master
15
- [github-url]: https://github.com/seeden/rbac
16
-
17
- RBAC is the authorization library for NodeJS.
18
-
19
- :tada: We have supported DynamoDB storage now by implementation of [dynamoose](https://github.com/automategreen/dynamoose).
20
-
21
- ## Motivation
22
-
23
- I needed hierarchical role based access control for my projects based on ExpressJS.
24
- I had one requirement. This structure must be permanently stored in various storages.
25
- For example in memory or Mongoose.
26
- Because there is a lot of options for storing of data and many of them are asynchronous.
27
- I created asynchronous API.
28
- Please, if you found any bug or you need custom API, create an issue or pull request.
29
-
30
- ## Documentation
31
-
32
- [Read more about API in documentation](http://seeden.github.io/rbac/RBAC.html)
33
-
34
- # Support us
35
-
36
- Star this project on [GitHub][github-url].
5
+ [![NPM version](https://img.shields.io/npm/v/rbac.svg)](https://www.npmjs.com/package/rbac)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
37
7
 
38
8
  ## Install
39
9
 
@@ -44,7 +14,8 @@ npm install rbac
44
14
  ## Usage
45
15
 
46
16
  ```js
47
- import { RBAC } from 'rbac'; // ES5 var RBAC = require('rbac').default;
17
+ import { RBAC } from 'rbac';
18
+
48
19
  const rbac = new RBAC({
49
20
  roles: ['superadmin', 'admin', 'user', 'guest'],
50
21
  permissions: {
@@ -64,77 +35,60 @@ const rbac = new RBAC({
64
35
  await rbac.init();
65
36
  ```
66
37
 
67
- ## Usage with express
68
-
69
- ```js
70
- import express from 'express';
71
- import { RBAC } from 'rbac';
72
- import secure from 'rbac/controllers/express';
73
-
74
- // your custom controller for express
75
- function adminController(req, res, next) {
76
- res.send('Hello admin');
77
- }
78
-
79
- const app = express();
80
- const rbac = new RBAC({
81
- roles: ['admin', 'user'],
82
- });
83
-
84
- await rbac.init();
85
-
86
- // setup express routes
87
- app.use('/admin', secure.hasRole(rbac, 'admin'), adminController);
88
- ```
89
-
90
38
  ## Check permissions
91
39
 
92
40
  ```js
93
41
  const can = await rbac.can('admin', 'create', 'article');
94
42
  if (can) {
95
- console.log('Admin is able create article');
43
+ console.log('Admin is able to create article');
96
44
  }
45
+ ```
46
+
47
+ Or use a role instance:
97
48
 
98
- // or you can use instance of admin role
49
+ ```js
99
50
  const admin = await rbac.getRole('admin');
100
51
  if (!admin) {
101
- return console.log('Role does not exists');
102
- }
103
-
104
- const can = await admin.can('create', 'article');
105
- if (can) {
106
- console.log('Admin is able create article');
52
+ console.log('Role does not exist');
53
+ } else {
54
+ const can = await admin.can('create', 'article');
55
+ if (can) {
56
+ console.log('Admin is able to create article');
57
+ }
107
58
  }
108
59
  ```
109
60
 
110
- ## Mongoose user model
61
+ ## Custom storage
111
62
 
112
- Please take a look on plugin [mongoose-hrbac](http://github.com/seeden/mongoose-hrbac)
63
+ RBAC uses in-memory storage by default. You can implement custom storage by extending the `Storage` class:
113
64
 
114
- ## Build documentation
115
-
116
- ```sh
117
- npm run doc
118
- ```
119
-
120
- ## Running Tests
65
+ ```js
66
+ import { Storage } from 'rbac';
67
+
68
+ class MyStorage extends Storage {
69
+ async add(item) { /* ... */ }
70
+ async remove(item) { /* ... */ }
71
+ async grant(role, child) { /* ... */ }
72
+ async revoke(role, child) { /* ... */ }
73
+ async get(name) { /* ... */ }
74
+ async getRoles() { /* ... */ }
75
+ async getPermissions() { /* ... */ }
76
+ async getGrants(role) { /* ... */ }
77
+ }
121
78
 
122
- ```sh
123
- npm run test
79
+ const rbac = new RBAC({ storage: new MyStorage() });
124
80
  ```
125
81
 
126
- ## Build
82
+ ## Running Tests
127
83
 
128
84
  ```sh
129
- npm run build
85
+ npm test
130
86
  ```
131
87
 
132
88
  ## Credits
133
89
 
134
- - [Zlatko Fedor](http://github.com/seeden)
90
+ [Zlatko Fedor](https://github.com/seeden)
135
91
 
136
92
  ## License
137
93
 
138
- The MIT License (MIT)
139
-
140
- Copyright (c) 2016-2018 Zlatko Fedor zfedor@goodmodule.com
94
+ MIT
package/dist/Base.js CHANGED
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
-
6
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
7
-
8
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
9
-
10
7
  class Base {
11
8
  /**
12
9
  * Base constructor
@@ -19,46 +16,33 @@ class Base {
19
16
  if (!rbac || !name) {
20
17
  throw new Error('One of parameters is undefined');
21
18
  }
22
-
23
19
  this.name = name;
24
20
  this.rbac = rbac;
25
21
  }
22
+
26
23
  /**
27
24
  * Add this to RBAC (storage)
28
25
  * @method Base#remove
29
26
  * @return {boolean}
30
27
  */
31
-
32
-
33
- add() {
34
- var _this = this;
35
-
36
- return _asyncToGenerator(function* () {
37
- const {
38
- rbac
39
- } = _this;
40
- return rbac.add(_this);
41
- })();
28
+ async add() {
29
+ const {
30
+ rbac
31
+ } = this;
32
+ return rbac.add(this);
42
33
  }
34
+
43
35
  /**
44
36
  * Remove this from RBAC (storage)
45
37
  * @method Base#remove
46
38
  * @return {boolean}
47
39
  */
48
-
49
-
50
- remove() {
51
- var _this2 = this;
52
-
53
- return _asyncToGenerator(function* () {
54
- const {
55
- rbac
56
- } = _this2;
57
- return rbac.remove(_this2);
58
- })();
40
+ async remove() {
41
+ const {
42
+ rbac
43
+ } = this;
44
+ return rbac.remove(this);
59
45
  }
60
-
61
46
  }
62
-
63
47
  exports.default = Base;
64
48
  //# sourceMappingURL=Base.js.map
package/dist/Base.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Base.js"],"names":["Base","constructor","rbac","name","Error","add","remove"],"mappings":";;;;;;;;;AAGe,MAAMA,IAAN,CAAW;AACxB;;;;;;;AAOAC,EAAAA,WAAW,CAACC,IAAD,EAAaC,IAAb,EAA2B;AACpC,QAAI,CAACD,IAAD,IAAS,CAACC,IAAd,EAAoB;AAClB,YAAM,IAAIC,KAAJ,CAAU,gCAAV,CAAN;AACD;;AAED,SAAKD,IAAL,GAAYA,IAAZ;AACA,SAAKD,IAAL,GAAYA,IAAZ;AACD;AAED;;;;;;;AAKMG,EAAAA,GAAN,GAAqB;AAAA;;AAAA;AACnB,YAAM;AAAEH,QAAAA;AAAF,UAAW,KAAjB;AACA,aAAOA,IAAI,CAACG,GAAL,CAAS,KAAT,CAAP;AAFmB;AAGpB;AAED;;;;;;;AAKMC,EAAAA,MAAN,GAAwB;AAAA;;AAAA;AACtB,YAAM;AAAEJ,QAAAA;AAAF,UAAW,MAAjB;AACA,aAAOA,IAAI,CAACI,MAAL,CAAY,MAAZ,CAAP;AAFsB;AAGvB;;AAnCuB","sourcesContent":["// @flow\nimport type RBAC from './RBAC';\n\nexport default class Base {\n /**\n * Base constructor\n * @constructor Base\n * @param {RBAC} rbac Instance of the RBAC\n * @param {String} name Name of the grant\n * @param {Function} cb Callback function after add\n */\n constructor(rbac: RBAC, name: string) {\n if (!rbac || !name) {\n throw new Error('One of parameters is undefined');\n }\n\n this.name = name;\n this.rbac = rbac;\n }\n\n /**\n * Add this to RBAC (storage)\n * @method Base#remove\n * @return {boolean}\n */\n async add(): boolean {\n const { rbac } = this;\n return rbac.add(this);\n }\n\n /**\n * Remove this from RBAC (storage)\n * @method Base#remove\n * @return {boolean}\n */\n async remove(): boolean {\n const { rbac } = this;\n return rbac.remove(this);\n }\n}\n"],"file":"Base.js"}
1
+ {"version":3,"file":"Base.js","names":["Base","constructor","rbac","name","Error","add","remove","exports","default"],"sources":["../src/Base.js"],"sourcesContent":["// @flow\nimport type RBAC from './RBAC';\n\nexport default class Base {\n /**\n * Base constructor\n * @constructor Base\n * @param {RBAC} rbac Instance of the RBAC\n * @param {String} name Name of the grant\n * @param {Function} cb Callback function after add\n */\n constructor(rbac: RBAC, name: string) {\n if (!rbac || !name) {\n throw new Error('One of parameters is undefined');\n }\n\n this.name = name;\n this.rbac = rbac;\n }\n\n /**\n * Add this to RBAC (storage)\n * @method Base#remove\n * @return {boolean}\n */\n async add(): boolean {\n const { rbac } = this;\n return rbac.add(this);\n }\n\n /**\n * Remove this from RBAC (storage)\n * @method Base#remove\n * @return {boolean}\n */\n async remove(): boolean {\n const { rbac } = this;\n return rbac.remove(this);\n }\n}\n"],"mappings":";;;;;;AAGe,MAAMA,IAAI,CAAC;EACxB;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,IAAU,EAAEC,IAAY,EAAE;IACpC,IAAI,CAACD,IAAI,IAAI,CAACC,IAAI,EAAE;MAClB,MAAM,IAAIC,KAAK,CAAC,gCAAgC,CAAC;IACnD;IAEA,IAAI,CAACD,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACD,IAAI,GAAGA,IAAI;EAClB;;EAEA;AACF;AACA;AACA;AACA;EACE,MAAMG,GAAGA,CAAA,EAAY;IACnB,MAAM;MAAEH;IAAK,CAAC,GAAG,IAAI;IACrB,OAAOA,IAAI,CAACG,GAAG,CAAC,IAAI,CAAC;EACvB;;EAEA;AACF;AACA;AACA;AACA;EACE,MAAMC,MAAMA,CAAA,EAAY;IACtB,MAAM;MAAEJ;IAAK,CAAC,GAAG,IAAI;IACrB,OAAOA,IAAI,CAACI,MAAM,CAAC,IAAI,CAAC;EAC1B;AACF;AAACC,OAAA,CAAAC,OAAA,GAAAR,IAAA","ignoreList":[]}
package/dist/Memory.js CHANGED
@@ -1,216 +1,139 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
-
6
7
  var _Storage = _interopRequireDefault(require("./Storage"));
7
-
8
8
  var _Permission = _interopRequireDefault(require("./Permission"));
9
-
10
9
  var _Role = _interopRequireDefault(require("./Role"));
11
-
12
10
  var _Base = _interopRequireDefault(require("./Base"));
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
17
-
18
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
19
-
20
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
-
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
22
12
  class Memory extends _Storage.default {
23
- constructor(...args) {
24
- super(...args);
25
-
26
- _defineProperty(this, "items", {});
13
+ items = {};
14
+ async add(item) {
15
+ const {
16
+ name
17
+ } = item;
18
+ if (this.items[name]) {
19
+ throw new Error(`Item ${name} already exists`);
20
+ }
21
+ this.items[name] = {
22
+ instance: item,
23
+ grants: []
24
+ };
25
+ return true;
27
26
  }
28
-
29
- add(item) {
30
- var _this = this;
31
-
32
- return _asyncToGenerator(function* () {
27
+ async remove(item) {
28
+ const {
29
+ items
30
+ } = this;
31
+ const {
32
+ name
33
+ } = item;
34
+ if (!items[name]) {
35
+ throw new Error(`Item ${name} is not presented in storage`);
36
+ }
37
+
38
+ // revoke from all instances
39
+ Object.keys(items).forEach(itemName => {
33
40
  const {
34
- name
35
- } = item;
36
-
37
- if (_this.items[name]) {
38
- throw new Error(`Item ${name} already exists`);
39
- }
41
+ grants
42
+ } = items[itemName];
43
+ items[itemName].grants = grants.filter(grant => grant !== name);
44
+ });
40
45
 
41
- _this.items[name] = {
42
- instance: item,
43
- grants: []
44
- };
45
- return true;
46
- })();
46
+ // delete from items
47
+ delete this.items[name];
48
+ return true;
47
49
  }
48
-
49
- remove(item) {
50
- var _this2 = this;
51
-
52
- return _asyncToGenerator(function* () {
53
- const {
54
- items
55
- } = _this2;
56
- const {
57
- name
58
- } = item;
59
-
60
- if (!items[name]) {
61
- throw new Error(`Item ${name} is not presented in storage`);
62
- } // revoke from all instances
63
-
64
-
65
- Object.keys(items).forEach(itemName => {
66
- const {
67
- grants
68
- } = items[itemName];
69
- items[itemName].grants = grants.filter(grant => grant !== name);
70
- }); // delete from items
71
-
72
- delete _this2.items[name];
73
- return true;
74
- })();
50
+ async grant(role, child) {
51
+ const {
52
+ name
53
+ } = role;
54
+ const {
55
+ name: childName
56
+ } = child;
57
+ if (!this.items[name]) {
58
+ throw new Error(`Role ${name} is not exist`);
59
+ }
60
+ if (!this.items[childName]) {
61
+ throw new Error(`Base ${childName} is not exist`);
62
+ }
63
+ if (!(role instanceof _Role.default)) {
64
+ throw new Error('Role is not instance of Role');
65
+ }
66
+ if (name === childName) {
67
+ throw new Error(`You can grant yourself ${name}`);
68
+ }
69
+ const {
70
+ grants
71
+ } = this.items[name];
72
+ if (!grants.includes(childName)) {
73
+ grants.push(childName);
74
+ }
75
+ return true;
75
76
  }
76
-
77
- grant(role, child) {
78
- var _this3 = this;
79
-
80
- return _asyncToGenerator(function* () {
81
- const {
82
- name
83
- } = role;
84
- const {
85
- name: childName
86
- } = child;
87
-
88
- if (!_this3.items[name]) {
89
- throw new Error(`Role ${name} is not exist`);
90
- }
91
-
92
- if (!_this3.items[childName]) {
93
- throw new Error(`Base ${childName} is not exist`);
94
- }
95
-
96
- if (!(role instanceof _Role.default)) {
97
- throw new Error('Role is not instance of Role');
98
- }
99
-
100
- if (name === childName) {
101
- throw new Error(`You can grant yourself ${name}`);
102
- }
103
-
104
- const {
105
- grants
106
- } = _this3.items[name];
107
-
108
- if (!grants.includes(childName)) {
109
- grants.push(childName);
110
- }
111
-
112
- return true;
113
- })();
77
+ async revoke(role, child) {
78
+ const {
79
+ name
80
+ } = role;
81
+ const {
82
+ name: childName
83
+ } = child;
84
+ if (!this.items[name] || !this.items[childName]) {
85
+ throw new Error('Role is not exist');
86
+ }
87
+ const {
88
+ grants
89
+ } = this.items[name];
90
+ if (!grants.includes(childName)) {
91
+ throw new Error('Item is not associated to this item');
92
+ }
93
+ this.items[name].grants = grants.filter(grant => grant !== childName);
94
+ return true;
114
95
  }
115
-
116
- revoke(role, child) {
117
- var _this4 = this;
118
-
119
- return _asyncToGenerator(function* () {
120
- const {
121
- name
122
- } = role;
123
- const {
124
- name: childName
125
- } = child;
126
-
127
- if (!_this4.items[name] || !_this4.items[childName]) {
128
- throw new Error('Role is not exist');
129
- }
130
-
96
+ async get(name) {
97
+ if (name && this.items[name]) {
98
+ return this.items[name].instance;
99
+ }
100
+ return undefined;
101
+ }
102
+ async getRoles() {
103
+ return this.items.reduce((filtered, item) => {
131
104
  const {
132
- grants
133
- } = _this4.items[name];
134
-
135
- if (!grants.includes(childName)) {
136
- throw new Error('Item is not associated to this item');
105
+ instance
106
+ } = item;
107
+ if (instance instanceof _Role.default) {
108
+ filtered.push(instance);
137
109
  }
138
-
139
- _this4.items[name].grants = grants.filter(grant => grant !== childName);
140
- return true;
141
- })();
110
+ return filtered;
111
+ }, []);
142
112
  }
143
-
144
- get(name) {
145
- var _this5 = this;
146
-
147
- return _asyncToGenerator(function* () {
148
- if (name && _this5.items[name]) {
149
- return _this5.items[name].instance;
113
+ async getPermissions() {
114
+ return this.items.reduce((filtered, item) => {
115
+ const {
116
+ instance
117
+ } = item;
118
+ if (instance instanceof _Permission.default) {
119
+ filtered.push(instance);
150
120
  }
151
-
152
- return undefined;
153
- })();
154
- }
155
-
156
- getRoles() {
157
- var _this6 = this;
158
-
159
- return _asyncToGenerator(function* () {
160
- return _this6.items.reduce((filtered, item) => {
161
- const {
162
- instance
163
- } = item;
164
-
165
- if (instance instanceof _Role.default) {
166
- filtered.push(instance);
167
- }
168
-
169
- return filtered;
170
- }, []);
171
- })();
121
+ return filtered;
122
+ }, []);
172
123
  }
173
-
174
- getPermissions() {
175
- var _this7 = this;
176
-
177
- return _asyncToGenerator(function* () {
178
- return _this7.items.reduce((filtered, item) => {
179
- const {
180
- instance
181
- } = item;
182
-
183
- if (instance instanceof _Permission.default) {
184
- filtered.push(instance);
124
+ async getGrants(role) {
125
+ if (role && this.items[role]) {
126
+ const currentGrants = this.items[role].grants;
127
+ return currentGrants.reduce((filtered, grantName) => {
128
+ const grant = this.items[grantName];
129
+ if (grant) {
130
+ filtered.push(grant.instance);
185
131
  }
186
-
187
132
  return filtered;
188
133
  }, []);
189
- })();
190
- }
191
-
192
- getGrants(role) {
193
- var _this8 = this;
194
-
195
- return _asyncToGenerator(function* () {
196
- if (role && _this8.items[role]) {
197
- const currentGrants = _this8.items[role].grants;
198
- return currentGrants.reduce((filtered, grantName) => {
199
- const grant = _this8.items[grantName];
200
-
201
- if (grant) {
202
- filtered.push(grant.instance);
203
- }
204
-
205
- return filtered;
206
- }, []);
207
- }
208
-
209
- return [];
210
- })();
134
+ }
135
+ return [];
211
136
  }
212
-
213
137
  }
214
-
215
138
  exports.default = Memory;
216
139
  //# sourceMappingURL=Memory.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Memory.js"],"names":["Memory","Storage","add","item","name","items","Error","instance","grants","remove","Object","keys","forEach","itemName","filter","grant","role","child","childName","Role","includes","push","revoke","get","undefined","getRoles","reduce","filtered","getPermissions","Permission","getGrants","currentGrants","grantName"],"mappings":";;;;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;AAEe,MAAMA,MAAN,SAAqBC,gBAArB,CAA6B;AAAA;AAAA;;AAAA,mCACxB,EADwB;AAAA;;AAGpCC,EAAAA,GAAN,CAAUC,IAAV,EAA+B;AAAA;;AAAA;AAC7B,YAAM;AAAEC,QAAAA;AAAF,UAAWD,IAAjB;;AACA,UAAI,KAAI,CAACE,KAAL,CAAWD,IAAX,CAAJ,EAAsB;AACpB,cAAM,IAAIE,KAAJ,CAAW,QAAOF,IAAK,iBAAvB,CAAN;AACD;;AAED,MAAA,KAAI,CAACC,KAAL,CAAWD,IAAX,IAAmB;AACjBG,QAAAA,QAAQ,EAAEJ,IADO;AAEjBK,QAAAA,MAAM,EAAE;AAFS,OAAnB;AAKA,aAAO,IAAP;AAX6B;AAY9B;;AAEKC,EAAAA,MAAN,CAAaN,IAAb,EAAkC;AAAA;;AAAA;AAChC,YAAM;AAAEE,QAAAA;AAAF,UAAY,MAAlB;AACA,YAAM;AAAED,QAAAA;AAAF,UAAWD,IAAjB;;AACA,UAAI,CAACE,KAAK,CAACD,IAAD,CAAV,EAAkB;AAChB,cAAM,IAAIE,KAAJ,CAAW,QAAOF,IAAK,8BAAvB,CAAN;AACD,OAL+B,CAOhC;;;AACAM,MAAAA,M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AAED,eAAOoB,QAAP;AACD,OATI,EASF,EATE,CAAP;AADuB;AAWxB;;AAEKC,EAAAA,cAAN,GAAqC;AAAA;;AAAA;AACnC,aAAO,MAAI,CAACvB,KAAL,CACJqB,MADI,CACG,CAACC,QAAD,EAAyBxB,IAAzB,KAA0C;AAChD,cAAM;AAAEI,UAAAA;AAAF,YAAeJ,IAArB;;AAEA,YAAII,QAAQ,YAAYsB,mBAAxB,EAAoC;AAClCF,UAAAA,QAAQ,CAACN,IAAT,CAAcd,QAAd;AACD;;AAED,eAAOoB,QAAP;AACD,OATI,EASF,EATE,CAAP;AADmC;AAWpC;;AAEKG,EAAAA,SAAN,CAAgBd,IAAhB,EAAsC;AAAA;;AAAA;AACpC,UAAIA,IAAI,IAAI,MAAI,CAACX,KAAL,CAAWW,IAAX,CAAZ,EAA8B;AAC5B,cAAMe,aAAa,GAAG,MAAI,CAAC1B,KAAL,CAAWW,IAAX,EAAiBR,MAAvC;AAEA,eAAOuB,aAAa,CAACL,MAAd,CAAqB,CAACC,QAAD,EAAqBK,SAArB,KAA2C;AACrE,gBAAMjB,KAAK,GAAG,MAAI,CAACV,KAAL,CAAW2B,SAAX,CAAd;;AACA,cAAIjB,KAAJ,EAAW;AACTY,YAAAA,QAAQ,CAACN,IAAT,CAAcN,KAAK,CAACR,QAApB;AACD;;AAED,iBAAOoB,QAAP;AACD,SAPM,EAOJ,EAPI,CAAP;AAQD;;AAED,aAAO,EAAP;AAdoC;AAerC;;AAlIyC","sourcesContent":["// @flow\nimport Storage from './Storage';\nimport Permission from './Permission';\nimport Role from './Role';\nimport Base from './Base';\n\nexport default class Memory extends Storage {\n items: Object[] = {};\n\n async add(item: Base): boolean {\n const { name } = item;\n if (this.items[name]) {\n throw new Error(`Item ${name} already exists`);\n }\n\n this.items[name] = {\n instance: item,\n grants: [],\n };\n\n return true;\n }\n\n async remove(item: Base): boolean {\n const { items } = this;\n const { name } = item;\n if (!items[name]) {\n throw new Error(`Item ${name} is not presented in storage`);\n }\n\n // revoke from all instances\n Object.keys(items).forEach((itemName: string) => {\n const { grants } = items[itemName];\n items[itemName].grants = grants.filter(grant => grant !== name);\n });\n\n // delete from items\n delete this.items[name];\n return true;\n }\n\n async grant(role: Role, child: Base): boolean {\n const { name } = role;\n const { name: childName } = child;\n\n if (!this.items[name]) {\n throw new Error(`Role ${name} is not exist`);\n }\n\n if (!this.items[childName]) {\n throw new Error(`Base ${childName} is not exist`);\n }\n\n if (!(role instanceof Role)) {\n throw new Error('Role is not instance of Role');\n }\n\n if (name === childName) {\n throw new Error(`You can grant yourself ${name}`);\n }\n\n const { grants } = this.items[name];\n if (!grants.includes(childName)) {\n grants.push(childName);\n }\n\n return true;\n }\n\n async revoke(role: Role, child: Base): boolean {\n const { name } = role;\n const { name: childName } = child;\n\n if (!this.items[name] || !this.items[childName]) {\n throw new Error('Role is not exist');\n }\n\n const { grants } = this.items[name];\n if (!grants.includes(childName)) {\n throw new Error('Item is not associated to this item');\n }\n\n this.items[name].grants = grants.filter(grant => grant !== childName);\n\n return true;\n }\n\n async get(name: string): ?Base {\n if (name && this.items[name]) {\n return this.items[name].instance;\n }\n\n return undefined;\n }\n\n async getRoles(): Role[] {\n return this.items\n .reduce((filtered: Role[], item: Object) => {\n const { instance } = item;\n\n if (instance instanceof Role) {\n filtered.push(instance);\n }\n\n return filtered;\n }, []);\n }\n\n async getPermissions(): Permission[] {\n return this.items\n .reduce((filtered: Permission[], item: Object) => {\n const { instance } = item;\n\n if (instance instanceof Permission) {\n filtered.push(instance);\n }\n\n return filtered;\n }, []);\n }\n\n async getGrants(role: string): Base[] {\n if (role && this.items[role]) {\n const currentGrants = this.items[role].grants;\n\n return currentGrants.reduce((filtered: Object[], grantName: string) => {\n const grant = this.items[grantName];\n if (grant) {\n filtered.push(grant.instance);\n }\n\n return filtered;\n }, []);\n }\n\n return [];\n }\n}\n"],"file":"Memory.js"}
1
+ {"version":3,"file":"Memory.js","names":["_Storage","_interopRequireDefault","require","_Permission","_Role","_Base","e","__esModule","default","Memory","Storage","items","add","item","name","Error","instance","grants","remove","Object","keys","forEach","itemName","filter","grant","role","child","childName","Role","includes","push","revoke","get","undefined","getRoles","reduce","filtered","getPermissions","Permission","getGrants","currentGrants","grantName","exports"],"sources":["../src/Memory.js"],"sourcesContent":["// @flow\nimport Storage from './Storage';\nimport Permission from './Permission';\nimport Role from './Role';\nimport Base from './Base';\n\nexport default class Memory extends Storage {\n items: Object[] = {};\n\n async add(item: Base): boolean {\n const { name } = item;\n if (this.items[name]) {\n throw new Error(`Item ${name} already exists`);\n }\n\n this.items[name] = {\n instance: item,\n grants: [],\n };\n\n return true;\n }\n\n async remove(item: Base): boolean {\n const { items } = this;\n const { name } = item;\n if (!items[name]) {\n throw new Error(`Item ${name} is not presented in storage`);\n }\n\n // revoke from all instances\n Object.keys(items).forEach((itemName: string) => {\n const { grants } = items[itemName];\n items[itemName].grants = grants.filter(grant => grant !== name);\n });\n\n // delete from items\n delete this.items[name];\n return true;\n }\n\n async grant(role: Role, child: Base): boolean {\n const { name } = role;\n const { name: childName } = child;\n\n if (!this.items[name]) {\n throw new Error(`Role ${name} is not exist`);\n }\n\n if (!this.items[childName]) {\n throw new Error(`Base ${childName} is not exist`);\n }\n\n if (!(role instanceof Role)) {\n throw new Error('Role is not instance of Role');\n }\n\n if (name === childName) {\n throw new Error(`You can grant yourself ${name}`);\n }\n\n const { grants } = this.items[name];\n if (!grants.includes(childName)) {\n grants.push(childName);\n }\n\n return true;\n }\n\n async revoke(role: Role, child: Base): boolean {\n const { name } = role;\n const { name: childName } = child;\n\n if (!this.items[name] || !this.items[childName]) {\n throw new Error('Role is not exist');\n }\n\n const { grants } = this.items[name];\n if (!grants.includes(childName)) {\n throw new Error('Item is not associated to this item');\n }\n\n this.items[name].grants = grants.filter(grant => grant !== childName);\n\n return true;\n }\n\n async get(name: string): ?Base {\n if (name && this.items[name]) {\n return this.items[name].instance;\n }\n\n return undefined;\n }\n\n async getRoles(): Role[] {\n return this.items\n .reduce((filtered: Role[], item: Object) => {\n const { instance } = item;\n\n if (instance instanceof Role) {\n filtered.push(instance);\n }\n\n return filtered;\n }, []);\n }\n\n async getPermissions(): Permission[] {\n return this.items\n .reduce((filtered: Permission[], item: Object) => {\n const { instance } = item;\n\n if (instance instanceof Permission) {\n filtered.push(instance);\n }\n\n return filtered;\n }, []);\n }\n\n async getGrants(role: string): Base[] {\n if (role && this.items[role]) {\n const currentGrants = this.items[role].grants;\n\n return currentGrants.reduce((filtered: Object[], grantName: string) => {\n const grant = this.items[grantName];\n if (grant) {\n filtered.push(grant.instance);\n }\n\n return filtered;\n }, []);\n }\n\n return [];\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,QAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,WAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,KAAA,GAAAH,sBAAA,CAAAC,OAAA;AACA,IAAAG,KAAA,GAAAJ,sBAAA,CAAAC,OAAA;AAA0B,SAAAD,uBAAAK,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAEX,MAAMG,MAAM,SAASC,gBAAO,CAAC;EAC1CC,KAAK,GAAa,CAAC,CAAC;EAEpB,MAAMC,GAAGA,CAACC,IAAU,EAAW;IAC7B,MAAM;MAAEC;IAAK,CAAC,GAAGD,IAAI;IACrB,IAAI,IAAI,CAACF,KAAK,CAACG,IAAI,CAAC,EAAE;MACpB,MAAM,IAAIC,KAAK,CAAC,QAAQD,IAAI,iBAAiB,CAAC;IAChD;IAEA,IAAI,CAACH,KAAK,CAACG,IAAI,CAAC,GAAG;MACjBE,QAAQ,EAAEH,IAAI;MACdI,MAAM,EAAE;IACV,CAAC;IAED,OAAO,IAAI;EACb;EAEA,MAAMC,MAAMA,CAACL,IAAU,EAAW;IAChC,MAAM;MAAEF;IAAM,CAAC,GAAG,IAAI;IACtB,MAAM;MAAEG;IAAK,CAAC,GAAGD,IAAI;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