From 263bbd5092c25a7dc97a68b1334af218c64f7e1b Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 10 May 2015 20:48:32 -0500 Subject: [PATCH] Fix up filtering implementation --- core/server/api/roles.js | 43 ++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/core/server/api/roles.js b/core/server/api/roles.js index 649470e63b..a166d9c1d5 100644 --- a/core/server/api/roles.js +++ b/core/server/api/roles.js @@ -1,7 +1,6 @@ // # Roles API // RESTful API for the Role resource var Promise = require('bluebird'), - _ = require('lodash'), canThis = require('../permissions').canThis, dataProvider = require('../models'), errors = require('../errors'), @@ -28,37 +27,29 @@ roles = { * @returns {Promise(Roles)} Roles Collection */ browse: function browse(options) { - var permissionMap = []; options = options || {}; return canThis(options.context).browse.role().then(function () { - return dataProvider.Role.findAll(options).then(function (foundRoles) { - if (options.permissions === 'assign') { - // Hacky implementation of filtering because when.filter is only available in when 3.4.0, - // but that's buggy and kills other tests and introduces Heisenbugs. Until we turn everything - // to Bluebird, this works. Sorry. - // TODO: replace with better filter when bluebird lands - _.each(foundRoles.toJSON(), function (role) { - permissionMap.push(canThis(options.context).assign.role(role).then(function () { - if (role.name === 'Owner') { - return null; - } - return role; - }).catch(function () { - return null; - })); - }); + return dataProvider.Role.findAll(options).then(function (results) { + var roles = results.map(function (r) { + return r.toJSON(); + }); - return Promise.all(permissionMap).then(function (resolved) { - return {roles: _.filter(resolved, function (role) { - return role !== null; - })}; - }).catch(errors.logAndThrowError); + if (options.permissions !== 'assign') { + return {roles: roles}; } - return {roles: foundRoles.toJSON()}; + + return Promise.filter(roles.map(function (role) { + return canThis(options.context).assign.role(role) + .return(role) + .catch(function () {}); + }), function (value) { + return value && value.name !== 'Owner'; + }).then(function (roles) { + return {roles: roles}; + }); }); - }) - .catch(errors.logAndThrowError); + }).catch(errors.logAndThrowError); } };