Skip to content

Commit 4ecab74

Browse files
authored
Merge pull request #236 from kuzzleio/36-security-meta
Add meta to Security documents (User, Profile, Role)
2 parents 31df206 + 769bfb0 commit 4ecab74

File tree

10 files changed

+68
-43
lines changed

10 files changed

+68
-43
lines changed

src/Collection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,6 @@ Collection.prototype.search = function (filters, options, cb) {
693693

694694
query = self.kuzzle.addHeaders({body: filters}, this.headers);
695695

696-
697696
self.kuzzle.query(this.buildQueryArgs('document', 'search'), query, options, function (error, result) {
698697
var documents = [];
699698

src/Kuzzle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ Kuzzle.prototype.whoAmI = function (cb) {
751751
self.callbackRequired('Kuzzle.whoAmI', cb);
752752

753753
self.query({controller: 'auth', action: 'getCurrentUser'}, {}, {}, function (err, res) {
754-
cb(err, err ? undefined : new User(self.security, res.result._id, res.result._source));
754+
cb(err, err ? undefined : new User(self.security, res.result._id, res.result._source, res.result._meta));
755755
});
756756
};
757757

src/security/Profile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var SecurityDocument = require('./SecurityDocument');
22

3-
function Profile(Security, id, content) {
3+
function Profile(Security, id, content, meta) {
44

5-
SecurityDocument.call(this, Security, id, content);
5+
SecurityDocument.call(this, Security, id, content, meta);
66

77
// Define properties
88
Object.defineProperties(this, {
@@ -124,6 +124,7 @@ Profile.prototype.serialize = function () {
124124
}
125125

126126
data.body = this.content;
127+
data.meta = this.meta;
127128

128129
return data;
129130
};

src/security/Role.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var SecurityDocument = require('./SecurityDocument');
22

3-
function Role(Security, id, content) {
3+
function Role(Security, id, content, meta) {
44

5-
SecurityDocument.call(this, Security, id, content);
5+
SecurityDocument.call(this, Security, id, content, meta);
66

77
// Define properties
88
Object.defineProperties(this, {

src/security/Security.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Security.prototype.fetchRole = function (id, options, cb) {
6666
self.kuzzle.callbackRequired('Security.fetchRole', cb);
6767

6868
self.kuzzle.query(this.buildQueryArgs('getRole'), data, options, function (err, response) {
69-
cb(err, err ? undefined : new Role(self, response.result._id, response.result._source));
69+
cb(err, err ? undefined : new Role(self, response.result._id, response.result._source, response.result._meta));
7070
});
7171
};
7272

@@ -101,7 +101,7 @@ Security.prototype.searchRoles = function (filters, options, cb) {
101101
}
102102

103103
documents = result.result.hits.map(function (doc) {
104-
return new Role(self, doc._id, doc._source);
104+
return new Role(self, doc._id, doc._source, doc._meta);
105105
});
106106

107107
cb(null, { total: result.result.total, roles: documents });
@@ -144,7 +144,7 @@ Security.prototype.createRole = function (id, content, options, cb) {
144144
}
145145

146146
self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
147-
cb(err, err ? undefined : new Role(self, res.result._id, res.result._source));
147+
cb(err, err ? undefined : new Role(self, res.result._id, res.result._source, res.result._meta));
148148
});
149149
};
150150

@@ -173,8 +173,8 @@ Security.prototype.updateRole = function (id, content, options, cb) {
173173
options = null;
174174
}
175175

176-
self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err) {
177-
cb(err, err ? undefined : new Role(self, id, content));
176+
self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
177+
cb(err, err ? undefined : new Role(self, id, content, res.result._meta));
178178
});
179179

180180
return this;
@@ -214,10 +214,11 @@ Security.prototype.deleteRole = function (id, options, cb) {
214214
*
215215
* @param {string} id - role id
216216
* @param {object} content - role content
217+
* @param {object} meta - role metadata
217218
* @constructor
218219
*/
219-
Security.prototype.role = function(id, content) {
220-
return new Role(this, id, content);
220+
Security.prototype.role = function(id, content, meta) {
221+
return new Role(this, id, content, meta);
221222
};
222223

223224

@@ -249,7 +250,7 @@ Security.prototype.fetchProfile = function (id, options, cb) {
249250
self.kuzzle.callbackRequired('Security.fetchProfile', cb);
250251

251252
self.kuzzle.query(this.buildQueryArgs('getProfile'), data, options, function (error, response) {
252-
cb(error, error ? undefined : new Profile(self, response.result._id, response.result._source));
253+
cb(error, error ? undefined : new Profile(self, response.result._id, response.result._source, response.result._meta));
253254
});
254255
};
255256

@@ -286,7 +287,7 @@ Security.prototype.searchProfiles = function (filters, options, cb) {
286287
}
287288

288289
documents = response.result.hits.map(function (doc) {
289-
return new Profile(self, doc._id, doc._source);
290+
return new Profile(self, doc._id, doc._source, doc._meta);
290291
});
291292

292293
if (response.result.scrollId) {
@@ -336,7 +337,7 @@ Security.prototype.createProfile = function (id, policies, options, cb) {
336337
}
337338

338339
self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
339-
cb(err, err ? undefined : new Profile(self, res.result._id, res.result._source));
340+
cb(err, err ? undefined : new Profile(self, res.result._id, res.result._source, res.result._meta));
340341
});
341342
};
342343

@@ -382,7 +383,7 @@ Security.prototype.updateProfile = function (id, policies, options, cb) {
382383
updatedContent[property] = res.result._source[property];
383384
});
384385

385-
cb(null, new Profile(self, res.result._id, updatedContent));
386+
cb(null, new Profile(self, res.result._id, updatedContent, res.result._meta));
386387
});
387388

388389
return this;
@@ -451,7 +452,7 @@ Security.prototype.scrollProfiles = function (scrollId, options, cb) {
451452
}
452453

453454
result.result.hits.forEach(function (profile) {
454-
var newProfile = new Profile(self, profile._id, profile._source);
455+
var newProfile = new Profile(self, profile._id, profile._source, profile._meta);
455456

456457
newProfile.version = profile._version;
457458

@@ -472,10 +473,11 @@ Security.prototype.scrollProfiles = function (scrollId, options, cb) {
472473
*
473474
* @param {string} id - profile id
474475
* @param {object} content - profile content
476+
* @param {object} meta - profile metadata
475477
* @constructor
476478
*/
477-
Security.prototype.profile = function(id, content) {
478-
return new Profile(this, id, content);
479+
Security.prototype.profile = function(id, content, meta) {
480+
return new Profile(this, id, content, meta);
479481
};
480482

481483
/**
@@ -502,7 +504,7 @@ Security.prototype.fetchUser = function (id, options, cb) {
502504
self.kuzzle.callbackRequired('Security.fetchUser', cb);
503505

504506
self.kuzzle.query(this.buildQueryArgs('getUser'), data, options, function (err, response) {
505-
cb(err, err ? undefined : new User(self, response.result._id, response.result._source));
507+
cb(err, err ? undefined : new User(self, response.result._id, response.result._source, response.result._meta));
506508
});
507509
};
508510

@@ -538,7 +540,7 @@ Security.prototype.searchUsers = function (filters, options, cb) {
538540
}
539541

540542
documents = response.result.hits.map(function (doc) {
541-
return new User(self, doc._id, doc._source);
543+
return new User(self, doc._id, doc._source, doc._meta);
542544
});
543545

544546
if (response.result.scrollId) {
@@ -568,7 +570,7 @@ Security.prototype.createUser = function (id, content, options, cb) {
568570
}
569571

570572
self.kuzzle.query(self.buildQueryArgs('createUser'), data, null, cb && function (err, res) {
571-
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
573+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
572574
});
573575
};
574576

@@ -595,7 +597,7 @@ Security.prototype.replaceUser = function (id, content, options, cb) {
595597
}
596598

597599
self.kuzzle.query(this.buildQueryArgs('replaceUser'), data, options, cb && function (err, res) {
598-
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
600+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
599601
});
600602
};
601603

@@ -658,7 +660,7 @@ Security.prototype.updateUser = function (id, content, options, cb) {
658660
data.body = content;
659661

660662
self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
661-
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
663+
cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
662664
});
663665

664666
return this;
@@ -727,7 +729,7 @@ Security.prototype.scrollUsers = function (scrollId, options, cb) {
727729
}
728730

729731
result.result.hits.forEach(function (user) {
730-
var newUser = new User(self, user._id, user._source);
732+
var newUser = new User(self, user._id, user._source, user._meta);
731733

732734
newUser.version = user._version;
733735

@@ -750,10 +752,11 @@ Security.prototype.scrollUsers = function (scrollId, options, cb) {
750752
*
751753
* @param {string} id - user id
752754
* @param {object} content - user content
755+
* @param {object} meta - user metadata
753756
* @constructor
754757
*/
755-
Security.prototype.user = function(id, content) {
756-
return new User(this, id, content);
758+
Security.prototype.user = function(id, content, meta) {
759+
return new User(this, id, content, meta);
757760
};
758761

759762
/**

src/security/SecurityDocument.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function SecurityDocument(Security, id, content) {
1+
function SecurityDocument(Security, id, content, meta) {
22

33
if (!id) {
44
throw new Error('A security document must have an id');
@@ -23,6 +23,11 @@ function SecurityDocument(Security, id, content) {
2323
value: {},
2424
writable: true,
2525
enumerable: true
26+
},
27+
meta: {
28+
value: meta || {},
29+
writable: true,
30+
enumerable: true
2631
}
2732
});
2833

@@ -69,6 +74,7 @@ SecurityDocument.prototype.serialize = function () {
6974
}
7075

7176
data.body = this.content;
77+
data.meta = this.meta;
7278

7379
return data;
7480
};

src/security/User.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ var
77
* @param {Object} content
88
* @constructor
99
*/
10-
function User(Security, id, content) {
11-
12-
KuzzleSecurityDocument.call(this, Security, id, content);
10+
function User(Security, id, content, meta) {
11+
KuzzleSecurityDocument.call(this, Security, id, content, meta);
1312

1413
// Define properties
1514
Object.defineProperties(this, {
@@ -189,7 +188,7 @@ User.prototype.saveRestricted = function (options, cb) {
189188
* @return {object} JSON object representing this User
190189
*/
191190
User.prototype.serialize = function () {
192-
return {_id: this.id, body: this.content};
191+
return {_id: this.id, body: this.content, meta: this.meta};
193192
};
194193

195194
/**
@@ -198,7 +197,7 @@ User.prototype.serialize = function () {
198197
* @return {object} JSON object representing this User
199198
*/
200199
User.prototype.creationSerialize = function () {
201-
return {_id: this.id, body: {content: this.content, credentials: this.credentials}};
200+
return {_id: this.id, body: {content: this.content, credentials: this.credentials, meta: this.meta}};
202201
};
203202

204203
/**

test/security/kuzzleProfile/methods.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('Profile methods', function () {
4444
}));
4545

4646
should(kuzzle.query).be.calledOnce();
47-
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myProfile', body: {policies: []}}, null, sinon.match.func);
47+
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myProfile', body: {policies: []}, meta: {}}, null, sinon.match.func);
4848

4949
kuzzle.query.yield(null, result);
5050
});
@@ -159,25 +159,27 @@ describe('Profile methods', function () {
159159

160160
describe('#serialize', function () {
161161
beforeEach(function () {
162-
profile = new Profile(kuzzle.security, 'myProfile', {some: 'content', policies: [{roleId:'role1'}]});
162+
profile = new Profile(kuzzle.security, 'myProfile', {some: 'content', policies: [{roleId:'role1'}]}, {createdAt: '123456789'});
163163
});
164164

165165
it('should serialize with correct attributes', function () {
166166
var serialized = profile.serialize();
167167

168168
should(serialized._id).be.exactly('myProfile');
169169
should(serialized.body).match({some: 'content', policies: [{roleId:'role1'}]});
170+
should(serialized.meta).match({createdAt: '123456789'});
170171
});
171172

172173
it('should serialize without policies if no policies attribute is defined', function () {
173174
var
174175
serialized;
175176

176-
profile = new Profile(kuzzle.security, 'myProfile', {some: 'content'});
177+
profile = new Profile(kuzzle.security, 'myProfile', {some: 'content'}, {createdAt: '123456789'});
177178

178179
serialized = profile.serialize();
179180

180181
should(serialized._id).be.exactly('myProfile');
182+
should(serialized.meta).match({createdAt: '123456789'});
181183
should.exist(serialized.body.some);
182184
should.not.exist(serialized.body.policies);
183185
});

test/security/kuzzleRole/methods.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Role methods', function () {
3636
}));
3737

3838
should(kuzzle.query).be.calledOnce();
39-
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myRole', body: {indexes : {}}}, null, sinon.match.func);
39+
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myRole', body: {indexes : {}}, meta: {}}, null, sinon.match.func);
4040

4141
kuzzle.query.yield(null, result);
4242
});
@@ -97,6 +97,20 @@ describe('Role methods', function () {
9797
});
9898
});
9999

100+
describe('#serialize', function () {
101+
beforeEach(function () {
102+
role = new Role(kuzzle.security, 'myRole', {indexes : {}}, {createdAt: '123456789'});
103+
});
104+
105+
it('should serialize with correct attributes', function () {
106+
var serialized = role.serialize();
107+
108+
should(serialized._id).be.exactly('myRole');
109+
should(serialized.body).match({indexes : {}});
110+
should(serialized.meta).match({createdAt: '123456789'});
111+
});
112+
});
113+
100114
describe('#delete', function () {
101115
beforeEach(function () {
102116
result = { result: {_id: 'myRole'} };

test/security/kuzzleUser/methods.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('User methods', function () {
133133
}));
134134

135135
should(kuzzle.query).be.calledOnce();
136-
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myUser', body: {some: 'content'}}, null, sinon.match.func);
136+
should(kuzzle.query).be.calledWith(expectedQuery, {_id: 'myUser', body: {some: 'content'}, meta: {}}, null, sinon.match.func);
137137

138138
kuzzle.query.yield(null, result);
139139
});
@@ -250,28 +250,29 @@ describe('User methods', function () {
250250

251251
describe('#serialize', function () {
252252
beforeEach(function () {
253-
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']});
253+
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']}, {createdAt: '0123456789'});
254254
});
255255

256256
it('should serialize with correct attributes', function () {
257257
var serialized = kuzzleUser.serialize();
258258

259259
should(serialized._id).be.exactly('user');
260-
should(serialized.body).be.match({some: 'content', profileIds: ['profile']});
260+
should(serialized.body).match({some: 'content', profileIds: ['profile']});
261+
should(serialized.meta).match({createdAt: '0123456789'});
261262
});
262263
});
263264

264265
describe('#creationSerialize', function () {
265266
beforeEach(function () {
266-
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']});
267+
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']}, {createdAt: '0123456789'});
267268
kuzzleUser.setCredentials({some: 'credentials'});
268269
});
269270

270271
it('should serialize with correct attributes', function () {
271272
var serialized = kuzzleUser.creationSerialize();
272273

273274
should(serialized._id).be.exactly('user');
274-
should(serialized.body).be.match({content: {some: 'content', profileIds: ['profile']}, credentials: {some: 'credentials'}});
275+
should(serialized.body).match({content: {some: 'content', profileIds: ['profile']}, credentials: {some: 'credentials'}, meta: {createdAt: '0123456789'}});
275276
});
276277
});
277278

0 commit comments

Comments
 (0)