Skip to content

Commit 753801f

Browse files
committed
Merge branch 'feature-138-should-fail' of github.com:kuzzleio/sdk-javascript into feature-138-should-fail
2 parents e21ccf8 + cea90e7 commit 753801f

File tree

11 files changed

+191
-33
lines changed

11 files changed

+191
-33
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ install:
1818
- npm install
1919
env:
2020
global:
21-
secure: EZ+XXJFDtehmag7wt8JbutqgzpkkJUCzkEWw6W+p7MIxWenLyuGv/RUu7KGqAloOud1cZbmZGSfRx/vsPvxRjo+ZC7aaPqb74qRvfctGdlJTtEffS2jFno1pEppJzCR4n7vocSfMVYtOD6oHP67lXwn/Qnh++WUkZaV137m5+7rqiYgJ+JTeSs5jBRZ6X7VNoNz8tZXejQ+FOymCFQTS2JM/rSZX9NEK+OwUEhEjRcxqWMm2avk6NAQNAL3WQuIBJQH8lxEJxOXkMc0rQZZ09AoLpqJKzsaicaZMInt5JstPo2e9vJfhi+PpRSNhErIff36aCHWRYYDPKl5x6IelZpBfZeTdrs4V2TbRi1y0ELbgMG1FHS4HAw3vTnh4/49g01lAJed4UguFRc6MzwTLAL8uPG/zFe8snm9+kaf4pL7yO89Hf3eltE03+N//tyVv+X/jdSca2dBZ9FMeLE5pJ2jlRiKitCLjXmLUEMMxEwNF7K/IvOrD9wue3JXK0UogOpcXfppk++pNTijsZluU1Yj7k3fGFeS8d6rIU0Yj40y6/VMaJeL4EXlARRsGLigUKVjQL6+/N5xudlsCng+EG+WVIy4wEEPH0QgjUkPTgWx1NrUlTwjskE0cdqDOBu39WkRFJHHECYFPTfTuDJ7xxuuibFTQmUFu2zjbYfYKhPE=
21+
secure: J5FHR91BBBrVqS5QMl+ZC3Oi2UgyQpW5SlKhfCwXJU2rz8XHNWajT5hjihlNiE6SVuUDVNflCcJI7DRD7+hwCt+G0rL9UuLnkKuMGVuVc/vMAjYpjRoVgWHk4KSwN4qWYcbF0k9d5POelI36xFnuQM0ttb3kDi/8T1s26Vsqw2Db1YDoK94ZkqO633gwwmr9RX05+sLXIuKai1bfIjUGU3YTusY0hDVw78Sg+/o1qCHNVwLDutpae/wLypO1/hujgh8SjIkAQBa/yp1gT2W+ykIaApk8eZn630usVttdToDgh2Z5+RLcgHOMi/p2/NvB/XKDq+sYh55tuKtdzu6MNx1vEVolcfiPkI0yPzwAs1IgTxAtGpKVkRlIZNMyksAQBikLqI6iFJHqYcPDHo+R+IwrXXSwZL/gbcfAtLiYWx1JnZHL8KfFz5KvRtINA4iEopzTQrOPJibFYNMWMyybvx7cHe4SAbtZxKq6cCt3fUzvwzfTIQ8m5k6y+w/bOiYY/HV8zvNw5bCS1N6h/6u8mmZMw3wv9N9eQz+h2BKwSziWt27CXcUKl6HhHklR7zj3/b7HwKW1o7Y0cixTl8kUBC8k+093Mhg4FmJWEAacfXVzRDs5nEejronmNUTi8hnpsSBeNMQnuD99zQp+eIi5PjfZ9DsCCM85cYb/LG1HyLs=
2222
deploy:
2323
provider: npm
2424
skip_cleanup: true

deploy.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ then
55
echo "Push generated dist folder"
66
git config --global user.email "[email protected]"
77
git config --global user.name "Travis CI"
8-
git clone --quiet --branch=${TRAVIS_BRANCH} https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} travis-build
8+
git clone --quiet --branch=${TRAVIS_BRANCH} https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} travis-build > /dev/null 2>&1
99
cd travis-build
1010
ln -s ${TRAVIS_BUILD_DIR}/node_modules
1111
npm run build
1212
git add dist/
13-
git commit -am "Travis CI - [ci skip] - automatic dist folder"
13+
git commit -am "Travis CI - [ci skip] - automatic dist folder" > /dev/null 2>&1
1414
git push origin ${TRAVIS_BRANCH}
1515
fi
1616

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: 2 additions & 2 deletions
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": {
@@ -29,7 +29,7 @@
2929
"dependencies": {
3030
"bluebird": "3.4.6",
3131
"bufferutil": "^1.2.1",
32-
"node-uuid": "1.4.7",
32+
"uuid": "3.0.0",
3333
"utf-8-validate": "^1.2.1",
3434
"ws": "^1.1.1"
3535
},

src/kuzzle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var
2-
uuid = require('node-uuid'),
2+
uuid = require('uuid'),
33
KuzzleDataCollection = require('./kuzzleDataCollection'),
44
KuzzleSecurity = require('./security/kuzzleSecurity'),
55
KuzzleMemoryStorage = require('./kuzzleMemoryStorage'),

src/kuzzleRoom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var
2-
uuid = require('node-uuid');
2+
uuid = require('uuid');
33

44
/**
55
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.

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'});

0 commit comments

Comments
 (0)