kuzzle 2.16.11 → 2.17.2

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 (187) hide show
  1. package/lib/api/controllers/adminController.js +3 -3
  2. package/lib/api/controllers/authController.js +12 -12
  3. package/lib/api/controllers/baseController.js +60 -3
  4. package/lib/api/controllers/clusterController.js +1 -1
  5. package/lib/api/controllers/collectionController.js +7 -5
  6. package/lib/api/controllers/documentController.js +130 -17
  7. package/lib/api/controllers/indexController.js +1 -1
  8. package/lib/api/controllers/memoryStorageController.js +39 -38
  9. package/lib/api/controllers/realtimeController.js +1 -1
  10. package/lib/api/controllers/securityController.js +50 -50
  11. package/lib/api/controllers/serverController.js +73 -27
  12. package/lib/api/documentExtractor.js +3 -3
  13. package/lib/api/funnel.js +44 -21
  14. package/lib/api/httpRoutes.js +9 -4
  15. package/lib/api/openapi/OpenApiManager.d.ts +11 -0
  16. package/lib/api/openapi/OpenApiManager.js +96 -0
  17. package/lib/api/openapi/{document → components/document}/count.yaml +2 -2
  18. package/lib/api/openapi/{document → components/document}/create.yaml +2 -2
  19. package/lib/api/openapi/{document → components/document}/createOrReplace.yaml +2 -2
  20. package/lib/api/openapi/{document → components/document}/delete.yaml +1 -1
  21. package/lib/api/openapi/{document → components/document}/deleteByQuery.yaml +2 -2
  22. package/lib/api/openapi/{document → components/document}/exists.yaml +1 -1
  23. package/lib/api/openapi/{document → components/document}/get.yaml +1 -1
  24. package/lib/api/openapi/{document → components/document}/index.d.ts +2 -0
  25. package/lib/api/openapi/{document → components/document}/index.js +7 -2
  26. package/lib/api/openapi/{document → components/document}/replace.yaml +2 -2
  27. package/lib/api/openapi/{document → components/document}/scroll.yaml +1 -1
  28. package/lib/api/openapi/{document → components/document}/update.yaml +2 -2
  29. package/lib/api/openapi/components/document/validate.yaml +42 -0
  30. package/lib/api/openapi/components/index.d.ts +2 -0
  31. package/lib/api/openapi/components/index.js +18 -0
  32. package/lib/api/openapi/{payloads.yaml → components/payloads.yaml} +0 -0
  33. package/lib/api/openapi/index.d.ts +1 -2
  34. package/lib/api/openapi/index.js +1 -5
  35. package/lib/api/openapi/openApiGenerator.d.ts +7 -0
  36. package/lib/api/openapi/openApiGenerator.js +133 -0
  37. package/lib/api/request/kuzzleRequest.js +8 -6
  38. package/lib/cluster/node.js +9 -9
  39. package/lib/cluster/publisher.js +1 -1
  40. package/lib/cluster/state.js +20 -4
  41. package/lib/cluster/subscriber.js +1 -1
  42. package/lib/cluster/workers/IDCardRenewer.js +2 -2
  43. package/lib/config/default.config.js +1 -0
  44. package/lib/config/index.js +6 -6
  45. package/lib/core/auth/passportResponse.js +6 -6
  46. package/lib/core/auth/passportWrapper.js +5 -5
  47. package/lib/core/backend/backend.d.ts +11 -3
  48. package/lib/core/backend/backend.js +22 -17
  49. package/lib/core/backend/backendConfig.d.ts +5 -1
  50. package/lib/core/backend/backendConfig.js +25 -2
  51. package/lib/core/backend/backendController.js +21 -5
  52. package/lib/core/backend/backendErrors.d.ts +58 -0
  53. package/lib/core/backend/backendErrors.js +121 -0
  54. package/lib/core/backend/backendHook.js +21 -5
  55. package/lib/core/backend/backendImport.js +21 -5
  56. package/lib/core/backend/backendOpenApi.d.ts +9 -0
  57. package/lib/core/backend/backendOpenApi.js +69 -0
  58. package/lib/core/backend/backendPipe.js +21 -5
  59. package/lib/core/backend/backendPlugin.js +22 -3
  60. package/lib/core/backend/backendVault.js +21 -2
  61. package/lib/core/backend/index.d.ts +2 -0
  62. package/lib/core/backend/index.js +2 -0
  63. package/lib/core/network/accessLogger.js +6 -6
  64. package/lib/core/network/clientConnection.js +1 -1
  65. package/lib/core/network/entryPoint.js +5 -5
  66. package/lib/core/network/httpRouter/index.js +5 -5
  67. package/lib/core/network/httpRouter/routeHandler.js +3 -3
  68. package/lib/core/network/httpRouter/routePart.js +5 -5
  69. package/lib/core/network/protocolManifest.js +1 -1
  70. package/lib/core/network/protocols/httpMessage.js +2 -2
  71. package/lib/core/network/protocols/httpwsProtocol.js +228 -50
  72. package/lib/core/network/protocols/mqttProtocol.js +3 -3
  73. package/lib/core/network/protocols/protocol.js +3 -3
  74. package/lib/core/network/router.js +7 -6
  75. package/lib/core/plugin/plugin.js +38 -64
  76. package/lib/core/plugin/pluginContext.js +22 -3
  77. package/lib/core/plugin/pluginManifest.js +3 -3
  78. package/lib/core/plugin/pluginRepository.js +5 -5
  79. package/lib/core/plugin/pluginsManager.js +29 -28
  80. package/lib/core/realtime/channel.js +20 -4
  81. package/lib/core/realtime/hotelClerk.js +24 -5
  82. package/lib/core/realtime/notification/server.js +1 -1
  83. package/lib/core/realtime/notification/user.js +1 -1
  84. package/lib/core/realtime/notifier.js +5 -5
  85. package/lib/core/security/index.js +1 -1
  86. package/lib/core/security/profileRepository.d.ts +176 -0
  87. package/lib/core/security/profileRepository.js +445 -443
  88. package/lib/core/security/roleRepository.js +16 -16
  89. package/lib/core/security/securityLoader.js +2 -2
  90. package/lib/core/security/tokenRepository.js +11 -11
  91. package/lib/core/security/userRepository.js +8 -8
  92. package/lib/core/shared/abstractManifest.js +4 -4
  93. package/lib/core/shared/repository.js +5 -5
  94. package/lib/core/shared/sdk/embeddedSdk.js +21 -2
  95. package/lib/core/shared/sdk/funnelProtocol.js +1 -1
  96. package/lib/core/shared/sdk/impersonatedSdk.js +1 -1
  97. package/lib/core/shared/store.js +30 -23
  98. package/lib/core/statistics/statistics.js +17 -17
  99. package/lib/core/storage/clientAdapter.js +45 -10
  100. package/lib/core/storage/indexCache.js +20 -4
  101. package/lib/core/validation/baseType.js +5 -5
  102. package/lib/core/validation/types/anything.js +1 -1
  103. package/lib/core/validation/types/boolean.js +2 -2
  104. package/lib/core/validation/types/date.js +9 -9
  105. package/lib/core/validation/types/email.js +5 -5
  106. package/lib/core/validation/types/enum.js +6 -6
  107. package/lib/core/validation/types/geoPoint.js +2 -2
  108. package/lib/core/validation/types/geoShape.js +28 -25
  109. package/lib/core/validation/types/integer.js +4 -4
  110. package/lib/core/validation/types/ipAddress.js +7 -6
  111. package/lib/core/validation/types/numeric.js +4 -4
  112. package/lib/core/validation/types/object.js +5 -5
  113. package/lib/core/validation/types/string.js +5 -5
  114. package/lib/core/validation/types/url.js +7 -6
  115. package/lib/core/validation/validation.js +95 -84
  116. package/lib/kerror/codes/1-services.json +12 -0
  117. package/lib/kerror/codes/2-api.json +12 -0
  118. package/lib/kerror/codes/3-network.json +12 -0
  119. package/lib/kerror/codes/4-plugin.json +6 -0
  120. package/lib/kerror/codes/index.js +11 -11
  121. package/lib/kerror/errors/multipleErrorsError.d.ts +1 -1
  122. package/lib/kerror/errors/multipleErrorsError.js +3 -3
  123. package/lib/kerror/index.d.ts +82 -0
  124. package/lib/kerror/index.js +176 -143
  125. package/lib/kuzzle/dumpGenerator.js +3 -3
  126. package/lib/kuzzle/event/kuzzleEventEmitter.js +4 -4
  127. package/lib/kuzzle/event/pipeRunner.js +1 -1
  128. package/lib/kuzzle/event/waterfall.js +6 -6
  129. package/lib/kuzzle/kuzzle.js +59 -9
  130. package/lib/kuzzle/log.js +3 -3
  131. package/lib/kuzzle/vault.js +3 -3
  132. package/lib/model/security/profile.d.ts +54 -0
  133. package/lib/model/security/profile.js +192 -232
  134. package/lib/model/security/rights.js +1 -1
  135. package/lib/model/security/role.d.ts +40 -0
  136. package/lib/model/security/role.js +174 -190
  137. package/lib/model/security/user.d.ts +29 -0
  138. package/lib/model/security/user.js +103 -52
  139. package/lib/model/storage/apiKey.js +2 -2
  140. package/lib/model/storage/baseModel.js +3 -3
  141. package/lib/service/cache/redis.js +7 -7
  142. package/lib/service/storage/elasticsearch.js +152 -90
  143. package/lib/service/storage/esWrapper.js +2 -3
  144. package/lib/types/ControllerDefinition.d.ts +3 -3
  145. package/lib/types/ControllerRights.d.ts +22 -0
  146. package/lib/types/ControllerRights.js +23 -0
  147. package/lib/types/HttpStream.d.ts +32 -0
  148. package/lib/types/HttpStream.js +70 -0
  149. package/lib/types/OpenApiDefinition.d.ts +43 -0
  150. package/lib/types/{config/StorageService/StorageServiceElasticsearchConfiguration.js → OpenApiDefinition.js} +1 -1
  151. package/lib/types/Plugin.js +20 -4
  152. package/lib/types/Policy.d.ts +25 -0
  153. package/lib/types/{InternalLogger.js → Policy.js} +2 -2
  154. package/lib/types/PolicyRestrictions.d.ts +21 -0
  155. package/lib/types/PolicyRestrictions.js +23 -0
  156. package/lib/types/Target.d.ts +15 -0
  157. package/lib/types/Target.js +23 -0
  158. package/lib/types/config/KuzzleConfiguration.d.ts +4 -0
  159. package/lib/types/config/ServicesConfiguration.d.ts +2 -2
  160. package/lib/types/config/{StorageService/StorageServiceElasticsearchConfiguration.d.ts → storageEngine/StorageEngineElasticsearchConfiguration.d.ts} +10 -3
  161. package/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.js +3 -0
  162. package/lib/types/errors/ErrorDefinition.d.ts +27 -0
  163. package/lib/types/errors/ErrorDefinition.js +3 -0
  164. package/lib/types/errors/ErrorDomains.d.ts +17 -0
  165. package/lib/types/errors/ErrorDomains.js +3 -0
  166. package/lib/types/index.d.ts +9 -1
  167. package/lib/types/index.js +9 -1
  168. package/lib/util/array.d.ts +11 -0
  169. package/lib/util/array.js +57 -0
  170. package/lib/util/assertType.js +6 -6
  171. package/lib/util/bufferedPassThrough.d.ts +76 -0
  172. package/lib/util/bufferedPassThrough.js +161 -0
  173. package/lib/util/deprecate.js +7 -5
  174. package/lib/util/didYouMean.js +1 -1
  175. package/lib/util/dump-collection.d.ts +3 -0
  176. package/lib/util/dump-collection.js +284 -0
  177. package/lib/util/extractFields.js +2 -2
  178. package/lib/util/inflector.d.ts +8 -0
  179. package/lib/util/inflector.js +16 -0
  180. package/lib/util/mutex.js +21 -2
  181. package/lib/util/requestAssertions.js +7 -7
  182. package/lib/util/wildcard.js +55 -0
  183. package/package-lock.json +535 -75
  184. package/package.json +5 -3
  185. package/lib/api/openApiGenerator.d.ts +0 -7
  186. package/lib/api/openApiGenerator.js +0 -197
  187. package/lib/types/InternalLogger.d.ts +0 -25
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /*
2
3
  * Kuzzle, a backend software, self-hostable and ready to use
3
4
  * to power modern apps
@@ -18,259 +19,218 @@
18
19
  * See the License for the specific language governing permissions and
19
20
  * limitations under the License.
20
21
  */
21
-
22
- 'use strict';
23
-
24
- const _ = require('lodash');
25
- const Bluebird = require('bluebird');
26
-
27
- const Rights = require('./rights');
28
- const kerror = require('../../kerror');
29
- const { isPlainObject } = require('../../util/safeObject');
30
-
22
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
25
+ }) : (function(o, m, k, k2) {
26
+ if (k2 === undefined) k2 = k;
27
+ o[k2] = m[k];
28
+ }));
29
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
30
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
31
+ }) : function(o, v) {
32
+ o["default"] = v;
33
+ });
34
+ var __importStar = (this && this.__importStar) || function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ var __importDefault = (this && this.__importDefault) || function (mod) {
42
+ return (mod && mod.__esModule) ? mod : { "default": mod };
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.Profile = void 0;
46
+ const lodash_1 = __importDefault(require("lodash"));
47
+ const bluebird_1 = __importDefault(require("bluebird"));
48
+ const rights_1 = __importDefault(require("./rights"));
49
+ const kerror = __importStar(require("../../kerror"));
50
+ const safeObject_1 = require("../../util/safeObject");
31
51
  const assertionError = kerror.wrap('api', 'assert');
32
-
33
52
  /**
34
53
  * @class Profile
35
54
  */
36
55
  class Profile {
37
- constructor() {
38
- this._id = null;
39
- this.policies = [];
40
- this.rateLimit = 0;
41
- }
42
-
43
- /**
44
- * @param {Kuzzle} kuzzle
45
- *
46
- * @returns {Promise}
47
- */
48
- async getPolicies() {
49
- if (!global.kuzzle) {
50
- throw kerror.get('security', 'profile', 'uninitialized', this._id);
56
+ constructor() {
57
+ this._id = null;
58
+ this.policies = [];
59
+ this.optimizedPolicies = [];
60
+ this.rateLimit = 0;
51
61
  }
52
-
53
- return Bluebird.map(this.policies, async ({restrictedTo, roleId}) => {
54
- const role = await global.kuzzle.ask('core:security:role:get', roleId);
55
- return {restrictedTo, role};
56
- });
57
- }
58
-
59
- /**
60
- * @param {Request} request
61
- * @param {Kuzzle} kuzzle
62
- * @returns {Promise<boolean>}
63
- */
64
- async isActionAllowed(request) {
65
- if (this.policies === undefined || this.policies.length === 0) {
66
- return false;
67
- }
68
-
69
- const policies = await this.getPolicies();
70
-
71
- const results = await Bluebird.map(
72
- policies,
73
- policy => policy.role.isActionAllowed(request, policy.restrictedTo));
74
-
75
- return results.includes(true);
76
- }
77
-
78
- /**
79
- * Validates the Profile format
80
- *
81
- * @param {Object} [options]
82
- * @param {boolean} [options.strict] - If true, only allows resctrictions on
83
- * existing indexes/collections
84
- * @returns {Promise}
85
- */
86
- async validateDefinition({ strict = false } = {}) {
87
- this.validateRateLimit();
88
-
89
- if (!this.policies) {
90
- throw assertionError.get('missing_argument', `${this._id}.policies`);
62
+ /**
63
+ * @param {Kuzzle} kuzzle
64
+ *
65
+ * @returns {Promise}
66
+ */
67
+ async getPolicies() {
68
+ if (!global.kuzzle) {
69
+ throw kerror.get('security', 'profile', 'uninitialized', this._id);
70
+ }
71
+ return bluebird_1.default.map(this.optimizedPolicies, async ({ restrictedTo, roleId }) => {
72
+ const role = await global.kuzzle.ask('core:security:role:get', roleId);
73
+ return { restrictedTo, role };
74
+ });
91
75
  }
92
-
93
- if (!Array.isArray(this.policies)) {
94
- throw assertionError.get('invalid_type', `${this._id}.policies`, 'object[]');
76
+ /**
77
+ * @param {Request} request
78
+ * @returns {Promise}
79
+ */
80
+ async getAllowedPolicies(request) {
81
+ if (this.optimizedPolicies === undefined || this.optimizedPolicies.length === 0) {
82
+ return [];
83
+ }
84
+ const policies = await this.getPolicies();
85
+ return policies.filter(policy => policy.role.isActionAllowed(request));
95
86
  }
96
-
97
- if (this.policies.length === 0) {
98
- throw assertionError.get('empty_argument', `${this._id}.policies`);
87
+ /**
88
+ * @param {Request} request
89
+ * @returns {Promise<boolean>}
90
+ */
91
+ async isActionAllowed(request) {
92
+ if (this.optimizedPolicies === undefined || this.optimizedPolicies.length === 0) {
93
+ return false;
94
+ }
95
+ const allowedPolicies = await this.getAllowedPolicies(request);
96
+ return allowedPolicies
97
+ .some(policy => policy.role.checkRestrictions(request.input.args.index, request.input.args.collection, policy.restrictedTo));
99
98
  }
100
-
101
- let i = 0;
102
- for (const policy of this.policies) {
103
- if (!policy.roleId) {
104
- throw assertionError.get('missing_argument', `${this._id}.policies[${i}].roleId`);
105
- }
106
-
107
- for (const member of Object.keys(policy)) {
108
- if (member !== 'roleId' && member !== 'restrictedTo') {
109
- throw assertionError.get(
110
- 'unexpected_argument',
111
- `${this._id}.policies[${i}].${member}`,
112
- '"roleId", "restrictedTo"');
99
+ /**
100
+ * Validates the Profile format
101
+ *
102
+ * @param {Object} [options]
103
+ * @param {boolean} [options.strict] - If true, only allows resctrictions on
104
+ * existing indexes/collections
105
+ * @returns {Promise}
106
+ */
107
+ async validateDefinition({ strict = false } = {}) {
108
+ this.validateRateLimit();
109
+ if (!this.policies) {
110
+ throw assertionError.get('missing_argument', `${this._id}.policies`);
113
111
  }
114
- }
115
-
116
- if (policy.restrictedTo) {
117
- if (!Array.isArray(policy.restrictedTo)) {
118
- throw assertionError.get(
119
- 'invalid_type',
120
- `${this._id}.policies[${i}].restrictedTo`,
121
- 'object[]');
112
+ if (!Array.isArray(this.policies)) {
113
+ throw assertionError.get('invalid_type', `${this._id}.policies`, 'object[]');
122
114
  }
123
-
124
- let j = 0;
125
- for (const restriction of policy.restrictedTo) {
126
- if (!isPlainObject(restriction)) {
127
- throw assertionError.get(
128
- 'invalid_type',
129
- `${this._id}.policies[${i}].restrictedTo[${restriction}]`,
130
- 'object');
131
- }
132
-
133
- if (restriction.index === null || restriction.index === undefined) {
134
- throw assertionError.get(
135
- 'missing_argument',
136
- `${this._id}.policies[${i}].restrictedTo[${j}].index`);
137
- }
138
-
139
- if (strict) {
140
- const indexExists = await global.kuzzle.ask(
141
- 'core:storage:public:index:exist',
142
- restriction.index);
143
-
144
- if (!indexExists) {
145
- throw kerror.get(
146
- 'services',
147
- 'storage',
148
- 'unknown_index',
149
- restriction.index);
150
- }
151
- }
152
-
153
- if ( restriction.collections !== undefined
154
- && restriction.collections !== null
155
- ) {
156
- if (!Array.isArray(restriction.collections)) {
157
- throw assertionError.get(
158
- 'invalid_type',
159
- `${this._id}.policies[${i}].restrictedTo[${j}].collections`,
160
- 'string[]');
115
+ if (this.policies.length === 0) {
116
+ throw assertionError.get('empty_argument', `${this._id}.policies`);
117
+ }
118
+ let i = 0;
119
+ for (const policy of this.policies) {
120
+ if (!policy.roleId) {
121
+ throw assertionError.get('missing_argument', `${this._id}.policies[${i}].roleId`);
161
122
  }
162
-
163
- if (strict) {
164
- const invalidCollections = [];
165
- for (const collection of restriction.collections) {
166
- const isValid = await global.kuzzle.ask(
167
- 'core:storage:public:collection:exist',
168
- restriction.index,
169
- collection);
170
-
171
- if (!isValid) {
172
- invalidCollections.push(collection);
123
+ for (const member of Object.keys(policy)) {
124
+ if (member !== 'roleId' && member !== 'restrictedTo') {
125
+ throw assertionError.get('unexpected_argument', `${this._id}.policies[${i}].${member}`, '"roleId", "restrictedTo"');
173
126
  }
174
- }
175
-
176
- if (invalidCollections.length > 0) {
177
- throw kerror.get(
178
- 'services',
179
- 'storage',
180
- 'unknown_collection',
181
- restriction.index,
182
- invalidCollections);
183
- }
184
127
  }
185
- }
186
-
187
- for (const member of Object.keys(restriction)) {
188
- if (member !== 'index' && member !== 'collections') {
189
- throw assertionError.get(
190
- 'unexpected_argument',
191
- `${this._id}.policies[${i}].restrictedTo[${j}].${member}`,
192
- '"index", "collections"');
128
+ if (policy.restrictedTo) {
129
+ if (!Array.isArray(policy.restrictedTo)) {
130
+ throw assertionError.get('invalid_type', `${this._id}.policies[${i}].restrictedTo`, 'object[]');
131
+ }
132
+ let j = 0;
133
+ for (const restriction of policy.restrictedTo) {
134
+ if (!(0, safeObject_1.isPlainObject)(restriction)) {
135
+ throw assertionError.get('invalid_type', `${this._id}.policies[${i}].restrictedTo[${restriction}]`, 'object');
136
+ }
137
+ if (restriction.index === null || restriction.index === undefined) {
138
+ throw assertionError.get('missing_argument', `${this._id}.policies[${i}].restrictedTo[${j}].index`);
139
+ }
140
+ if (strict) {
141
+ const indexExists = await global.kuzzle.ask('core:storage:public:index:exist', restriction.index);
142
+ if (!indexExists) {
143
+ throw kerror.get('services', 'storage', 'unknown_index', restriction.index);
144
+ }
145
+ }
146
+ if (restriction.collections !== undefined
147
+ && restriction.collections !== null) {
148
+ if (!Array.isArray(restriction.collections)) {
149
+ throw assertionError.get('invalid_type', `${this._id}.policies[${i}].restrictedTo[${j}].collections`, 'string[]');
150
+ }
151
+ if (strict) {
152
+ const invalidCollections = [];
153
+ for (const collection of restriction.collections) {
154
+ const isValid = await global.kuzzle.ask('core:storage:public:collection:exist', restriction.index, collection);
155
+ if (!isValid) {
156
+ invalidCollections.push(collection);
157
+ }
158
+ }
159
+ if (invalidCollections.length > 0) {
160
+ throw kerror.get('services', 'storage', 'unknown_collection', restriction.index, invalidCollections);
161
+ }
162
+ }
163
+ }
164
+ for (const member of Object.keys(restriction)) {
165
+ if (member !== 'index' && member !== 'collections') {
166
+ throw assertionError.get('unexpected_argument', `${this._id}.policies[${i}].restrictedTo[${j}].${member}`, '"index", "collections"');
167
+ }
168
+ }
169
+ j++;
170
+ }
193
171
  }
194
- }
195
-
196
- j++;
172
+ i++;
197
173
  }
198
- }
199
-
200
- i++;
174
+ return true;
201
175
  }
202
-
203
- return true;
204
- }
205
-
206
- /**
207
- * Resolves an array of rights related to the profile's roles.
208
- *
209
- * @returns {Promise}
210
- */
211
- async getRights () {
212
- const profileRights = {};
213
-
214
- const policies = await this.getPolicies();
215
-
216
- for (const policy of policies) {
217
- const role = policy.role;
218
- let restrictedTo = _.cloneDeep(policy.restrictedTo);
219
-
220
- if (restrictedTo === undefined || restrictedTo.length === 0) {
221
- restrictedTo = [{collections: ['*'], index: '*'}];
222
- }
223
-
224
- for (const [controller, rights] of Object.entries(role.controllers)) {
225
- for (const [action, actionRights] of Object.entries(rights.actions)) {
226
- for (const restriction of restrictedTo) {
227
- if (restriction.collections === undefined
228
- || restriction.collections.length === 0
229
- ) {
230
- restriction.collections = ['*'];
176
+ /**
177
+ * Resolves an array of rights related to the profile's roles.
178
+ *
179
+ * @returns {Promise}
180
+ */
181
+ async getRights() {
182
+ const profileRights = {};
183
+ const policies = await this.getPolicies();
184
+ for (const policy of policies) {
185
+ const role = policy.role;
186
+ let restrictedTo = lodash_1.default.cloneDeep(policy.restrictedTo);
187
+ if (restrictedTo === undefined || restrictedTo.size === 0) {
188
+ restrictedTo = new Map([['*', ['*']]]);
231
189
  }
232
-
233
- for (const collection of restriction.collections) {
234
- const rightsItem = {
235
- action,
236
- collection,
237
- controller,
238
- index: restriction.index,
239
- value: actionRights
240
- };
241
- const rightsObject = {
242
- [this.constructor._hash(rightsItem)]: rightsItem
243
- };
244
-
245
- _.assignWith(profileRights, rightsObject, Rights.merge);
190
+ for (const [controller, rights] of Object.entries(role.controllers)) {
191
+ for (const [action, actionRights] of Object.entries(rights.actions)) {
192
+ for (const [restrictedIndex, restrictedCollections] of restrictedTo.entries()) {
193
+ let collections = restrictedCollections;
194
+ if (restrictedCollections === undefined
195
+ || restrictedCollections.length === 0) {
196
+ collections = ['*'];
197
+ }
198
+ for (const collection of collections) {
199
+ const rightsItem = {
200
+ action,
201
+ collection,
202
+ controller,
203
+ index: restrictedIndex,
204
+ value: actionRights
205
+ };
206
+ const rightsObject = {
207
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
208
+ // @ts-ignore
209
+ [this.constructor._hash(rightsItem)]: rightsItem
210
+ };
211
+ lodash_1.default.assignWith(profileRights, rightsObject, rights_1.default.merge);
212
+ }
213
+ }
214
+ }
246
215
  }
247
- }
248
216
  }
249
- }
250
- }
251
-
252
- return profileRights;
253
- }
254
-
255
- static _hash () {
256
- return false;
257
- }
258
-
259
- validateRateLimit () {
260
- if (this.rateLimit === null || this.rateLimit === undefined) {
261
- this.rateLimit = 0;
217
+ return profileRights;
262
218
  }
263
-
264
- if ( typeof this.rateLimit !== 'number'
265
- || !Number.isInteger(this.rateLimit)
266
- ) {
267
- throw assertionError.get('invalid_type', 'rateLimit', 'integer');
219
+ static _hash() {
220
+ return false;
268
221
  }
269
-
270
- if (this.rateLimit < 0) {
271
- throw assertionError.get('invalid_argument', 'rateLimit', 'positive integer, or zero');
222
+ validateRateLimit() {
223
+ if (this.rateLimit === null || this.rateLimit === undefined) {
224
+ this.rateLimit = 0;
225
+ }
226
+ if (typeof this.rateLimit !== 'number'
227
+ || !Number.isInteger(this.rateLimit)) {
228
+ throw assertionError.get('invalid_type', 'rateLimit', 'integer');
229
+ }
230
+ if (this.rateLimit < 0) {
231
+ throw assertionError.get('invalid_argument', 'rateLimit', 'positive integer, or zero');
232
+ }
272
233
  }
273
- }
274
234
  }
275
-
276
- module.exports = Profile;
235
+ exports.Profile = Profile;
236
+ //# sourceMappingURL=profile.js.map
@@ -38,4 +38,4 @@ function merge (prev, cur) {
38
38
  return cur;
39
39
  }
40
40
 
41
- module.exports = {merge};
41
+ module.exports = { merge };
@@ -0,0 +1,40 @@
1
+ import { ControllerRight, ControllerRights } from '../../types/ControllerRights';
2
+ import { KuzzleRequest } from '../../../index';
3
+ import { OptimizedPolicyRestrictions } from '../../types/PolicyRestrictions';
4
+ /**
5
+ * @class Role
6
+ */
7
+ export declare class Role {
8
+ controllers: ControllerRights;
9
+ _id: string;
10
+ constructor();
11
+ /**
12
+ * @param {Request} request
13
+ * @returns {boolean}
14
+ */
15
+ isActionAllowed(request: KuzzleRequest): boolean;
16
+ /**
17
+ * @returns {Promise}
18
+ */
19
+ validateDefinition(): Promise<void>;
20
+ /**
21
+ * @param {String} index
22
+ * @param {String} collection
23
+ * @param {Map<string, string[]>} restrictedTo Restricted indexes
24
+ * @returns {Boolean} resolves to a Boolean value
25
+ */
26
+ checkRestrictions(index: string, collection: string, restrictedTo: OptimizedPolicyRestrictions): boolean;
27
+ /**
28
+ * Verifies that a controller rights definition is correct
29
+ *
30
+ * @param {Array.<string, Object>}
31
+ * @throws If the controller definition is invalid
32
+ */
33
+ validateControllerRights(name: string, controller: ControllerRight): void;
34
+ /**
35
+ * Checks if current role allows to log in
36
+ *
37
+ * @returns {boolean}
38
+ */
39
+ canLogIn(): boolean;
40
+ }