Skip to content

Commit 096244c

Browse files
Merge pull request #143 from kuzzleio/feature-131-create-restricted
Implement createRestrictedUser in security and saveRestricted in security/user
2 parents 4112ff4 + ac3b22f commit 096244c

File tree

7 files changed

+185
-27
lines changed

7 files changed

+185
-27
lines changed

dist/kuzzle.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.js.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": "2.2.1",
3+
"version": "2.2.2",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/security/kuzzleSecurity.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function KuzzleSecurity(kuzzle) {
4444
* Retrieve a single Role using its unique role ID.
4545
*
4646
* @param {string} id
47-
* @param {object} [options] - Optional parameters
47+
* @param {object|responseCallback} [options] - Optional parameters
4848
* @param {responseCallback} [cb] - returns Kuzzle's response
4949
*/
5050
KuzzleSecurity.prototype.getRole = function (id, options, cb) {
@@ -78,7 +78,7 @@ KuzzleSecurity.prototype.getRole = function (id, options, cb) {
7878
* That means that a role that was just been created won’t be returned by this function.
7979
*
8080
* @param {Object} filters - this object can contains an array `indexes` with a list of index id, a integer `from` and a integer `size`
81-
* @param {object} [options] - Optional parameters
81+
* @param {object|responseCallback} [options] - Optional parameters
8282
* @param {responseCallback} [cb] - returns Kuzzle's response
8383
*
8484
*/
@@ -118,7 +118,7 @@ KuzzleSecurity.prototype.searchRoles = function (filters, options, cb) {
118118
*
119119
* @param {string} id - role identifier
120120
* @param {object} content - a plain javascript object representing the role
121-
* @param {object} [options] - (optional) arguments
121+
* @param {object|responseCallback} [options] - (optional) arguments
122122
* @param {responseCallback} [cb] - (optional) Handles the query response
123123
*/
124124
KuzzleSecurity.prototype.createRole = function (id, content, options, cb) {
@@ -154,7 +154,7 @@ KuzzleSecurity.prototype.createRole = function (id, content, options, cb) {
154154
*
155155
* @param {string} id - role identifier
156156
* @param {object} content - a plain javascript object representing the role's modification
157-
* @param {object} [options] - (optional) arguments
157+
* @param {object|responseCallback} [options] - (optional) arguments
158158
* @param {responseCallback} [cb] - (optional) Handles the query response
159159
* @returns {KuzzleSecurity} this object
160160
*/
@@ -189,7 +189,7 @@ KuzzleSecurity.prototype.updateRole = function (id, content, options, cb) {
189189
*
190190
*
191191
* @param {string} id - Role id to delete
192-
* @param {object} [options] - (optional) arguments
192+
* @param {object|responseCallback} [options] - (optional) arguments
193193
* @param {responseCallback} [cb] - Handles the query response
194194
* @returns {KuzzleSecurity} this object
195195
*/
@@ -226,7 +226,7 @@ KuzzleSecurity.prototype.roleFactory = function(id, content) {
226226
*
227227
*
228228
* @param {string} id
229-
* @param {object} [options] - (optional) arguments
229+
* @param {object|responseCallback} [options] - (optional) arguments
230230
* @param {responseCallback} cb - returns Kuzzle's response
231231
*/
232232
KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
@@ -262,7 +262,7 @@ KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
262262
* That means that a profile that was just been created won’t be returned by this function.
263263
*
264264
* @param {Object} filters - this object can contains an array `roles` with a list of roles id, a integer `from` and a integer `size`
265-
* @param {object} [options] - (optional) arguments
265+
* @param {object|responseCallback} [options] - (optional) arguments
266266
* @param {responseCallback} [cb] - returns Kuzzle's response
267267
*/
268268
KuzzleSecurity.prototype.searchProfiles = function (filters, options, cb) {
@@ -301,7 +301,7 @@ KuzzleSecurity.prototype.searchProfiles = function (filters, options, cb) {
301301
*
302302
* @param {string} id - profile identifier
303303
* @param {object} content - attribute `roles` in `content` must only contains an array of role id
304-
* @param {object} [options] - (optional) arguments
304+
* @param {object|responseCallback} [options] - (optional) arguments
305305
* @param {responseCallback} [cb] - (optional) Handles the query response
306306
*/
307307
KuzzleSecurity.prototype.createProfile = function (id, content, options, cb) {
@@ -337,7 +337,7 @@ KuzzleSecurity.prototype.createProfile = function (id, content, options, cb) {
337337
*
338338
* @param {string} id - profile identifier
339339
* @param {object} content - a plain javascript object representing the profile's modification
340-
* @param {object} [options] - (optional) arguments
340+
* @param {object|responseCallback} [options] - (optional) arguments
341341
* @param {responseCallback} [cb] - (optional) Handles the query response
342342
* @returns {KuzzleSecurity} this object
343343
*/
@@ -385,7 +385,7 @@ KuzzleSecurity.prototype.updateProfile = function (id, content, options, cb) {
385385
*
386386
*
387387
* @param {string} id - Profile id to delete
388-
* @param {object} [options] - (optional) arguments
388+
* @param {object|responseCallback} [options] - (optional) arguments
389389
* @param {responseCallback} [cb] - Handles the query response
390390
* @returns {KuzzleSecurity} this object
391391
*/
@@ -420,7 +420,7 @@ KuzzleSecurity.prototype.profileFactory = function(id, content) {
420420
* Get a specific user from kuzzle using its unique ID
421421
*
422422
* @param {string} id
423-
* @param {object} [options] - (optional) arguments
423+
* @param {object|responseCallback} [options] - (optional) arguments
424424
* @param {responseCallback} cb - returns Kuzzle's response
425425
*/
426426
KuzzleSecurity.prototype.getUser = function (id, options, cb) {
@@ -452,7 +452,7 @@ KuzzleSecurity.prototype.getUser = function (id, options, cb) {
452452
* That means that a user that was just been created won’t be returned by this function.
453453
*
454454
* @param {Object} filters - same filters as documents filters
455-
* @param {object} [options] - (optional) arguments
455+
* @param {object|responseCallback} [options] - (optional) arguments
456456
* @param {responseCallback} [cb] - returns Kuzzle's response
457457
*/
458458
KuzzleSecurity.prototype.searchUsers = function (filters, options, cb) {
@@ -490,8 +490,8 @@ KuzzleSecurity.prototype.searchUsers = function (filters, options, cb) {
490490
* Replace the existing user otherwise
491491
*
492492
* @param {string} id - user identifier
493-
* @param {object} content - attribute `profile` in `content` must only contains the profile id
494-
* @param {object} [options] - (optional) arguments
493+
* @param {object} content - attribute `profileIds` in `content` must only contain an array of profile ids
494+
* @param {object|responseCallback} [options] - (optional) arguments
495495
* @param {responseCallback} [cb] - (optional) Handles the query response
496496
*/
497497
KuzzleSecurity.prototype.createUser = function (id, content, options, cb) {
@@ -518,13 +518,47 @@ KuzzleSecurity.prototype.createUser = function (id, content, options, cb) {
518518
});
519519
};
520520

521+
/**
522+
* Create a new restricted user in Kuzzle.
523+
*
524+
* This function will create a new user. It is not usable to update an existing user.
525+
* This function allows anonymous users to create a "restricted" user with predefined rights.
526+
*
527+
* @param {string} id - user identifier
528+
* @param {object} content - attribute `profile` in `content` must only contains the profile id
529+
* @param {object|responseCallback} [options] - (optional) arguments
530+
* @param {responseCallback} [cb] - (optional) Handles the query response
531+
*/
532+
KuzzleSecurity.prototype.createRestrictedUser = function (id, content, options, cb) {
533+
var
534+
self = this,
535+
data = {_id: id, body: content};
536+
537+
if (!id || typeof id !== 'string') {
538+
throw new Error('KuzzleSecurity.createRestrictedUser: cannot create a user without a user ID');
539+
}
540+
541+
if (content.profileIds) {
542+
throw new Error('KuzzleSecurity.createRestrictedUser: cannot provide profileIds');
543+
}
544+
545+
if (!cb && typeof options === 'function') {
546+
cb = options;
547+
options = null;
548+
}
549+
550+
self.kuzzle.query(this.buildQueryArgs('createRestrictedUser'), data, null, cb && function (err, res) {
551+
cb(err, err ? undefined : new KuzzleUser(self, res.result._id, res.result._source));
552+
});
553+
};
554+
521555

522556
/**
523557
* Update an user in Kuzzle.
524558
*
525559
* @param {string} id - user identifier
526560
* @param {object} content - a plain javascript object representing the user's modification
527-
* @param {object} [options] - (optional) arguments
561+
* @param {object|responseCallback} [options] - (optional) arguments
528562
* @param {responseCallback} [cb] - (optional) Handles the query response
529563
* @returns {KuzzleSecurity} this object
530564
*/
@@ -562,7 +596,7 @@ KuzzleSecurity.prototype.updateUser = function (id, content, options, cb) {
562596
*
563597
*
564598
* @param {string} id - Profile id to delete
565-
* @param {object} [options] - (optional) arguments
599+
* @param {object|responseCallback} [options] - (optional) arguments
566600
* @param {responseCallback} [cb] - Handles the query response
567601
* @returns {KuzzleSecurity} this object
568602
*/
@@ -654,8 +688,8 @@ KuzzleSecurity.prototype.isActionAllowed = function(rights, controller, action,
654688
* Gets the rights array of a given user.
655689
*
656690
* @param {string} userId The id of the user.
657-
* @param {object} [options] - (optional) arguments
658-
* @param {function} cb The callback containing the normalized array of rights.
691+
* @param {object|responseCallback} [options] - (optional) arguments
692+
* @param {function} cb The callback containing the normalized array of rights.
659693
*/
660694
KuzzleSecurity.prototype.getUserRights = function (userId, options, cb) {
661695
var

src/security/kuzzleUser.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
var
22
KuzzleSecurityDocument = require('./kuzzleSecurityDocument');
33

4+
/**
5+
* @param {KuzzleSecurity} kuzzleSecurity
6+
* @param {string} id
7+
* @param {Object} content
8+
* @constructor
9+
*/
410
function KuzzleUser(kuzzleSecurity, id, content) {
511

612
KuzzleSecurityDocument.call(this, kuzzleSecurity, id, content);
@@ -21,7 +27,7 @@ function KuzzleUser(kuzzleSecurity, id, content) {
2127
return kuzzleSecurity.kuzzle.bluebird.promisifyAll(this, {
2228
suffix: 'Promise',
2329
filter: function (name, func, target, passes) {
24-
var whitelist = ['save'];
30+
var whitelist = ['save', 'saveRestricted'];
2531

2632
return passes && whitelist.indexOf(name) !== -1;
2733
}
@@ -37,7 +43,7 @@ KuzzleUser.prototype = Object.create(KuzzleSecurityDocument.prototype, {
3743

3844
/**
3945
* Set profiles in content
40-
* @param {array} profile - an array of profiles ids string
46+
* @param {array} profileIds - an array of profiles ids string
4147
*
4248
* @returns {KuzzleUser} this
4349
*/
@@ -53,7 +59,7 @@ KuzzleUser.prototype.setProfiles = function (profileIds) {
5359

5460
/**
5561
* Add a profile
56-
* @param {string} profile - a profile ids string
62+
* @param {string} profileId - a profile ids string
5763
*
5864
* @returns {KuzzleUser} this
5965
*/
@@ -80,8 +86,8 @@ KuzzleUser.prototype.addProfile = function (profileId) {
8086
* Otherwise, this method will replace the latest version of this user in Kuzzle by the current content
8187
* of this object.
8288
*
89+
* @param {object|responseCallback} [options] - Optional parameters
8390
* @param {responseCallback} [cb] - Handles the query response
84-
* @param {object} [options] - Optional parameters
8591
* @returns {KuzzleUser} this
8692
*/
8793
KuzzleUser.prototype.save = function (options, cb) {
@@ -101,6 +107,34 @@ KuzzleUser.prototype.save = function (options, cb) {
101107
return self;
102108
};
103109

110+
/**
111+
* Saves this user as restricted into Kuzzle.
112+
*
113+
* This function will create a new user. It is not usable to update an existing user.
114+
* The "profileIds" property must not be provided, or the request will be rejected by Kuzzle.
115+
* This function allows anonymous users to create a "restricted" user with predefined rights.
116+
*
117+
* @param {object|responseCallback} [options] - Optional parameters
118+
* @param {responseCallback} [cb] - Handles the query response
119+
* @returns {KuzzleUser} this
120+
*/
121+
KuzzleUser.prototype.saveRestricted = function (options, cb) {
122+
var
123+
data = this.serialize(),
124+
self = this;
125+
126+
if (options && cb === undefined && typeof options === 'function') {
127+
cb = options;
128+
options = null;
129+
}
130+
131+
self.kuzzle.query(this.kuzzleSecurity.buildQueryArgs('createRestrictedUser'), data, options, cb && function (error) {
132+
cb(error, error ? undefined : self);
133+
});
134+
135+
return self;
136+
};
137+
104138
/**
105139
* Serialize this object into a JSON object
106140
*

test/security/kuzzleSecurity/userMethods.test.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe('KuzzleSecurity user methods', function () {
197197
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
198198
kuzzle.query = queryStub;
199199
error = null;
200-
result = { result: {_id: 'foobar', _source: {profileIds: ['myRole']}} };
200+
result = { result: {_id: 'foobar', _source: {profileIds: ['myProfile']}} };
201201
expectedQuery = {
202202
action: 'createUser',
203203
controller: 'security'
@@ -264,6 +264,58 @@ describe('KuzzleSecurity user methods', function () {
264264
});
265265
});
266266

267+
describe('#createRestrictedUser', function () {
268+
beforeEach(function () {
269+
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
270+
kuzzle.query = queryStub;
271+
error = null;
272+
result = {result: {_id: 'foobar', _source: {some: 'body'}}};
273+
expectedQuery = {
274+
action: 'createRestrictedUser',
275+
controller: 'security'
276+
};
277+
});
278+
279+
it('should send the right query to Kuzzle', function (done) {
280+
expectedQuery.body = result.result._source;
281+
expectedQuery._id = result.result._id;
282+
283+
should(kuzzle.security.createRestrictedUser(result.result._id, result.result._source, function (err, res) {
284+
should(err).be.null();
285+
should(res).be.instanceof(KuzzleUser);
286+
done();
287+
}));
288+
});
289+
290+
it('should send the right query to Kuzzle even without callback', function (done) {
291+
expectedQuery.body = result.result._source;
292+
expectedQuery._id = result.result._id;
293+
294+
kuzzle.security.createRestrictedUser(result.result._id, result.result._source);
295+
done();
296+
});
297+
298+
it('should throw an error if no id provided', function () {
299+
should(function () { kuzzle.security.createRestrictedUser(null); }).throw(Error);
300+
});
301+
302+
it('should throw an error if profileIds is provided', function () {
303+
should(function () { kuzzle.security.createRestrictedUser(result.result._id, {profileIds: ['someProfile']}); }).throw(Error);
304+
});
305+
306+
it('should call the callback with an error if one occurs', function (done) {
307+
expectedQuery.body = result.result._source;
308+
error = 'foobar';
309+
this.timeout(50);
310+
311+
kuzzle.security.createRestrictedUser(result.result._id, result.result._source, function (err, res) {
312+
should(err).be.exactly('foobar');
313+
should(res).be.undefined();
314+
done();
315+
});
316+
});
317+
});
318+
267319
describe('#updateUser', function () {
268320
beforeEach(function () {
269321
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});

test/security/kuzzleUser/methods.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,44 @@ describe('KuzzleUser methods', function () {
8484
});
8585
});
8686

87+
describe('#saveRestricted', function () {
88+
beforeEach(function () {
89+
kuzzle = new Kuzzle('http://localhost:7512');
90+
kuzzle.query = queryStub;
91+
error = null;
92+
93+
result = { result: {_id: 'myUser', _source: {some: 'content'}} };
94+
kuzzleUser = new KuzzleUser(kuzzle.security, result.result._id, result.result._source);
95+
expectedQuery = {
96+
action: 'createRestrictedUser',
97+
controller: 'security'
98+
};
99+
});
100+
101+
it('should send the right query to kuzzle', function (done) {
102+
expectedQuery.body = result.result._source;
103+
expectedQuery._id = result.result._id;
104+
105+
should(kuzzleUser.saveRestricted(function (err, res) {
106+
should(err).be.null();
107+
should(res).be.instanceof(KuzzleUser);
108+
done();
109+
}));
110+
});
111+
112+
it('should call the callback with an error if one occurs', function (done) {
113+
expectedQuery.body = result.result._source;
114+
expectedQuery._id = result.result._id;
115+
error = 'foobar';
116+
117+
kuzzleUser.saveRestricted(function (err, res) {
118+
should(err).be.exactly('foobar');
119+
should(res).be.undefined();
120+
done();
121+
});
122+
});
123+
});
124+
87125
describe('#update', function () {
88126
before(function () {
89127
kuzzle = new Kuzzle('http://localhost:7512');

0 commit comments

Comments
 (0)