Skip to content

Commit 2fdffee

Browse files
Merge pull request #60 from kuzzleio/fix-security-hydrate-user
fix hydrate user and replace in setContent
2 parents 7309a79 + 853e050 commit 2fdffee

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"rules": {
3-
"consistent-return": 2,
3+
"consistent-return": 0,
44
"curly": 2,
55
"dot-notation": 2,
66
"eqeqeq": 2,
@@ -44,7 +44,7 @@
4444
"no-with": 2,
4545
"quotes": [2, "single"],
4646
"semi": [2, "always"],
47-
"space-after-keywords": 2,
47+
"keyword-spacing": 2,
4848
"space-before-blocks": 2,
4949
"space-in-parens": [2, "never"],
5050
"vars-on-top": 2,

src/security/kuzzleProfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ KuzzleProfile.prototype.hydrate = function (options, cb) {
162162
return cb(error);
163163
}
164164

165-
cb(null, new KuzzleProfile(self, response.result._id, response.result._source));
165+
cb(null, new KuzzleProfile(self, self.id, {roles: response.result.hits}));
166166
});
167167
};
168168

src/security/kuzzleSecurityDocument.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function KuzzleSecurityDocument(kuzzleSecurity, id, content) {
2727
});
2828

2929
if (content) {
30-
this.setContent(content);
30+
this.setContent(content, true);
3131
}
3232

3333
// promisifying
@@ -44,13 +44,25 @@ function KuzzleSecurityDocument(kuzzleSecurity, id, content) {
4444
}
4545

4646
/**
47+
* Replaces the current content with new data.
48+
* Changes made by this function won’t be applied until the save method is called.
4749
*
4850
* @param {Object} data - New securityDocument content
51+
* @param {boolean} replace - if true: replace this document content with the provided data.
4952
*
5053
* @return {Object} this
5154
*/
52-
KuzzleSecurityDocument.prototype.setContent = function (data) {
53-
this.content = data;
55+
KuzzleSecurityDocument.prototype.setContent = function (data, replace) {
56+
var self = this;
57+
58+
if (replace) {
59+
this.content = data;
60+
}
61+
else {
62+
Object.keys(data).forEach(function (key) {
63+
self.content[key] = data[key];
64+
});
65+
}
5466

5567
return this;
5668
};

src/security/kuzzleUser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ KuzzleUser.prototype.hydrate = function (options, cb) {
6161
}
6262

6363
self.kuzzle.query(this.kuzzleSecurity.buildQueryArgs('getProfile'), {_id: this.content.profile}, options, function (error, response) {
64+
var hydratedUser;
65+
6466
if (error) {
6567
return cb(error);
6668
}
6769

68-
cb(null, new KuzzleUser(self, response.result._id, response.result._source));
70+
hydratedUser = new KuzzleUser(self.kuzzleSecurity, self.id, self.content);
71+
hydratedUser.setProfile(new KuzzleProfile(self.kuzzleSecurity, response.result._id, response.result._source));
72+
73+
cb(null, hydratedUser);
6974
});
7075
};
7176

test/security/kuzzleProfile/methods.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ describe('KuzzleRole methods', function () {
183183
kuzzle.query = queryStub;
184184
error = false;
185185

186-
result = { result: {_id: 'myProfile', _source: {roles : ['role1', 'role2']}} };
187-
kuzzleProfile = new KuzzleProfile(kuzzle.security, result.result._id, result.result._source);
186+
result = { result: {hits: [{_id: 'role1', _source: {indexes: {}}}, {_id: 'role2', _source: {indexes: {}}}]}};
187+
kuzzleProfile = new KuzzleProfile(kuzzle.security, 'profile', {roles: result.result.hits});
188188
expectedQuery = {
189189
action: 'mGetRoles',
190190
controller: 'security'
@@ -196,11 +196,18 @@ describe('KuzzleRole methods', function () {
196196
});
197197

198198
it('should send the right query to kuzzle', function (done) {
199-
expectedQuery.body = {ids: result.result._source.roles};
199+
expectedQuery.body = {ids: ['role1', 'role2']};
200200

201201
should(kuzzleProfile.hydrate(function (err, res) {
202202
should(err).be.null();
203203
should(res).be.instanceof(KuzzleProfile);
204+
should(res.content.roles).be.an.Array();
205+
should(res.content.roles).not.be.empty();
206+
207+
res.content.roles.forEach(function (role) {
208+
should(role).instanceof(KuzzleRole);
209+
});
210+
204211
done();
205212
}));
206213
});

0 commit comments

Comments
 (0)