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 |
1×
1×
6×
1×
1×
1×
47×
47×
1×
1×
1×
1×
1×
47×
47×
47×
47×
47×
1×
15×
15×
10×
10×
10×
15×
8×
6×
6×
6×
8×
101×
41×
41×
41×
132×
132×
1×
1× | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DELIMITER = undefined;
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 _Base2 = require('./Base');
var _Base3 = _interopRequireDefault(_Base2);
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 DELIMITER = exports.DELIMITER = '_';
var Permission = function (_Base) {
_inherits(Permission, _Base);
/**
* Permission constructor
* @constructor Permission
* @extends {Base}
* @param {RBAC} rbac Instance of the RBAC
* @param {String} action Name of the action
* @param {String} resource Name of the resource
* @param {Boolean} [add=true] True if you need to save it to storage
* @param {Function} cb Callback function after add
*/
function Permission(rbac, action, resource, add, cb) {
_classCallCheck(this, Permission);
Iif (typeof add === 'function') {
cb = add;
add = true;
}
Iif (!action || !resource) {
var _ret;
return _ret = cb(new Error('One of parameters is undefined')), _possibleConstructorReturn(_this, _ret);
}
Iif (!Permission.isValidName(action) || !Permission.isValidName(resource)) {
var _ret2;
return _ret2 = cb(new Error('Action or resource has no valid name')), _possibleConstructorReturn(_this, _ret2);
}
return _possibleConstructorReturn(this, Object.getPrototypeOf(Permission).call(this, rbac, Permission.createName(action, resource), add, cb));
}
/**
* Get action name of actual permission
* @member Permission#action {String} Action of permission
*/
_createClass(Permission, [{
key: 'can',
/**
* Return true if has same action and resource
* @method Permission#can
* @param {String} action Name of action
* @param {String} resource Name of resource
* @return {Boolean}
*/
value: function can(action, resource) {
return this.action === action && this.resource === resource;
}
/**
* Compute name of permission from action and resource
* @function createName
* @memberof Permission
* @param {String} action Name of permission
* @param {String} resource Resource of permission
* @return {String} Computed name of permission
* @static
*/
}, {
key: 'action',
get: function get() {
if (!this._action) {
var decoded = Permission.decodeName(this.name);
Iif (!decoded) {
throw new Error('Action is null');
}
this._action = decoded.action;
}
return this._action;
}
/**
* Get resource name of actual permission
* @member Permission#resource {String} Resource of permission
*/
}, {
key: 'resource',
get: function get() {
if (!this._resource) {
var decoded = Permission.decodeName(this.name);
Iif (!decoded) {
throw new Error('Resource is null');
}
this._resource = decoded.resource;
}
return this._resource;
}
}], [{
key: 'createName',
value: function createName(action, resource) {
return action + DELIMITER + resource;
}
}, {
key: 'decodeName',
value: function decodeName(name) {
var pos = name.indexOf(DELIMITER);
Iif (pos === -1) {
return null;
}
return {
action: name.substr(0, pos),
resource: name.substr(pos + 1)
};
}
/**
* Correct name can contain only alphanumeric characters
* @function isValidName
* @memberof Permission
* @param {String} name Name
* @return {Boolean}
* @static
*/
}, {
key: 'isValidName',
value: function isValidName(name) {
Eif (/^[a-zA-Z0-9]+$/.test(name)) {
return true;
}
return false;
}
}]);
return Permission;
}(_Base3.default);
exports.default = Permission; |