Skip to content

Commit c89bfdd

Browse files
committed
Merge remote-tracking branch 'origin/master' into KUZ415_serialize
Conflicts: dist/kuzzle.min.js dist/kuzzle.min.map
2 parents bb4d31c + 7b00102 commit c89bfdd

25 files changed

+4407
-36
lines changed

dist/kuzzle.js

Lines changed: 1139 additions & 16 deletions
Large diffs are not rendered by default.

dist/kuzzle.min.js

Lines changed: 3 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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.4.4",
3+
"version": "1.6.0",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzle.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var
22
uuid = require('node-uuid'),
3-
KuzzleDataCollection = require('./kuzzleDataCollection');
3+
KuzzleDataCollection = require('./kuzzleDataCollection'),
4+
KuzzleSecurity = require('./security/kuzzleSecurity'),
5+
KuzzleUser = require('./security/kuzzleUser');
46

57
/**
68
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.
@@ -18,6 +20,7 @@ var
1820
* @param {responseCallback} [cb] - Handles connection response
1921
* @constructor
2022
*/
23+
2124
module.exports = Kuzzle = function (url, options, cb) {
2225
var self = this;
2326

@@ -111,7 +114,6 @@ module.exports = Kuzzle = function (url, options, cb) {
111114
value: url,
112115
enumerable: true
113116
},
114-
// writable properties
115117
autoQueue: {
116118
value: false,
117119
enumerable: true,
@@ -219,7 +221,7 @@ module.exports = Kuzzle = function (url, options, cb) {
219221
}
220222
});
221223

222-
/*
224+
/**
223225
* Some methods (mainly read queries) require a callback function. This function exists to avoid repetition of code,
224226
* and is called by these methods
225227
*/
@@ -231,7 +233,15 @@ module.exports = Kuzzle = function (url, options, cb) {
231233
}
232234
});
233235

234-
/*
236+
/**
237+
* Create an attribute security that embed all methods to manage Role, Profile and User
238+
*/
239+
Object.defineProperty(this, 'security', {
240+
value: new KuzzleSecurity(this),
241+
enumerable: true
242+
});
243+
244+
/**
235245
* Emit an event to all registered listeners
236246
* An event cannot be emitted multiple times before a timeout has been reached.
237247
*/
@@ -273,7 +283,6 @@ module.exports = Kuzzle = function (url, options, cb) {
273283
});
274284
}
275285

276-
return this;
277286
};
278287

279288

@@ -504,13 +513,18 @@ Kuzzle.prototype.checkToken = function (token, callback) {
504513
Kuzzle.prototype.whoAmI = function (callback) {
505514
var self = this;
506515

507-
this.callbackRequired('Kuzzle.whoAmI', callback);
516+
self.callbackRequired('Kuzzle.whoAmI', callback);
508517

509-
this.query({controller: 'auth', action: 'getCurrentUser'}, {}, {}, callback);
518+
self.query({controller: 'auth', action: 'getCurrentUser'}, {}, {}, function (err, response) {
519+
if (err) {
520+
return callback(err);
521+
}
522+
523+
callback(null, new KuzzleUser(self.security, response.result._id, response.result._source));
524+
});
510525

511526
return self;
512527
};
513-
514528
/**
515529
* Clean up the queue, ensuring the queryTTL and queryMaxSize properties are respected
516530
*/
@@ -724,10 +738,9 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
724738
*
725739
* @param {string} [index] - The name of the data index containing the data collection
726740
* @param {string} collection - The name of the data collection you want to manipulate
727-
* @param headers {object} [headers] - Common properties for all future write documents queries
728741
* @returns {object} A KuzzleDataCollection instance
729742
*/
730-
Kuzzle.prototype.dataCollectionFactory = function(index, collection, headers) {
743+
Kuzzle.prototype.dataCollectionFactory = function(index, collection) {
731744
this.isValid();
732745

733746
if (arguments.length === 1) {
@@ -749,7 +762,7 @@ Kuzzle.prototype.dataCollectionFactory = function(index, collection, headers) {
749762
}
750763

751764
if (!this.collections[index][collection]) {
752-
this.collections[index][collection] = new KuzzleDataCollection(this, index, collection, headers);
765+
this.collections[index][collection] = new KuzzleDataCollection(this, index, collection);
753766
}
754767

755768
return this.collections[index][collection];
@@ -976,7 +989,7 @@ Kuzzle.prototype.query = function (queryArgs, query, options, cb) {
976989
* Do not add the token for the checkToken route, to avoid getting a token error when
977990
* a developer simply wish to verify his token
978991
*/
979-
if (self.jwtToken !== undefined && object.controller !== 'auth' && object.action !== 'checkToken') {
992+
if (self.jwtToken !== undefined && !(object.controller === 'auth' && object.action === 'checkToken')) {
980993
object.headers = object.headers || {};
981994
object.headers.authorization = 'Bearer ' + self.jwtToken;
982995
}
@@ -1126,7 +1139,6 @@ Kuzzle.prototype.startQueuing = function () {
11261139
if (this.state === 'offline' && !this.autoQueue) {
11271140
this.queuing = true;
11281141
}
1129-
11301142
return this;
11311143
};
11321144

src/kuzzleDataCollection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ KuzzleDataCollection.prototype.createDocument = function (id, document, options,
226226
data._id = id;
227227
}
228228

229-
data.persist = true;
230229
data = self.kuzzle.addHeaders(data, self.headers);
231230

232231
if (cb) {

src/security/kuzzleProfile.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
var
2+
KuzzleSecurityDocument = require('./kuzzleSecurityDocument'),
3+
KuzzleRole = require('./kuzzleRole');
4+
5+
function KuzzleProfile(kuzzleSecurity, id, content) {
6+
7+
KuzzleSecurityDocument.call(this, kuzzleSecurity, id, content);
8+
9+
// Define properties
10+
Object.defineProperties(this, {
11+
// private properties
12+
deleteActionName: {
13+
value: 'deleteProfile'
14+
}
15+
});
16+
17+
// Hydrate profile with roles if roles are not only string but objects with `_id` and `_source`
18+
if (content && content.roles) {
19+
content.roles = content.roles.map(function (role) {
20+
if (!role._id || !role._source) {
21+
return role;
22+
}
23+
24+
return new KuzzleRole(kuzzleSecurity, role._id, role._source);
25+
});
26+
}
27+
28+
// promisifying
29+
if (kuzzleSecurity.kuzzle.bluebird) {
30+
return kuzzleSecurity.kuzzle.bluebird.promisifyAll(this, {
31+
suffix: 'Promise',
32+
filter: function (name, func, target, passes) {
33+
var whitelist = ['hydrate', 'save'];
34+
35+
return passes && whitelist.indexOf(name) !== -1;
36+
}
37+
});
38+
}
39+
40+
}
41+
42+
KuzzleProfile.prototype = Object.create(KuzzleSecurityDocument.prototype, {
43+
constructor: {
44+
value: KuzzleProfile
45+
}
46+
});
47+
48+
/**
49+
* Persist to the persistent layer the current profile
50+
*
51+
* @param {object} [options] - Optional parameters
52+
* @param {responseCallback} [cb] - Handles the query response
53+
* @returns {Object} this
54+
*/
55+
KuzzleProfile.prototype.save = function (options, cb) {
56+
var
57+
data,
58+
self = this;
59+
60+
if (!this.content.roles) {
61+
throw new Error('Argument "roles" is mandatory in a profile. This argument contains an array of KuzzleRole or an array of id string');
62+
}
63+
64+
if (options && cb === undefined && typeof options === 'function') {
65+
cb = options;
66+
options = null;
67+
}
68+
69+
data = this.serialize();
70+
71+
self.kuzzle.query(self.kuzzleSecurity.buildQueryArgs('createOrReplaceProfile'), data, options, function (error) {
72+
if (error) {
73+
return cb ? cb(error) : false;
74+
}
75+
76+
if (cb) {
77+
cb(null, self);
78+
}
79+
});
80+
81+
return self;
82+
};
83+
84+
85+
/**
86+
* Add a role in the roles list
87+
* @param {KuzzleRole|string} role - can be an instance of KuzzleRole or an id in string
88+
*
89+
* @returns {KuzzleProfile} this
90+
*/
91+
KuzzleProfile.prototype.addRole = function (role) {
92+
93+
if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
94+
throw new Error('Parameter "roles" must be a KuzzleRole or a id string');
95+
}
96+
97+
if (!this.content.roles) {
98+
this.content.roles = [];
99+
}
100+
101+
this.content.roles.push(role);
102+
103+
return this;
104+
};
105+
106+
/**
107+
* Set roles list
108+
* @param {Array} roles - can be an array of KuzzleRole or an array of string
109+
*
110+
* @returns {KuzzleProfile} this
111+
*/
112+
KuzzleProfile.prototype.setRoles = function (roles) {
113+
114+
if (!Array.isArray(roles)) {
115+
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
116+
}
117+
118+
roles.map(function (role) {
119+
if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
120+
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
121+
}
122+
});
123+
124+
this.content.roles = roles;
125+
126+
return this;
127+
};
128+
129+
130+
/**
131+
* Hydrate the profile - get real KuzzleRole and not just ids
132+
* Warning: do not try to hydrate a profile with newly added role which is not created in kuzzle
133+
*
134+
* @param {object} [options] - Optional parameters
135+
* @param {responseCallback} [cb] - Handles the query response
136+
*/
137+
KuzzleProfile.prototype.hydrate = function (options, cb) {
138+
139+
var
140+
self = this,
141+
data = {ids: []};
142+
143+
data.ids = this.content.roles.map(function (role) {
144+
if (typeof role === 'string') {
145+
return role;
146+
}
147+
148+
if (role instanceof KuzzleRole) {
149+
return role.id;
150+
}
151+
});
152+
153+
if (options && cb === undefined && typeof options === 'function') {
154+
cb = options;
155+
options = null;
156+
}
157+
158+
self.kuzzle.callbackRequired('KuzzleProfile.hydrate', cb);
159+
160+
self.kuzzle.query(self.kuzzleSecurity.buildQueryArgs('mGetRoles'), {body: data}, options, function (error, response) {
161+
if (error) {
162+
return cb(error);
163+
}
164+
165+
cb(null, new KuzzleProfile(self, response.result._id, response.result._source));
166+
});
167+
};
168+
169+
/**
170+
* Serialize this object into a JSON object
171+
*
172+
* @return {object} JSON object representing this securityDocument
173+
*/
174+
KuzzleProfile.prototype.serialize = function () {
175+
var
176+
data = {};
177+
178+
if (this.id) {
179+
data._id = this.id;
180+
}
181+
182+
data.body = this.content;
183+
if (!data.body.roles || !Array.isArray(data.body.roles)) {
184+
return data;
185+
}
186+
187+
data.body.roles = data.body.roles.map(function(role) {
188+
if (role instanceof KuzzleRole) {
189+
return role.id;
190+
}
191+
192+
return role;
193+
});
194+
195+
return data;
196+
};
197+
198+
199+
module.exports = KuzzleProfile;

0 commit comments

Comments
 (0)