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
486 changes: 185 additions & 301 deletions dist/kuzzle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/kuzzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kuzzle.min.map

Large diffs are not rendered by default.

113 changes: 25 additions & 88 deletions src/security/kuzzleProfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var
KuzzleSecurityDocument = require('./kuzzleSecurityDocument'),
KuzzleRole = require('./kuzzleRole');
KuzzleSecurityDocument = require('./kuzzleSecurityDocument');

function KuzzleProfile(kuzzleSecurity, id, content) {

Expand All @@ -17,17 +16,6 @@ function KuzzleProfile(kuzzleSecurity, id, content) {
}
});

// Hydrate profile with roles if roles are not only string but objects with `_id` and `_source`
if (content && content.roles) {
content.roles = content.roles.map(function (role) {
if (!role._id || !role._source) {
return role;
}

return new KuzzleRole(kuzzleSecurity, role._id, role._source);
});
}

// promisifying
if (kuzzleSecurity.kuzzle.bluebird) {
return kuzzleSecurity.kuzzle.bluebird.promisifyAll(this, {
Expand Down Expand Up @@ -60,8 +48,8 @@ KuzzleProfile.prototype.save = function (options, cb) {
data,
self = this;

if (!this.content.roles) {
throw new Error('Argument "roles" is mandatory in a profile. This argument contains an array of KuzzleRole or an array of id string');
if (!this.content.policies) {
throw new Error('Argument "policies" is mandatory in a profile. This argument contains an array of objects.');
}

if (options && cb === undefined && typeof options === 'function') {
Expand All @@ -86,89 +74,49 @@ KuzzleProfile.prototype.save = function (options, cb) {


/**
* Add a role in the roles list
* @param {KuzzleRole|string} role - can be an instance of KuzzleRole or an id in string
* Add a policy in the policies list
* @param {Object} policy - must be an object containing at least a "roleId" member which must be a string.
*
* @returns {KuzzleProfile} this
*/
KuzzleProfile.prototype.addRole = function (role) {
KuzzleProfile.prototype.addPolicy = function (policy) {

if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
throw new Error('Parameter "roles" must be a KuzzleRole or a id string');
if (typeof policy !== 'object' || typeof policy.roleId !== 'string') {
throw new Error('Parameter "policies" must be an object containing at least a "roleId" member which must be a string.');
}

if (!this.content.roles) {
this.content.roles = [];
if (!this.content.policies) {
this.content.policies = [];
}

this.content.roles.push(role);
this.content.policies.push(policy);

return this;
};

/**
* Set roles list
* @param {Array} roles - can be an array of KuzzleRole or an array of string
* Set policies list
* @param {Array} policies - must be an array of objects containing at least a "roleId" member which must be a string
*
* @returns {KuzzleProfile} this
*/
KuzzleProfile.prototype.setRoles = function (roles) {
KuzzleProfile.prototype.setPolicies = function (policies) {

if (!Array.isArray(roles)) {
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
if (!Array.isArray(policies)) {
throw new Error('Parameter "policies" must be an array of objects containing at least a "roleId" member which must be a string');
}

roles.map(function (role) {
if (typeof role !== 'string' && !(role instanceof KuzzleRole)) {
throw new Error('Parameter "roles" must be an array of KuzzleRole or an array of string');
policies.map(function (policy) {
if (typeof policy !== 'object' || typeof policy.roleId !== 'string') {
throw new Error('Parameter "policies" must be an array of objects containing at least a "roleId" member which must be a string');
}
});

this.content.roles = roles;
this.content.policies = policies;

return this;
};


/**
* Hydrate the profile - get real KuzzleRole and not just ids
* Warning: do not try to hydrate a profile with newly added role which is not created in kuzzle
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - Handles the query response
*/
KuzzleProfile.prototype.hydrate = function (options, cb) {

var
self = this,
data = {ids: []};

data.ids = this.content.roles.map(function (role) {
if (typeof role === 'string') {
return role;
}

if (role instanceof KuzzleRole) {
return role.id;
}
});

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

self.kuzzle.callbackRequired('KuzzleProfile.hydrate', cb);

self.kuzzle.query(self.kuzzleSecurity.buildQueryArgs('mGetRoles'), {body: data}, options, function (error, response) {
if (error) {
return cb(error);
}

cb(null, new KuzzleProfile(self, self.id, {roles: response.result.hits}));
});
};

/**
* Serialize this object into a JSON object
*
Expand All @@ -183,29 +131,18 @@ KuzzleProfile.prototype.serialize = function () {
}

data.body = this.content;
if (!data.body.roles || !Array.isArray(data.body.roles)) {
return data;
}

data.body.roles = data.body.roles.map(function(role) {
if (role instanceof KuzzleRole) {
return role.id;
}

return role;
});

return data;
};

/**
* Returns the list of roles associated to this profile.
* Each role element can be either a string or a KuzzleRole object
* Returns the list of policies associated to this profile.
* Each policy element is an array of objects containing at least a "roleId" member which must be a string
*
* @return {object} an array of roles
* @return {object} an array of policies
*/
KuzzleProfile.prototype.getRoles = function () {
return this.content.roles;
KuzzleProfile.prototype.getPolicies = function () {
return this.content.policies;
};

module.exports = KuzzleProfile;
78 changes: 11 additions & 67 deletions src/security/kuzzleSecurity.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ KuzzleSecurity.prototype.roleFactory = function(id, content) {
/**
* Get a specific profile from kuzzle
*
* Takes an optional argument object with the following property:
* - hydrate (boolean, default: true):
* if is set to false, return a list id in role instead of KuzzleRole.
*
* @param {string} id
* @param {object} [options] - (optional) arguments
Expand All @@ -263,21 +260,18 @@ KuzzleSecurity.prototype.roleFactory = function(id, content) {
KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jsdoc must also be restored.

var
data,
self = this,
hydrate = true;

if (!id || typeof id !== 'string') {
throw new Error('Id parameter is mandatory for getProfile function');
}
self = this;

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}
else if (options.hydrate !== undefined) {
hydrate = options.hydrate;

if (!id || typeof id !== 'string') {
throw new Error('Id parameter is mandatory for getProfile function');
}


data = {_id: id};

self.kuzzle.callbackRequired('KuzzleSecurity.getProfile', cb);
Expand All @@ -287,31 +281,13 @@ KuzzleSecurity.prototype.getProfile = function (id, options, cb) {
return cb(error);
}

if (!hydrate) {
response.result._source.roles = response.result._source.roles.map(function (role) {
var formattedRole = {_id: role._id};
if (role._source.restrictedTo !== undefined) {
formattedRole.restrictedTo = role._source.restrictedTo;
}
if (role._source.allowInternalIndex !== undefined) {
formattedRole.allowInternalIndex = role._source.allowInternalIndex;
}

return formattedRole;
});
}

cb(null, new KuzzleProfile(self, response.result._id, response.result._source));
});
};

/**
* Executes a search on profiles according to a filter
*
* Takes an optional argument object with the following property:
* - hydrate (boolean, default: true):
* if is set to false, return a list id in role instead of KuzzleRole.
* Because hydrate need to fetch all related KuzzleRole object, leave hydrate to true will have a performance cost
*
* /!\ There is a small delay between profile creation and their existence in our persistent search layer,
* usually a couple of seconds.
Expand All @@ -325,15 +301,10 @@ KuzzleSecurity.prototype.searchProfiles = function (filters, options, cb) {
var
self = this;

filters.hydrate = true;

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}
else if (options.hydrate !== undefined) {
filters.hydrate = options.hydrate;
}

self.kuzzle.callbackRequired('KuzzleSecurity.searchProfiles', cb);

Expand Down Expand Up @@ -439,13 +410,7 @@ KuzzleSecurity.prototype.updateProfile = function (id, content, options, cb) {
}

Object.keys(res.result._source).forEach(function (property) {
if (property !== 'roles') {
updatedContent[property] = res.result._source[property];
}
});

updatedContent.roles = res.result._source.roles.map(function (role) {
return role._id;
updatedContent[property] = res.result._source[property];
});

cb(null, new KuzzleProfile(self, res.result._id, updatedContent));
Expand Down Expand Up @@ -503,19 +468,14 @@ KuzzleSecurity.prototype.profileFactory = function(id, content) {
/**
* Get a specific user from kuzzle using its unique ID
*
* Takes an optional argument object with the following property:
* - hydrate (boolean, default: true):
* if is set to false, return a list id in role instead of KuzzleRole.
*
* @param {string} id
* @param {object} [options] - (optional) arguments
* @param {responseCallback} cb - returns Kuzzle's response
*/
KuzzleSecurity.prototype.getUser = function (id, options, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jsdoc must also be restored.

var
data,
self = this,
hydrate = true;
self = this;

if (!id || typeof id !== 'string') {
throw new Error('Id parameter is mandatory for getUser function');
Expand All @@ -525,9 +485,6 @@ KuzzleSecurity.prototype.getUser = function (id, options, cb) {
cb = options;
options = null;
}
else if (options.hydrate !== undefined) {
hydrate = options.hydrate;
}

data = {_id: id};

Expand All @@ -538,22 +495,13 @@ KuzzleSecurity.prototype.getUser = function (id, options, cb) {
return cb(err);
}

if (!hydrate) {
response.result._source.profile = response.result._source.profile._id;
}

cb(null, new KuzzleUser(self, response.result._id, response.result._source));
});
};

/**
* Executes a search on user according to a filter
*
* Takes an optional argument object with the following property:
* - hydrate (boolean, default: true):
* if is set to false, return a list id in role instead of KuzzleRole.
* Because hydrate need to fetch all related KuzzleRole object, leave hydrate to true will have a performance cost
*
* /!\ There is a small delay between user creation and their existence in our persistent search layer,
* usually a couple of seconds.
* That means that a user that was just been created won’t be returned by this function.
Expand All @@ -566,15 +514,10 @@ KuzzleSecurity.prototype.searchUsers = function (filters, options, cb) {
var
self = this;

filters.hydrate = true;

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}
else if (options.hydrate !== undefined) {
filters.hydrate = options.hydrate;
}

self.kuzzle.callbackRequired('KuzzleSecurity.searchUsers', cb);

Expand Down Expand Up @@ -788,16 +731,17 @@ KuzzleSecurity.prototype.isActionAllowed = function(rights, controller, action,
/**
* Gets the rights array of a given user.
*
* @param {string} userId The id of the user.
* @param {function} cb The callback containing the normalized array of rights.
* @param {string} userId The id of the user.
* @param {object} [options] - (optional) arguments
* @param {function} cb The callback containing the normalized array of rights.
*/
KuzzleSecurity.prototype.getUserRights = function (userId, options, cb) {
var
data = {_id: userId},
self = this;

if (!userId || typeof userId !== 'string') {
throw new Error('userId parameter is mandatory for isActionAllowed function');
throw new Error('userId parameter is mandatory for getUserRights function');
}

if (!cb && typeof options === 'function') {
Expand Down
Loading