Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,6 @@ Collection.prototype.search = function (filters, options, cb) {

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


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

Expand Down
2 changes: 1 addition & 1 deletion src/Kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ Kuzzle.prototype.whoAmI = function (cb) {
self.callbackRequired('Kuzzle.whoAmI', cb);

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

Expand Down
5 changes: 3 additions & 2 deletions src/security/Profile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var SecurityDocument = require('./SecurityDocument');

function Profile(Security, id, content) {
function Profile(Security, id, content, meta) {

SecurityDocument.call(this, Security, id, content);
SecurityDocument.call(this, Security, id, content, meta);

// Define properties
Object.defineProperties(this, {
Expand Down Expand Up @@ -124,6 +124,7 @@ Profile.prototype.serialize = function () {
}

data.body = this.content;
data.meta = this.meta;

return data;
};
Expand Down
4 changes: 2 additions & 2 deletions src/security/Role.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var SecurityDocument = require('./SecurityDocument');

function Role(Security, id, content) {
function Role(Security, id, content, meta) {

SecurityDocument.call(this, Security, id, content);
SecurityDocument.call(this, Security, id, content, meta);

// Define properties
Object.defineProperties(this, {
Expand Down
47 changes: 25 additions & 22 deletions src/security/Security.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Security.prototype.fetchRole = function (id, options, cb) {
self.kuzzle.callbackRequired('Security.fetchRole', cb);

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

Expand Down Expand Up @@ -101,7 +101,7 @@ Security.prototype.searchRoles = function (filters, options, cb) {
}

documents = result.result.hits.map(function (doc) {
return new Role(self, doc._id, doc._source);
return new Role(self, doc._id, doc._source, doc._meta);
});

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

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

Expand Down Expand Up @@ -173,8 +173,8 @@ Security.prototype.updateRole = function (id, content, options, cb) {
options = null;
}

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

return this;
Expand Down Expand Up @@ -214,10 +214,11 @@ Security.prototype.deleteRole = function (id, options, cb) {
*
* @param {string} id - role id
* @param {object} content - role content
* @param {object} meta - role metadata
* @constructor
*/
Security.prototype.role = function(id, content) {
return new Role(this, id, content);
Security.prototype.role = function(id, content, meta) {
return new Role(this, id, content, meta);
};


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

self.kuzzle.query(this.buildQueryArgs('getProfile'), data, options, function (error, response) {
cb(error, error ? undefined : new Profile(self, response.result._id, response.result._source));
cb(error, error ? undefined : new Profile(self, response.result._id, response.result._source, response.result._meta));
});
};

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

documents = response.result.hits.map(function (doc) {
return new Profile(self, doc._id, doc._source);
return new Profile(self, doc._id, doc._source, doc._meta);
});

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

self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
cb(err, err ? undefined : new Profile(self, res.result._id, res.result._source));
cb(err, err ? undefined : new Profile(self, res.result._id, res.result._source, res.result._meta));
});
};

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

cb(null, new Profile(self, res.result._id, updatedContent));
cb(null, new Profile(self, res.result._id, updatedContent, res.result._meta));
});

return this;
Expand Down Expand Up @@ -451,7 +452,7 @@ Security.prototype.scrollProfiles = function (scrollId, options, cb) {
}

result.result.hits.forEach(function (profile) {
var newProfile = new Profile(self, profile._id, profile._source);
var newProfile = new Profile(self, profile._id, profile._source, profile._meta);

newProfile.version = profile._version;

Expand All @@ -472,10 +473,11 @@ Security.prototype.scrollProfiles = function (scrollId, options, cb) {
*
* @param {string} id - profile id
* @param {object} content - profile content
* @param {object} meta - profile metadata
* @constructor
*/
Security.prototype.profile = function(id, content) {
return new Profile(this, id, content);
Security.prototype.profile = function(id, content, meta) {
return new Profile(this, id, content, meta);
};

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

self.kuzzle.query(this.buildQueryArgs('getUser'), data, options, function (err, response) {
cb(err, err ? undefined : new User(self, response.result._id, response.result._source));
cb(err, err ? undefined : new User(self, response.result._id, response.result._source, response.result._meta));
});
};

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

documents = response.result.hits.map(function (doc) {
return new User(self, doc._id, doc._source);
return new User(self, doc._id, doc._source, doc._meta);
});

if (response.result.scrollId) {
Expand Down Expand Up @@ -572,7 +574,7 @@ Security.prototype.createUser = function (id, content, options, cb) {
}

self.kuzzle.query(self.buildQueryArgs('createUser'), data, null, cb && function (err, res) {
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
});
};

Expand All @@ -599,7 +601,7 @@ Security.prototype.replaceUser = function (id, content, options, cb) {
}

self.kuzzle.query(this.buildQueryArgs('replaceUser'), data, options, cb && function (err, res) {
cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
});
};

Expand Down Expand Up @@ -666,7 +668,7 @@ Security.prototype.updateUser = function (id, content, options, cb) {
data.body = content;

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

return this;
Expand Down Expand Up @@ -735,7 +737,7 @@ Security.prototype.scrollUsers = function (scrollId, options, cb) {
}

result.result.hits.forEach(function (user) {
var newUser = new User(self, user._id, user._source);
var newUser = new User(self, user._id, user._source, user._meta);

newUser.version = user._version;

Expand All @@ -758,10 +760,11 @@ Security.prototype.scrollUsers = function (scrollId, options, cb) {
*
* @param {string} id - user id
* @param {object} content - user content
* @param {object} meta - user metadata
* @constructor
*/
Security.prototype.user = function(id, content) {
return new User(this, id, content);
Security.prototype.user = function(id, content, meta) {
return new User(this, id, content, meta);
};

/**
Expand Down
8 changes: 7 additions & 1 deletion src/security/SecurityDocument.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function SecurityDocument(Security, id, content) {
function SecurityDocument(Security, id, content, meta) {

if (!id) {
throw new Error('A security document must have an id');
Expand All @@ -23,6 +23,11 @@ function SecurityDocument(Security, id, content) {
value: {},
writable: true,
enumerable: true
},
meta: {
value: meta || {},
writable: true,
enumerable: true
}
});

Expand Down Expand Up @@ -69,6 +74,7 @@ SecurityDocument.prototype.serialize = function () {
}

data.body = this.content;
data.meta = this.meta;

return data;
};
Expand Down
9 changes: 4 additions & 5 deletions src/security/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ var
* @param {Object} content
* @constructor
*/
function User(Security, id, content) {

KuzzleSecurityDocument.call(this, Security, id, content);
function User(Security, id, content, meta) {
KuzzleSecurityDocument.call(this, Security, id, content, meta);

// Define properties
Object.defineProperties(this, {
Expand Down Expand Up @@ -189,7 +188,7 @@ User.prototype.saveRestricted = function (options, cb) {
* @return {object} JSON object representing this User
*/
User.prototype.serialize = function () {
return {_id: this.id, body: this.content};
return {_id: this.id, body: this.content, meta: this.meta};
};

/**
Expand All @@ -198,7 +197,7 @@ User.prototype.serialize = function () {
* @return {object} JSON object representing this User
*/
User.prototype.creationSerialize = function () {
return {_id: this.id, body: {content: this.content, credentials: this.credentials}};
return {_id: this.id, body: {content: this.content, credentials: this.credentials, meta: this.meta}};
};

/**
Expand Down
8 changes: 5 additions & 3 deletions test/security/kuzzleProfile/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Profile methods', function () {
}));

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

kuzzle.query.yield(null, result);
});
Expand Down Expand Up @@ -159,25 +159,27 @@ describe('Profile methods', function () {

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

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

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

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

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

serialized = profile.serialize();

should(serialized._id).be.exactly('myProfile');
should(serialized.meta).match({createdAt: '123456789'});
should.exist(serialized.body.some);
should.not.exist(serialized.body.policies);
});
Expand Down
16 changes: 15 additions & 1 deletion test/security/kuzzleRole/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Role methods', function () {
}));

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

kuzzle.query.yield(null, result);
});
Expand Down Expand Up @@ -97,6 +97,20 @@ describe('Role methods', function () {
});
});

describe('#serialize', function () {
beforeEach(function () {
role = new Role(kuzzle.security, 'myRole', {indexes : {}}, {createdAt: '123456789'});
});

it('should serialize with correct attributes', function () {
var serialized = role.serialize();

should(serialized._id).be.exactly('myRole');
should(serialized.body).match({indexes : {}});
should(serialized.meta).match({createdAt: '123456789'});
});
});

describe('#delete', function () {
beforeEach(function () {
result = { result: {_id: 'myRole'} };
Expand Down
11 changes: 6 additions & 5 deletions test/security/kuzzleUser/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('User methods', function () {
}));

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

kuzzle.query.yield(null, result);
});
Expand Down Expand Up @@ -250,28 +250,29 @@ describe('User methods', function () {

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

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

should(serialized._id).be.exactly('user');
should(serialized.body).be.match({some: 'content', profileIds: ['profile']});
should(serialized.body).match({some: 'content', profileIds: ['profile']});
should(serialized.meta).match({createdAt: '0123456789'});
});
});

describe('#creationSerialize', function () {
beforeEach(function () {
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']});
kuzzleUser = new User(kuzzle.security, 'user', {some: 'content', profileIds: ['profile']}, {createdAt: '0123456789'});
kuzzleUser.setCredentials({some: 'credentials'});
});

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

should(serialized._id).be.exactly('user');
should(serialized.body).be.match({content: {some: 'content', profileIds: ['profile']}, credentials: {some: 'credentials'}});
should(serialized.body).match({content: {some: 'content', profileIds: ['profile']}, credentials: {some: 'credentials'}, meta: {createdAt: '0123456789'}});
});
});

Expand Down