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 |
1×
8×
1×
1×
1×
1×
1×
1×
3×
5×
5×
1×
1×
1×
1×
5×
5×
5×
5×
1×
41×
41×
41×
41×
41×
3×
3×
3×
18×
18×
18×
9×
2×
2×
3×
3×
3×
32×
32×
32×
32×
32×
32×
32×
25×
25×
3×
29×
29×
29×
1×
1×
1×
1×
1×
1×
1×
1×
1×
74×
5×
69×
69×
21×
21×
21×
21×
38×
38×
38×
38×
21×
21×
1×
1× | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _index = require('./index');
var _index2 = _interopRequireDefault(_index);
var _Permission = require('../Permission');
var _Permission2 = _interopRequireDefault(_Permission);
var _Role = require('../Role');
var _Role2 = _interopRequireDefault(_Role);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { Iif (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Memory = function (_Storage) {
_inherits(Memory, _Storage);
function Memory() {
_classCallCheck(this, Memory);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Memory).call(this));
_this._items = {};
return _this;
}
_createClass(Memory, [{
key: 'add',
value: function add(item, cb) {
var name = item.name;
Iif (this._items[name]) {
return cb(null, this._items[name].item);
}
this._items[name] = {
instance: item,
grants: []
};
cb(null, item);
return this;
}
}, {
key: 'remove',
value: function remove(item, cb) {
var name = item.name;
Iif (!this._items[name]) {
return cb(new Error('Item is not presented in storage'));
}
// revoke from all instances
for (var index in this._items) {
Iif (!this._items.hasOwnProperty(index)) {
continue;
}
var grants = this._items[index].grants;
for (var i = 0; i < grants.length; i++) {
if (grants[i] === name) {
grants.splice(i, 1);
break;
}
}
}
// delete from items
delete this._items[name];
cb(null, true);
return this;
}
}, {
key: 'grant',
value: function grant(role, child, cb) {
var name = role.name;
var childName = child.name;
Iif (!this._items[name] || !this._items[childName]) {
return cb(new Error('Role is not exist'));
}
Iif (!role instanceof _Role2.default) {
return cb(new Error('Role is not instance of Role'));
}
Iif (name === childName) {
return cb(new Error('You can grant yourself'));
}
var grants = this._items[name].grants;
for (var i = 0; i < grants.length; i++) {
var grant = grants[i];
if (grant === childName) {
return cb(null, true);
}
}
grants.push(childName);
cb(null, true);
return this;
}
}, {
key: 'revoke',
value: function revoke(role, child, cb) {
var name = role.name;
var childName = child.name;
Iif (!this._items[name] || !this._items[childName]) {
return cb(new Error('Role is not exist'));
}
var grants = this._items[name].grants;
for (var i = 0; i < grants.length; i++) {
var grant = grants[i];
Eif (grant === childName) {
grants.splice(i, 1);
return cb(null, true);
}
}
cb(new Error('Item is not associated to this item'));
return this;
}
}, {
key: 'get',
value: function get(name, cb) {
if (!name || !this._items[name]) {
return cb(null, null);
}
cb(null, this._items[name].instance);
return this;
}
}, {
key: 'getRoles',
value: function getRoles(cb) {
var items = [];
for (var name in this._items) {
if (!this._items.hasOwnProperty(name)) {
continue;
}
var item = this._items[name].instance;
if (item instanceof _Role2.default) {
items.push(item);
}
}
cb(null, items);
return this;
}
}, {
key: 'getPermissions',
value: function getPermissions(cb) {
var items = [];
for (var name in this._items) {
if (!this._items.hasOwnProperty(name)) {
continue;
}
var item = this._items[name].instance;
if (item instanceof _Permission2.default) {
items.push(item);
}
}
cb(null, items);
return this;
}
}, {
key: 'getGrants',
value: function getGrants(role, cb) {
Iif (!role || !this._items[role]) {
return cb(null, null);
}
var roleGrants = this._items[role].grants;
var grants = [];
for (var i = 0; i < roleGrants.length; i++) {
var grantName = roleGrants[i];
var grant = this._items[grantName];
Iif (!grant) {
continue;
}
grants.push(grant.instance);
}
cb(null, grants);
return this;
}
}]);
return Memory;
}(_index2.default);
exports.default = Memory; |