2014-02-12 07:40:39 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Models = require('../models'),
|
2014-05-16 06:29:42 +04:00
|
|
|
errors = require('../errors');
|
2014-02-12 07:40:39 +04:00
|
|
|
|
|
|
|
var effective = {
|
|
|
|
user: function (id) {
|
2014-08-05 22:11:17 +04:00
|
|
|
return Models.User.findOne({id: id, status: 'all'}, { include: ['permissions', 'roles', 'roles.permissions'] })
|
2014-02-12 07:40:39 +04:00
|
|
|
.then(function (foundUser) {
|
|
|
|
var seenPerms = {},
|
|
|
|
rolePerms = _.map(foundUser.related('roles').models, function (role) {
|
|
|
|
return role.related('permissions').models;
|
|
|
|
}),
|
2014-07-09 15:34:38 +04:00
|
|
|
allPerms = [],
|
|
|
|
user = foundUser.toJSON();
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
rolePerms.push(foundUser.related('permissions').models);
|
|
|
|
|
|
|
|
_.each(rolePerms, function (rolePermGroup) {
|
|
|
|
_.each(rolePermGroup, function (perm) {
|
|
|
|
var key = perm.get('action_type') + '-' + perm.get('object_type') + '-' + perm.get('object_id');
|
|
|
|
|
|
|
|
// Only add perms once
|
|
|
|
if (seenPerms[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
allPerms.push(perm);
|
|
|
|
seenPerms[key] = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
return {permissions: allPerms, roles: user.roles};
|
2014-02-12 07:40:39 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
},
|
|
|
|
|
|
|
|
app: function (appName) {
|
2014-05-16 06:29:42 +04:00
|
|
|
return Models.App.findOne({name: appName}, { withRelated: ['permissions'] })
|
2014-02-12 07:40:39 +04:00
|
|
|
.then(function (foundApp) {
|
|
|
|
if (!foundApp) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2014-07-23 22:17:29 +04:00
|
|
|
return {permissions: foundApp.related('permissions').models};
|
2014-02-12 07:40:39 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = effective;
|