joola.io.engine
Version:
joola.io's Framework Engine
72 lines (66 loc) • 2.06 kB
JavaScript
/**
* joola.io
*
* Copyright Joola Smart Solutions, Ltd. <info@joo.la>
*
* Licensed under GNU General Public License 3.0 or later.
* Some rights reserved. See LICENSE, AUTHORS.
*
* @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
*/
var
joola = global.joola, //for jslint
_roles = require('../objects/roles');
exports.validate = function (connection, permission, next) {
try {
joola.logger.debug('checking permission [' + permission + ']...');
api.session.load(connection, function (error, session) {
var pFound;
if (session && session.loggedIn) {
if (session.user) {
_.each(session.user.roles, function (role) {
if (!pFound) {
role = _roles.get(role);
pFound = _.find(role.permissions, function (permission) {
return permission.id == permission;
})
}
});
if (pFound) {
}
else {
joola.logger.warn('User does not have permission for this action.');
connection.response.error = 'Action not allowed';
connection.error = new Error("none of the required params for this action were provided.");
//throw new Error("[Authentication: User does not have permission for this action.]");
}
}
else {
joola.logger.warn('User does not have a valid authenticated session.');
connection.response.error = 'Authentication failed';
return next(connection, true);
}
}
else {
joola.logger.warn('User does not have a valid authenticated session.');
connection.response.error = 'Authentication failed';
return next(connection, true);
}
});
}
catch (ex) {
console.log('error');
throw ex;
}
};
exports.hasRole = function (roleList, rolesToCheck) {
var found;
_.each(roleList, function (role) {
if (!found) {
if (rolesToCheck.indexOf(role) > -1) {
found = role;
}
}
});
return found;
}