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
124 changes: 120 additions & 4 deletions src/security/Security.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ Security.prototype.searchProfiles = function (filters, options, cb) {
self.kuzzle.callbackRequired('Security.searchProfiles', cb);

self.kuzzle.query(this.buildQueryArgs('searchProfiles'), {body: filters}, options, function (error, response) {
var documents;
var
documents,
scrollId;

if (error) {
return cb(error);
Expand All @@ -287,7 +289,11 @@ Security.prototype.searchProfiles = function (filters, options, cb) {
return new Profile(self, doc._id, doc._source);
});

cb(null, { total: response.result.total, profiles: documents });
if (response.result.scrollId) {
scrollId = response.result.scrollId;
}

cb(null, { total: response.result.total, profiles: documents, scrollId: scrollId });
});
};

Expand Down Expand Up @@ -404,6 +410,58 @@ Security.prototype.deleteProfile = function (id, options, cb) {
return this;
};

/**
* @param {string} scrollId
* @param {object} [options]
* @param {responseCallback} cb
*/
Security.prototype.scrollProfiles = function (scrollId, options, cb) {
var
request = {},
self = this;

if (!scrollId) {
throw new Error('Security.scrollProfiles: scrollId is required');
}

if (!cb && typeof options === 'function') {
cb = options;
options = {};
}

this.kuzzle.callbackRequired('Security.scrollProfiles', cb);

request.scrollId = scrollId;

if (options && options.scroll) {
request.scroll = options.scroll;
}

this.kuzzle.query({controller: 'security', action: 'scrollProfiles'}, request, options, function (error, result) {
var profiles = [];

if (error) {
return cb(error);
}

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

newProfile.version = profile._version;

profiles.push(newProfile);
});

cb(null, {
total: result.result.total,
profiles: profiles,
scrollId: scrollId
});
});

return this;
};

/**
* Instantiate a new Profile object. Workaround to the module.exports limitation, preventing multiple
* constructors to be exposed without having to use a factory or a composed object.
Expand Down Expand Up @@ -467,7 +525,9 @@ Security.prototype.searchUsers = function (filters, options, cb) {
self.kuzzle.callbackRequired('Security.searchUsers', cb);

self.kuzzle.query(this.buildQueryArgs('searchUsers'), {body: filters}, options, function (error, response) {
var documents;
var
documents,
scrollId = null;

if (error) {
return cb(error);
Expand All @@ -477,7 +537,11 @@ Security.prototype.searchUsers = function (filters, options, cb) {
return new User(self, doc._id, doc._source);
});

cb(null, { total: response.result.total, users: documents });
if (response.result.scrollId) {
scrollId = response.result.scrollId;
}

cb(null, { total: response.result.total, users: documents, scrollId: scrollId });
});
};

Expand Down Expand Up @@ -632,6 +696,58 @@ Security.prototype.deleteUser = function (id, options, cb) {
return this;
};

/**
* @param {string} scrollId
* @param {object} [options]
* @param {responseCallback} cb
*/
Security.prototype.scrollUsers = function (scrollId, options, cb) {
var
request = {},
self = this;

if (!scrollId) {
throw new Error('Security.scrollUsers: scrollId is required');
}

if (!cb && typeof options === 'function') {
cb = options;
options = {};
}

this.kuzzle.callbackRequired('Security.scrollUsers', cb);

request.scrollId = scrollId;

if (options && options.scroll) {
request.scroll = options.scroll;
}

this.kuzzle.query({controller: 'security', action: 'scrollUsers'}, request, options, function (error, result) {
var users = [];

if (error) {
return cb(error);
}

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

newUser.version = user._version;

users.push(newUser);
});

cb(null, {
total: result.result.total,
users: users,
scrollId: scrollId
});
});

return this;
};

/**
* Instantiate a new User object. Workaround to the module.exports limitation, preventing multiple
* constructors to be exposed without having to use a factory or a composed object.
Expand Down
49 changes: 49 additions & 0 deletions test/security/kuzzleSecurity/profilesMethods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,55 @@ describe('Security profiles methods', function () {
});
});

describe('#scrollProfiles', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
result = { result: { _scroll_id: 'banana', total: 123, hits: [ {_id: 'foobar', _source: { foo: 'bar'}} ] } };
});

it('should throw an error if no scrollId is set', function () {
should(function () { kuzzle.security.scrollProfiles(); }).throw('Security.scrollProfiles: scrollId is required');
});

it('should throw an error if no callback is given', function () {
should(function () { kuzzle.security.scrollProfiles('scrollId'); }).throw('Security.scrollProfiles: a callback argument is required for read queries');
});

it('should parse the given parameters', function (done) {
var
queryScrollStub,
scrollId = 'scrollId',
options = { scroll: '30s' },
cb = function () {
done();
};

queryScrollStub = function (args, query, opts, callback) {
should(args.controller).be.exactly('security');
should(args.action).be.exactly('scrollProfiles');
should(query.scroll).be.exactly(options.scroll);
should(query.scrollId).be.exactly(scrollId);

callback(null, {
result: {
total: 1,
_scroll_id: 'banana',
hits: [
{
_id: 'foo',
_source: {bar: 'baz'}
}
]
}
});
};

kuzzle.query = queryScrollStub;

kuzzle.security.scrollProfiles(scrollId, options, cb);
});
});

describe('#ProfileFactory', function () {
it('should return an instance of Profile', function () {
var role = kuzzle.security.profile('test', {policies: [{roleId:'myRole'}]});
Expand Down
49 changes: 49 additions & 0 deletions test/security/kuzzleSecurity/userMethods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,55 @@ describe('Security user methods', function () {
});
});

describe('#scrollUsers', function () {
beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
result = { result: { _scroll_id: 'banana', total: 123, hits: [ {_id: 'foobar', _source: { foo: 'bar'}} ] } };
});

it('should throw an error if no scrollId is set', function () {
should(function () { kuzzle.security.scrollUsers(); }).throw('Security.scrollUsers: scrollId is required');
});

it('should throw an error if no callback is given', function () {
should(function () { kuzzle.security.scrollUsers('scrollId'); }).throw('Security.scrollUsers: a callback argument is required for read queries');
});

it('should parse the given parameters', function (done) {
var
queryScrollStub,
scrollId = 'scrollId',
options = { scroll: '30s' },
cb = function () {
done();
};

queryScrollStub = function (args, query, opts, callback) {
should(args.controller).be.exactly('security');
should(args.action).be.exactly('scrollUsers');
should(query.scroll).be.exactly(options.scroll);
should(query.scrollId).be.exactly(scrollId);

callback(null, {
result: {
total: 1,
_scroll_id: 'banana',
hits: [
{
_id: 'foo',
_source: {bar: 'baz'}
}
]
}
});
};

kuzzle.query = queryScrollStub;

kuzzle.security.scrollUsers(scrollId, options, cb);
});
});

describe('#UserFactory', function () {
it('should return an instance of User', function () {
var user = kuzzle.security.user('test', {profileIds: ['myProfile']});
Expand Down