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-05-16 06:29:42 +04:00
|
|
|
return Models.User.findOne({id: id}, { 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();
|
|
|
|
|
|
|
|
// TODO: using 'Owner' as return value is a bit hacky.
|
2014-07-22 00:50:43 +04:00
|
|
|
if (_.find(user.roles, { 'name': 'Owner' })) {
|
2014-07-09 15:34:38 +04:00
|
|
|
return 'Owner';
|
|
|
|
}
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return allPerms;
|
|
|
|
}, 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 [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundApp.related('permissions').models;
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = effective;
|