Skip to content

Commit ce795ae

Browse files
committed
Merge pull request #61 from kuzzleio/update-kuzzle-security-document
add update methods on users/profiles/roles
2 parents 1699a34 + a008173 commit ce795ae

File tree

11 files changed

+476
-1
lines changed

11 files changed

+476
-1
lines changed

src/security/kuzzleProfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function KuzzleProfile(kuzzleSecurity, id, content) {
1111
// private properties
1212
deleteActionName: {
1313
value: 'deleteProfile'
14+
},
15+
updateActionName: {
16+
value: 'updateProfile'
1417
}
1518
});
1619

src/security/kuzzleRole.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function KuzzleRole(kuzzleSecurity, id, content) {
99
// private properties
1010
deleteActionName: {
1111
value: 'deleteRole'
12+
},
13+
updateActionName: {
14+
value: 'updateRole'
1215
}
1316
});
1417

src/security/kuzzleSecurity.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,46 @@ KuzzleSecurity.prototype.createRole = function (id, content, options, cb) {
163163
}
164164
};
165165

166+
167+
/**
168+
* Update a role in Kuzzle.
169+
*
170+
* @param {string} id - role identifier
171+
* @param {object} content - a plain javascript object representing the role's modification
172+
* @param {object} [options] - (optional) arguments
173+
* @param {responseCallback} [cb] - (optional) Handles the query response
174+
*/
175+
KuzzleSecurity.prototype.updateRole = function (id, content, options, cb) {
176+
var
177+
self = this,
178+
data = {},
179+
action = 'updateRole';
180+
181+
if (!id || typeof id !== 'string') {
182+
throw new Error('KuzzleSecurity.updateRole: cannot update a role without a role ID');
183+
}
184+
185+
if (!cb && typeof options === 'function') {
186+
cb = options;
187+
options = null;
188+
}
189+
190+
data._id = id;
191+
data.body = content;
192+
193+
if (cb) {
194+
self.kuzzle.query(this.buildQueryArgs(action), data, options, function (err, res) {
195+
if (err) {
196+
return cb(err);
197+
}
198+
199+
cb(null, res.result._id);
200+
});
201+
} else {
202+
self.kuzzle.query(this.buildQueryArgs(action), data);
203+
}
204+
};
205+
166206
/**
167207
* Delete role.
168208
*
@@ -355,6 +395,46 @@ KuzzleSecurity.prototype.createProfile = function (id, content, options, cb) {
355395
}
356396
};
357397

398+
399+
/**
400+
* Update a profile in Kuzzle.
401+
*
402+
* @param {string} id - profile identifier
403+
* @param {object} content - a plain javascript object representing the profile's modification
404+
* @param {object} [options] - (optional) arguments
405+
* @param {responseCallback} [cb] - (optional) Handles the query response
406+
*/
407+
KuzzleSecurity.prototype.updateProfile = function (id, content, options, cb) {
408+
var
409+
self = this,
410+
data = {},
411+
action = 'updateProfile';
412+
413+
if (!id || typeof id !== 'string') {
414+
throw new Error('KuzzleSecurity.updateProfile: cannot update a profile without a profile ID');
415+
}
416+
417+
if (!cb && typeof options === 'function') {
418+
cb = options;
419+
options = null;
420+
}
421+
422+
data._id = id;
423+
data.body = content;
424+
425+
if (cb) {
426+
self.kuzzle.query(this.buildQueryArgs(action), data, options, function (err, res) {
427+
if (err) {
428+
return cb(err);
429+
}
430+
431+
cb(null, res.result._id);
432+
});
433+
} else {
434+
self.kuzzle.query(this.buildQueryArgs(action), data);
435+
}
436+
};
437+
358438
/**
359439
* Delete profile.
360440
*
@@ -544,6 +624,46 @@ KuzzleSecurity.prototype.createUser = function (id, content, options, cb) {
544624
}
545625
};
546626

627+
628+
/**
629+
* Update an user in Kuzzle.
630+
*
631+
* @param {string} id - user identifier
632+
* @param {object} content - a plain javascript object representing the user's modification
633+
* @param {object} [options] - (optional) arguments
634+
* @param {responseCallback} [cb] - (optional) Handles the query response
635+
*/
636+
KuzzleSecurity.prototype.updateUser = function (id, content, options, cb) {
637+
var
638+
self = this,
639+
data = {},
640+
action = 'updateUser';
641+
642+
if (!id || typeof id !== 'string') {
643+
throw new Error('KuzzleSecurity.updateUser: cannot update an user without an user ID');
644+
}
645+
646+
if (!cb && typeof options === 'function') {
647+
cb = options;
648+
options = null;
649+
}
650+
651+
data._id = id;
652+
data.body = content;
653+
654+
if (cb) {
655+
self.kuzzle.query(this.buildQueryArgs(action), data, options, function (err, res) {
656+
if (err) {
657+
return cb(err);
658+
}
659+
660+
cb(null, res.result._id);
661+
});
662+
} else {
663+
self.kuzzle.query(this.buildQueryArgs(action), data);
664+
}
665+
};
666+
547667
/**
548668
* Delete user.
549669
*

src/security/kuzzleSecurityDocument.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function KuzzleSecurityDocument(kuzzleSecurity, id, content) {
3535
return kuzzleSecurity.kuzzle.bluebird.promisifyAll(this, {
3636
suffix: 'Promise',
3737
filter: function (name, func, target, passes) {
38-
var whitelist = ['delete'];
38+
var whitelist = ['delete', 'update'];
3939

4040
return passes && whitelist.indexOf(name) !== -1;
4141
}
@@ -111,4 +111,41 @@ KuzzleSecurityDocument.prototype.delete = function (options, cb) {
111111
});
112112
};
113113

114+
/**
115+
* Update the current KuzzleSecurityDocument into Kuzzle.
116+
*
117+
* @param {object} content - Content to add to KuzzleSecurityDocument
118+
* @param {object} [options] - Optional parameters
119+
* @param {responseCallback} [cb] - Handles the query response
120+
*/
121+
KuzzleSecurityDocument.prototype.update = function (content, options, cb) {
122+
var
123+
data = {},
124+
self = this;
125+
126+
if (typeof content !== 'object') {
127+
throw new Error('Parameter "content" must be a object');
128+
}
129+
130+
if (options && cb === undefined && typeof options === 'function') {
131+
cb = options;
132+
options = null;
133+
}
134+
135+
data._id = self.id;
136+
data.body = content;
137+
138+
self.kuzzle.query(this.kuzzleSecurity.buildQueryArgs(this.updateActionName), data, options, function (error) {
139+
if (error) {
140+
return cb ? cb(error) : false;
141+
}
142+
143+
self.setContent(content, false);
144+
145+
if (cb) {
146+
cb(null, self);
147+
}
148+
});
149+
};
150+
114151
module.exports = KuzzleSecurityDocument;

src/security/kuzzleUser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function KuzzleUser(kuzzleSecurity, id, content) {
1616
// private properties
1717
deleteActionName: {
1818
value: 'deleteUser'
19+
},
20+
updateActionName: {
21+
value: 'updateUser'
1922
}
2023
});
2124

test/security/kuzzleProfile/methods.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,61 @@ describe('KuzzleRole methods', function () {
9393
});
9494
});
9595

96+
describe('#update', function () {
97+
before(function () {
98+
kuzzle = new Kuzzle('http://localhost:7512');
99+
kuzzle.query = queryStub;
100+
error = false;
101+
102+
result = { result: {_id: 'myProfile', _index: '%kuzzle', _type: 'profiles'} };
103+
kuzzleRole = new KuzzleRole(kuzzle.security, result.result._id, {indexes : {}});
104+
expectedQuery = {
105+
action: 'updateProfile',
106+
controller: 'security'
107+
};
108+
});
109+
110+
it('should send the right query to kuzzle', function (done) {
111+
expectedQuery.body = {'foo': 'bar'};
112+
expectedQuery._id = result.result._id;
113+
114+
should(kuzzleProfile.update({'foo': 'bar'}, function (err, res) {
115+
should(err).be.null();
116+
should(res).be.instanceof(KuzzleProfile);
117+
done();
118+
}));
119+
});
120+
121+
it('should call the callback with an error if one occurs', function (done) {
122+
expectedQuery.body = {'foo': 'bar'};
123+
expectedQuery._id = result.result._id;
124+
125+
error = 'foobar';
126+
this.timeout(50);
127+
128+
kuzzleProfile.update({'foo': 'bar'}, function (err, res) {
129+
should(err).be.exactly('foobar');
130+
should(res).be.undefined();
131+
done();
132+
});
133+
});
134+
135+
it('should raise an error if no content given', function (done) {
136+
expectedQuery.body = {'foo': 'bar'};
137+
expectedQuery._id = result.result._id;
138+
139+
this.timeout(50);
140+
141+
try {
142+
kuzzleProfile.update();
143+
}
144+
catch(error) {
145+
should(error).be.instanceOf(Error);
146+
done();
147+
}
148+
});
149+
});
150+
96151
describe('#addRole', function () {
97152
beforeEach(function () {
98153
kuzzle = new Kuzzle('http://localhost:7512');

test/security/kuzzleRole/methods.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,61 @@ describe('KuzzleRole methods', function () {
8383
});
8484
});
8585

86+
describe('#update', function () {
87+
before(function () {
88+
kuzzle = new Kuzzle('http://localhost:7512');
89+
kuzzle.query = queryStub;
90+
error = false;
91+
92+
result = { result: {_id: 'myRole', _index: '%kuzzle', _type: 'roles'} };
93+
kuzzleRole = new KuzzleRole(kuzzle.security, result.result._id, {indexes : {}});
94+
expectedQuery = {
95+
action: 'updateRole',
96+
controller: 'security'
97+
};
98+
});
99+
100+
it('should send the right query to kuzzle', function (done) {
101+
expectedQuery.body = {'foo': 'bar'};
102+
expectedQuery._id = result.result._id;
103+
104+
should(kuzzleRole.update({'foo': 'bar'}, function (err, res) {
105+
should(err).be.null();
106+
should(res).be.instanceof(KuzzleRole);
107+
done();
108+
}));
109+
});
110+
111+
it('should call the callback with an error if one occurs', function (done) {
112+
expectedQuery.body = {'foo': 'bar'};
113+
expectedQuery._id = result.result._id;
114+
115+
error = 'foobar';
116+
this.timeout(50);
117+
118+
kuzzleRole.update({'foo': 'bar'}, function (err, res) {
119+
should(err).be.exactly('foobar');
120+
should(res).be.undefined();
121+
done();
122+
});
123+
});
124+
125+
it('should raise an error if no content given', function (done) {
126+
expectedQuery.body = {'foo': 'bar'};
127+
expectedQuery._id = result.result._id;
128+
129+
this.timeout(50);
130+
131+
try {
132+
kuzzleRole.update();
133+
}
134+
catch(error) {
135+
should(error).be.instanceOf(Error);
136+
done();
137+
}
138+
});
139+
});
140+
86141
describe('#delete', function () {
87142
before(function () {
88143
kuzzle = new Kuzzle('http://localhost:7512');

0 commit comments

Comments
 (0)