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
34 changes: 21 additions & 13 deletions src/controllers/Security.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ class SecurityController extends BaseController {
};

return this.query(request, options)
.then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies));
.then(response => new Profile(
this.kuzzle,
response.result._id,
response.result._source));
}

createOrReplaceRole (_id, body, options = {}) {
Expand All @@ -135,7 +138,10 @@ class SecurityController extends BaseController {
};

return this.query(request, options)
.then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies));
.then(response => new Profile(
this.kuzzle,
response.result._id,
response.result._source));
}

createRestrictedUser (body, _id = null, options = {}) {
Expand Down Expand Up @@ -247,11 +253,11 @@ class SecurityController extends BaseController {
}

getProfile (_id, options = {}) {
return this.query({
_id,
action: 'getProfile'
}, options)
.then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies));
return this.query({_id, action: 'getProfile'}, options)
.then(response => new Profile(
this.kuzzle,
response.result._id,
response.result._source));
}

getProfileMapping (options = {}) {
Expand Down Expand Up @@ -347,11 +353,9 @@ class SecurityController extends BaseController {
}

mGetProfiles (ids, options = {}) {
return this.query({
action: 'mGetProfiles',
body: {ids}
}, options)
.then(response => response.result.hits.map(hit => new Profile(this.kuzzle, hit._id , hit._source.policies)));
return this.query({action: 'mGetProfiles', body: {ids}}, options)
.then(response => response.result.hits.map(
hit => new Profile(this.kuzzle, hit._id, hit._source)));
}

mGetUsers (ids, options = {}) {
Expand Down Expand Up @@ -440,8 +444,12 @@ class SecurityController extends BaseController {
body,
action: 'updateProfile'
};

return this.query(request, options)
.then(response => new Profile(this.kuzzle, response.result._id, response.result._source.policies));
.then(response => new Profile(
this.kuzzle,
response.result._id,
response.result._source));
}

updateProfileMapping (body, options = {}) {
Expand Down
8 changes: 5 additions & 3 deletions src/core/searchResult/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const Profile = require('../security/Profile');
const SearchResultBase = require('./SearchResultBase');

class ProfileSearchResult extends SearchResultBase {

constructor (kuzzle, request, options, response) {
super(kuzzle, request, options, response);

this._searchAction = 'searchProfiles';
this._scrollAction = 'scrollProfiles';
this.hits = response.hits.map(hit => new Profile(this._kuzzle, hit._id, hit._source.policies));
this.hits = response.hits.map(
hit => new Profile(this._kuzzle, hit._id, hit._source));
}

next () {
Expand All @@ -18,7 +18,9 @@ class ProfileSearchResult extends SearchResultBase {
return null;
}

nextSearchResult.hits = nextSearchResult._response.hits.map(hit => new Profile(nextSearchResult._kuzzle, hit._id, hit._source.policies));
nextSearchResult.hits = nextSearchResult._response.hits.map(
hit => new Profile(nextSearchResult._kuzzle, hit._id, hit._source));

return nextSearchResult;
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/security/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class Profile {
* @param {Kuzzle} kuzzle
* @param {Object} data
*/
constructor (kuzzle, _id = null, policies = []) {
constructor (kuzzle, _id = null, content = null) {
this._kuzzle = kuzzle;
this._id = _id;
this.policies = policies;
this.rateLimit = content && content.rateLimit ? content.rateLimit : 0;
this.policies = content && content.policies ? content.policies : [];
}

get kuzzle () {
Expand All @@ -21,7 +22,10 @@ class Profile {
if (!this.policies || this.policies.length === 0) {
return Promise.resolve([]);
}
return this.kuzzle.security.mGetRoles(this.policies.map(policy => policy.roleId), options);

return this.kuzzle.security.mGetRoles(
this.policies.map(policy => policy.roleId),
options);
}
}

Expand Down
19 changes: 9 additions & 10 deletions test/controllers/security.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const
SecurityController = require('../../src/controllers/Security'),
Profile = require('../../src/core/security/Profile'),
Role = require('../../src/core/security/Role'),
User = require('../../src/core/security/User'),
ProfileSearchResult = require('../../src/core/searchResult/Profile'),
RoleSearchResult = require('../../src/core/searchResult/Role'),
UserSearchResult = require('../../src/core/searchResult/User'),
sinon = require('sinon'),
should = require('should');
const SecurityController = require('../../src/controllers/Security');
const Profile = require('../../src/core/security/Profile');
const Role = require('../../src/core/security/Role');
const User = require('../../src/core/security/User');
const ProfileSearchResult = require('../../src/core/searchResult/Profile');
const RoleSearchResult = require('../../src/core/searchResult/Role');
const UserSearchResult = require('../../src/core/searchResult/User');
const sinon = require('sinon');
const should = require('should');

describe('Security Controller', () => {
const options = {opt: 'in'};
Expand Down
52 changes: 45 additions & 7 deletions test/core/security/profile.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
const
Profile = require('../../../src/core/security/Profile'),
sinon = require('sinon'),
should = require('should');
const Profile = require('../../../src/core/security/Profile');
const sinon = require('sinon');
const should = require('should');

describe('Profile', () => {
let profile;
let kuzzleMock;

beforeEach(() => {
profile = new Profile({
kuzzleMock = {
security: {
mGetRoles: sinon.stub().resolves([
{_id: 'role1', controllers: ['foo', 'bar']},
{_id: 'role2', controllers: ['foo', 'baz']}
])
}
};
});

describe('profile class', () => {
it('should initialize a null profile with the correct default values', () => {
const profile = new Profile(kuzzleMock);

should(profile._id).be.null();
should(profile.policies).be.Array().and.be.empty();
should(profile.rateLimit).eql(0);
});

it('should initialize an empty profile with the correct default values', () => {
const profile = new Profile(kuzzleMock, 'foo', {});

should(profile._id).eql('foo');
should(profile.policies).be.Array().and.be.empty();
should(profile.rateLimit).eql(0);
});

it('should initialize itself properly from a Kuzzle profile document', () => {
const policies = [
{ oh: 'noes' },
{ foo: 'bar' },
];
const profile = new Profile(kuzzleMock, 'foo', {
policies,
rateLimit: 123,
});

should(profile._id).eql('foo');
should(profile.policies).eql(policies);
should(profile.rateLimit).eql(123);
});
});

describe('getRoles', () => {
let profile;

beforeEach(() => {
profile = new Profile(kuzzleMock);
});

it('should return a Promise which resolves to an empty array if no profile is attached', () => {
return profile.getRoles()
.then(roles => {
Expand All @@ -44,4 +82,4 @@ describe('Profile', () => {
});
});
});
});
});