You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.8 KiB
JavaScript

9 years ago
'use strict';
9 years ago
var async = require('async');
9 years ago
var groups = require('../groups');
var plugins = require('../plugins');
9 years ago
module.exports = function(privileges) {
privileges.users = {};
privileges.users.isAdministrator = function(uid, callback) {
if (Array.isArray(uid)) {
groups.isMembers(uid, 'administrators', callback);
} else {
groups.isMember(uid, 'administrators', callback);
}
};
9 years ago
privileges.users.isGlobalModerator = function(uid, callback) {
if (Array.isArray(uid)) {
groups.isMembers(uid, 'Global Moderators', callback);
} else {
groups.isMember(uid, 'Global Moderators', callback);
}
};
9 years ago
privileges.users.isModerator = function(uid, cid, callback) {
if (Array.isArray(cid)) {
isModeratorOfCategories(cid, uid, callback);
} else {
if (Array.isArray(uid)) {
isModeratorsOfCategory(cid, uid, callback);
} else {
isModeratorOfCategory(cid, uid, callback);
}
}
};
function isModeratorOfCategories(cids, uid, callback) {
if (!parseInt(uid, 10)) {
9 years ago
return filterIsModerator(cids, uid, cids.map(function() {return false;}), callback);
9 years ago
}
9 years ago
privileges.users.isGlobalModerator(uid, function(err, isGlobalModerator) {
9 years ago
if (err) {
return callback(err);
}
9 years ago
if (isGlobalModerator) {
return filterIsModerator(cids, uid, cids.map(function() {return true;}), callback);
}
9 years ago
9 years ago
var uniqueCids = cids.filter(function(cid, index, array) {
return array.indexOf(cid) === index;
9 years ago
});
9 years ago
var groupNames = uniqueCids.map(function(cid) {
return 'cid:' + cid + ':privileges:mods'; // At some point we should *probably* change this to "moderate" as well
});
var groupListNames = uniqueCids.map(function(cid) {
return 'cid:' + cid + ':privileges:groups:moderate';
9 years ago
});
9 years ago
async.parallel({
user: async.apply(groups.isMemberOfGroups, uid, groupNames),
group: async.apply(groups.isMemberOfGroupsList, uid, groupListNames)
}, function(err, checks) {
if (err) {
return callback(err);
}
var isMembers = checks.user.map(function(isMember, idx) {
return isMember || checks.group[idx];
}),
map = {};
uniqueCids.forEach(function(cid, index) {
map[cid] = isMembers[index];
});
var isModerator = cids.map(function(cid) {
return map[cid];
});
filterIsModerator(cids, uid, isModerator, callback);
});
9 years ago
});
}
function isModeratorsOfCategory(cid, uids, callback) {
async.parallel([
9 years ago
async.apply(privileges.users.isGlobalModerator, uids),
9 years ago
async.apply(groups.isMembers, uids, 'cid:' + cid + ':privileges:mods'),
async.apply(groups.isMembersOfGroupList, uids, 'cid:' + cid + ':privileges:groups:moderate')
], function(err, checks) {
if (err) {
return callback(err);
}
var isModerator = checks[0].map(function(isMember, idx) {
9 years ago
return isMember || checks[1][idx] || checks[2][idx];
});
9 years ago
filterIsModerator(cid, uids, isModerator, callback);
});
}
function isModeratorOfCategory(cid, uid, callback) {
async.parallel([
9 years ago
async.apply(privileges.users.isGlobalModerator, uid),
9 years ago
async.apply(groups.isMember, uid, 'cid:' + cid + ':privileges:mods'),
async.apply(groups.isMemberOfGroupList, uid, 'cid:' + cid + ':privileges:groups:moderate')
], function(err, checks) {
if (err) {
return callback(err);
}
9 years ago
var isModerator = checks[0] || checks[1] || checks[2];
9 years ago
filterIsModerator(cid, uid, isModerator, callback);
});
}
function filterIsModerator(cid, uid, isModerator, callback) {
plugins.fireHook('filter:user.isModerator', {uid: uid, cid: cid, isModerator: isModerator}, function(err, data) {
if (err) {
return callback(err);
}
if (Array.isArray(uid) && !Array.isArray(data.isModerator) || Array.isArray(cid) && !Array.isArray(data.isModerator)) {
return callback(new Error('filter:user.isModerator - i/o mismatch'));
}
callback(null, data.isModerator);
});
}
};