Skip to content

Commit 761cacd

Browse files
author
Gilles Ballini
authored
Merge pull request #99 from kuzzleio/KUZ-490_Repositories_refactor
Kuz 490 repositories refactor
2 parents d9c6c8f + 638ff41 commit 761cacd

File tree

12 files changed

+408
-846
lines changed

12 files changed

+408
-846
lines changed

dist/kuzzle.js

Lines changed: 185 additions & 301 deletions
Large diffs are not rendered by default.

dist/kuzzle.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/security/kuzzleProfile.js

Lines changed: 25 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var
2-
KuzzleSecurityDocument = require('./kuzzleSecurityDocument'),
3-
KuzzleRole = require('./kuzzleRole');
2+
KuzzleSecurityDocument = require('./kuzzleSecurityDocument');
43

54
function KuzzleProfile(kuzzleSecurity, id, content) {
65

@@ -17,17 +16,6 @@ function KuzzleProfile(kuzzleSecurity, id, content) {
1716
}
1817
});
1918

20-
// Hydrate profile with roles if roles are not only string but objects with `_id` and `_source`
21-
if (content && content.roles) {
22-
content.roles = content.roles.map(function (role) {
23-
if (!role._id || !role._source) {
24-
return role;
25-
}
26-
27-
return new KuzzleRole(kuzzleSecurity, role._id, role._source);
28-
});
29-
}
30-
3119
// promisifying
3220
if (kuzzleSecurity.kuzzle.bluebird) {
3321
return kuzzleSecurity.kuzzle.bluebird.promisifyAll(this, {
@@ -60,8 +48,8 @@ KuzzleProfile.prototype.save = function (options, cb) {
6048
data,
6149
self = this;
6250

63-
if (!this.content.roles) {
64-
throw new Error('Argument "roles" is mandatory in a profile. This argument contains an array of KuzzleRole or an array of id string');
51+
if (!this.content.policies) {
52+
throw new Error('Argument "policies" is mandatory in a profile. This argument contains an array of objects.');
6553
}
6654

6755
if (options && cb === undefined && typeof options === 'function') {
@@ -86,89 +74,49 @@ KuzzleProfile.prototype.save = function (options, cb) {
8674

8775

8876
/**
89-
* Add a role in the roles list
90-
* @param {KuzzleRole|string} role - can be an instance of KuzzleRole or an id in string
77+
* Add a policy in the policies list
78+
* @param {Object} policy - must be an object containing at least a "roleId" member which must be a string.
9179
*
9280
* @returns {KuzzleProfile} this
9381
*/
94-
KuzzleProfile.prototype.addRole = function (role) {
82+
KuzzleProfile.prototype.addPolicy = function (policy) {
9583

96-
if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
97-
throw new Error('Parameter "roles" must be a KuzzleRole or a id string');
84+
if (typeof policy !== 'object' || typeof policy.roleId !== 'string') {
85+
throw new Error('Parameter "policies" must be an object containing at least a "roleId" member which must be a string.');
9886
}
9987

100-
if (!this.content.roles) {
101-
this.content.roles = [];
88+
if (!this.content.policies) {
89+
this.content.policies = [];
10290
}
10391

104-
this.content.roles.push(role);
92+
this.content.policies.push(policy);
10593

10694
return this;
10795
};
10896

10997
/**
110-
* Set roles list
111-
* @param {Array} roles - can be an array of KuzzleRole or an array of string
98+
* Set policies list
99+
* @param {Array} policies - must be an array of objects containing at least a "roleId" member which must be a string
112100
*
113101
* @returns {KuzzleProfile} this
114102
*/
115-
KuzzleProfile.prototype.setRoles = function (roles) {
103+
KuzzleProfile.prototype.setPolicies = function (policies) {
116104

117-
if (!Array.isArray(roles)) {
118-
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
105+
if (!Array.isArray(policies)) {
106+
throw new Error('Parameter "policies" must be an array of objects containing at least a "roleId" member which must be a string');
119107
}
120108

121-
roles.map(function (role) {
122-
if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
123-
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
109+
policies.map(function (policy) {
110+
if (typeof policy !== 'object' || typeof policy.roleId !== 'string') {
111+
throw new Error('Parameter "policies" must be an array of objects containing at least a "roleId" member which must be a string');
124112
}
125113
});
126114

127-
this.content.roles = roles;
115+
this.content.policies = policies;
128116

129117
return this;
130118
};
131119

132-
133-
/**
134-
* Hydrate the profile - get real KuzzleRole and not just ids
135-
* Warning: do not try to hydrate a profile with newly added role which is not created in kuzzle
136-
*
137-
* @param {object} [options] - Optional parameters
138-
* @param {responseCallback} [cb] - Handles the query response
139-
*/
140-
KuzzleProfile.prototype.hydrate = function (options, cb) {
141-
142-
var
143-
self = this,
144-
data = {ids: []};
145-
146-
data.ids = this.content.roles.map(function (role) {
147-
if (typeof role === 'string') {
148-
return role;
149-
}
150-
151-
if (role instanceof KuzzleRole) {
152-
return role.id;
153-
}
154-
});
155-
156-
if (options && cb === undefined && typeof options === 'function') {
157-
cb = options;
158-
options = null;
159-
}
160-
161-
self.kuzzle.callbackRequired('KuzzleProfile.hydrate', cb);
162-
163-
self.kuzzle.query(self.kuzzleSecurity.buildQueryArgs('mGetRoles'), {body: data}, options, function (error, response) {
164-
if (error) {
165-
return cb(error);
166-
}
167-
168-
cb(null, new KuzzleProfile(self, self.id, {roles: response.result.hits}));
169-
});
170-
};
171-
172120
/**
173121
* Serialize this object into a JSON object
174122
*
@@ -183,29 +131,18 @@ KuzzleProfile.prototype.serialize = function () {
183131
}
184132

185133
data.body = this.content;
186-
if (!data.body.roles || !Array.isArray(data.body.roles)) {
187-
return data;
188-
}
189-
190-
data.body.roles = data.body.roles.map(function(role) {
191-
if (role instanceof KuzzleRole) {
192-
return role.id;
193-
}
194-
195-
return role;
196-
});
197134

198135
return data;
199136
};
200137

201138
/**
202-
* Returns the list of roles associated to this profile.
203-
* Each role element can be either a string or a KuzzleRole object
139+
* Returns the list of policies associated to this profile.
140+
* Each policy element is an array of objects containing at least a "roleId" member which must be a string
204141
*
205-
* @return {object} an array of roles
142+
* @return {object} an array of policies
206143
*/
207-
KuzzleProfile.prototype.getRoles = function () {
208-
return this.content.roles;
144+
KuzzleProfile.prototype.getPolicies = function () {
145+
return this.content.policies;
209146
};
210147

211148
module.exports = KuzzleProfile;

src/security/kuzzleSecurity.js

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ KuzzleSecurity.prototype.roleFactory = function(id, content) {
252252
/**
253253
* Get a specific profile from kuzzle
254254
*
255-
* Takes an optional argument object with the following property:
256-
* - hydrate (boolean, default: true):
257-
* if is set to false, return a list id in role instead of KuzzleRole.
258255
*
259256
* @param {string} id
260257
* @param {object} [options] - (optional) arguments
@@ -263,21 +260,18 @@ KuzzleSecurity.prototype.roleFactory = function(id, content) {
263260
KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
264261
var
265262
data,
266-
self = this,
267-
hydrate = true;
268-
269-
if (!id || typeof id !== 'string') {
270-
throw new Error('Id parameter is mandatory for getProfile function');
271-
}
263+
self = this;
272264

273265
if (!cb && typeof options === 'function') {
274266
cb = options;
275267
options = null;
276268
}
277-
else if (options.hydrate !== undefined) {
278-
hydrate = options.hydrate;
269+
270+
if (!id || typeof id !== 'string') {
271+
throw new Error('Id parameter is mandatory for getProfile function');
279272
}
280273

274+
281275
data = {_id: id};
282276

283277
self.kuzzle.callbackRequired('KuzzleSecurity.getProfile', cb);
@@ -287,31 +281,13 @@ KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
287281
return cb(error);
288282
}
289283

290-
if (!hydrate) {
291-
response.result._source.roles = response.result._source.roles.map(function (role) {
292-
var formattedRole = {_id: role._id};
293-
if (role._source.restrictedTo !== undefined) {
294-
formattedRole.restrictedTo = role._source.restrictedTo;
295-
}
296-
if (role._source.allowInternalIndex !== undefined) {
297-
formattedRole.allowInternalIndex = role._source.allowInternalIndex;
298-
}
299-
300-
return formattedRole;
301-
});
302-
}
303-
304284
cb(null, new KuzzleProfile(self, response.result._id, response.result._source));
305285
});
306286
};
307287

308288
/**
309289
* Executes a search on profiles according to a filter
310290
*
311-
* Takes an optional argument object with the following property:
312-
* - hydrate (boolean, default: true):
313-
* if is set to false, return a list id in role instead of KuzzleRole.
314-
* Because hydrate need to fetch all related KuzzleRole object, leave hydrate to true will have a performance cost
315291
*
316292
* /!\ There is a small delay between profile creation and their existence in our persistent search layer,
317293
* usually a couple of seconds.
@@ -325,15 +301,10 @@ KuzzleSecurity.prototype.searchProfiles = function (filters, options, cb) {
325301
var
326302
self = this;
327303

328-
filters.hydrate = true;
329-
330304
if (!cb && typeof options === 'function') {
331305
cb = options;
332306
options = null;
333307
}
334-
else if (options.hydrate !== undefined) {
335-
filters.hydrate = options.hydrate;
336-
}
337308

338309
self.kuzzle.callbackRequired('KuzzleSecurity.searchProfiles', cb);
339310

@@ -439,13 +410,7 @@ KuzzleSecurity.prototype.updateProfile = function (id, content, options, cb) {
439410
}
440411

441412
Object.keys(res.result._source).forEach(function (property) {
442-
if (property !== 'roles') {
443-
updatedContent[property] = res.result._source[property];
444-
}
445-
});
446-
447-
updatedContent.roles = res.result._source.roles.map(function (role) {
448-
return role._id;
413+
updatedContent[property] = res.result._source[property];
449414
});
450415

451416
cb(null, new KuzzleProfile(self, res.result._id, updatedContent));
@@ -503,19 +468,14 @@ KuzzleSecurity.prototype.profileFactory = function(id, content) {
503468
/**
504469
* Get a specific user from kuzzle using its unique ID
505470
*
506-
* Takes an optional argument object with the following property:
507-
* - hydrate (boolean, default: true):
508-
* if is set to false, return a list id in role instead of KuzzleRole.
509-
*
510471
* @param {string} id
511472
* @param {object} [options] - (optional) arguments
512473
* @param {responseCallback} cb - returns Kuzzle's response
513474
*/
514475
KuzzleSecurity.prototype.getUser = function (id, options, cb) {
515476
var
516477
data,
517-
self = this,
518-
hydrate = true;
478+
self = this;
519479

520480
if (!id || typeof id !== 'string') {
521481
throw new Error('Id parameter is mandatory for getUser function');
@@ -525,9 +485,6 @@ KuzzleSecurity.prototype.getUser = function (id, options, cb) {
525485
cb = options;
526486
options = null;
527487
}
528-
else if (options.hydrate !== undefined) {
529-
hydrate = options.hydrate;
530-
}
531488

532489
data = {_id: id};
533490

@@ -538,22 +495,13 @@ KuzzleSecurity.prototype.getUser = function (id, options, cb) {
538495
return cb(err);
539496
}
540497

541-
if (!hydrate) {
542-
response.result._source.profile = response.result._source.profile._id;
543-
}
544-
545498
cb(null, new KuzzleUser(self, response.result._id, response.result._source));
546499
});
547500
};
548501

549502
/**
550503
* Executes a search on user according to a filter
551504
*
552-
* Takes an optional argument object with the following property:
553-
* - hydrate (boolean, default: true):
554-
* if is set to false, return a list id in role instead of KuzzleRole.
555-
* Because hydrate need to fetch all related KuzzleRole object, leave hydrate to true will have a performance cost
556-
*
557505
* /!\ There is a small delay between user creation and their existence in our persistent search layer,
558506
* usually a couple of seconds.
559507
* That means that a user that was just been created won’t be returned by this function.
@@ -566,15 +514,10 @@ KuzzleSecurity.prototype.searchUsers = function (filters, options, cb) {
566514
var
567515
self = this;
568516

569-
filters.hydrate = true;
570-
571517
if (!cb && typeof options === 'function') {
572518
cb = options;
573519
options = null;
574520
}
575-
else if (options.hydrate !== undefined) {
576-
filters.hydrate = options.hydrate;
577-
}
578521

579522
self.kuzzle.callbackRequired('KuzzleSecurity.searchUsers', cb);
580523

@@ -788,16 +731,17 @@ KuzzleSecurity.prototype.isActionAllowed = function(rights, controller, action,
788731
/**
789732
* Gets the rights array of a given user.
790733
*
791-
* @param {string} userId The id of the user.
792-
* @param {function} cb The callback containing the normalized array of rights.
734+
* @param {string} userId The id of the user.
735+
* @param {object} [options] - (optional) arguments
736+
* @param {function} cb The callback containing the normalized array of rights.
793737
*/
794738
KuzzleSecurity.prototype.getUserRights = function (userId, options, cb) {
795739
var
796740
data = {_id: userId},
797741
self = this;
798742

799743
if (!userId || typeof userId !== 'string') {
800-
throw new Error('userId parameter is mandatory for isActionAllowed function');
744+
throw new Error('userId parameter is mandatory for getUserRights function');
801745
}
802746

803747
if (!cb && typeof options === 'function') {

0 commit comments

Comments
 (0)