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 +21 -3
- package/LICENSE +21 -0
- package/README.md +36 -82
- package/dist/Base.js +15 -31
- package/dist/Base.js.map +1 -1
- package/dist/Memory.js +113 -190
- package/dist/Memory.js.map +1 -1
- package/dist/Permission.js +11 -36
- package/dist/Permission.js.map +1 -1
- package/dist/RBAC.js +238 -458
- package/dist/RBAC.js.map +1 -1
- package/dist/Role.js +26 -70
- package/dist/Role.js.map +1 -1
- package/dist/Storage.js +55 -119
- package/dist/Storage.js.map +1 -1
- package/dist/index.js +46 -27
- package/dist/index.js.map +1 -1
- package/package.json +15 -60
- package/.babelrc +0 -32
- package/.eslintignore +0 -7
- package/.eslintrc +0 -19
- package/.travis.yml +0 -16
- package/dist/RBAC.test.js +0 -360
- package/dist/RBAC.test.js.map +0 -1
- package/gulpfile.js +0 -10
- package/jsdocConfig.js +0 -20
package/HISTORY.md
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# v6.0.0
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
5
|
-
[![build status][travis-image]][travis-url]
|
|
6
|
-
[![Test coverage][coveralls-image]][coveralls-url]
|
|
7
|
-
[](https://gitter.im/seeden/rbac)
|
|
3
|
+
Hierarchical Role Based Access Control for Node.js
|
|
8
4
|
|
|
9
|
-
[
|
|
10
|
-
[
|
|
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
|
+
[](https://www.npmjs.com/package/rbac)
|
|
6
|
+
[](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';
|
|
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
|
-
|
|
49
|
+
```js
|
|
99
50
|
const admin = await rbac.getRole('admin');
|
|
100
51
|
if (!admin) {
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
##
|
|
61
|
+
## Custom storage
|
|
111
62
|
|
|
112
|
-
|
|
63
|
+
RBAC uses in-memory storage by default. You can implement custom storage by extending the `Storage` class:
|
|
113
64
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
123
|
-
npm run test
|
|
79
|
+
const rbac = new RBAC({ storage: new MyStorage() });
|
|
124
80
|
```
|
|
125
81
|
|
|
126
|
-
##
|
|
82
|
+
## Running Tests
|
|
127
83
|
|
|
128
84
|
```sh
|
|
129
|
-
npm
|
|
85
|
+
npm test
|
|
130
86
|
```
|
|
131
87
|
|
|
132
88
|
## Credits
|
|
133
89
|
|
|
134
|
-
|
|
90
|
+
[Zlatko Fedor](https://github.com/seeden)
|
|
135
91
|
|
|
136
92
|
## License
|
|
137
93
|
|
|
138
|
-
|
|
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
|
|
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
|
-
|
|
34
|
-
|
|
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
|
-
|
|
51
|
-
|
|
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,"
|
|
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
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
35
|
-
} =
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
45
|
-
return true;
|
|
46
|
-
})();
|
|
46
|
+
// delete from items
|
|
47
|
+
delete this.items[name];
|
|
48
|
+
return true;
|
|
47
49
|
}
|
|
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
|
-
})();
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
133
|
-
} =
|
|
134
|
-
|
|
135
|
-
|
|
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
|
-
|
|
140
|
-
return true;
|
|
141
|
-
})();
|
|
110
|
+
return filtered;
|
|
111
|
+
}, []);
|
|
142
112
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (
|
|
149
|
-
|
|
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
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
package/dist/Memory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
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,C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