3 var logger = require('gitter-web-env').logger;
4 var userService = require('gitter-web-users');
5 var Promise = require('bluebird');
6 var policyFactory = require('gitter-web-permissions/lib/policy-factory');
9 * Returns the permissions the user has in the orgs.
10 * This is not intended to be used for large sets, rather individual items
12 function TroupePermissionsStrategy(options) {
13 this.currentUser = options.currentUser;
14 this.currentUserId = options.currentUserId;
18 TroupePermissionsStrategy.prototype = {
19 preload: function(troupes) {
20 if (troupes.isEmpty()) return;
22 var currentUser = this.currentUser;
23 var currentUserId = this.currentUserId;
25 return Promise.try(function() {
31 return userService.findById(currentUserId);
35 .then(function(user) {
36 // setup this.isAdmin _before_ possibly returning, otherwise map will npe
37 var isAdmin = (this.isAdmin = {});
41 return Promise.map(troupes.toArray(), function(troupe) {
43 .createPolicyForRoom(user, troupe)
44 .then(function(policy) {
45 return policy.canAdmin();
47 .then(function(admin) {
48 isAdmin[troupe.id] = admin;
50 .catch(function(err) {
51 // Fallback in case of GitHub API downtime
52 logger.error('Unable to obtain admin permissions', { exception: err });
53 isAdmin[troupe.id] = false;
59 map: function(troupe) {
61 admin: this.isAdmin[troupe.id] || false
65 name: 'TroupePermissionsStrategy'
68 module.exports = TroupePermissionsStrategy;