Files
NodeBB/src/privileges/categories.js

356 lines
10 KiB
JavaScript
Raw Normal View History

'use strict';
var async = require('async');
2017-05-26 01:39:40 -06:00
var _ = require('lodash');
var categories = require('../categories');
var user = require('../user');
var groups = require('../groups');
var helpers = require('./helpers');
var plugins = require('../plugins');
module.exports = function (privileges) {
privileges.categories = {};
privileges.categories.list = function (cid, callback) {
2015-09-15 19:21:24 -04:00
// Method used in admin/category controller to show all users/groups with privs in that given cid
2017-05-25 21:17:20 -04:00
async.waterfall([
function (next) {
async.parallel({
2017-05-25 21:17:20 -04:00
labels: function (next) {
async.parallel({
2017-06-16 14:25:40 -04:00
users: async.apply(plugins.fireHook, 'filter:privileges.list_human', privileges.privilegeLabels),
groups: async.apply(plugins.fireHook, 'filter:privileges.groups.list_human', privileges.privilegeLabels),
2017-05-25 21:17:20 -04:00
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-25 21:17:20 -04:00
users: function (next) {
var userPrivileges;
var memberSets;
async.waterfall([
async.apply(plugins.fireHook, 'filter:privileges.list', privileges.userPrivilegeList),
function (_privs, next) {
userPrivileges = _privs;
groups.getMembersOfGroups(userPrivileges.map(function (privilege) {
return 'cid:' + cid + ':privileges:' + privilege;
}), next);
},
function (_memberSets, next) {
memberSets = _memberSets.map(function (set) {
return set.map(function (uid) {
return parseInt(uid, 10);
});
});
2017-05-26 01:39:40 -06:00
var members = _.uniq(_.flatten(memberSets));
2017-05-25 21:17:20 -04:00
user.getUsersFields(members, ['picture', 'username'], next);
},
function (memberData, next) {
memberData.forEach(function (member) {
member.privileges = {};
for (var x = 0, numPrivs = userPrivileges.length; x < numPrivs; x += 1) {
member.privileges[userPrivileges[x]] = memberSets[x].indexOf(parseInt(member.uid, 10)) !== -1;
}
});
next(null, memberData);
},
], next);
},
2017-05-25 21:17:20 -04:00
groups: function (next) {
var groupPrivileges;
async.waterfall([
async.apply(plugins.fireHook, 'filter:privileges.groups.list', privileges.groupPrivilegeList),
function (_privs, next) {
groupPrivileges = _privs;
async.parallel({
memberSets: function (next) {
groups.getMembersOfGroups(groupPrivileges.map(function (privilege) {
return 'cid:' + cid + ':privileges:' + privilege;
}), next);
},
groupNames: function (next) {
groups.getGroups('groups:createtime', 0, -1, next);
},
}, next);
},
function (results, next) {
var memberSets = results.memberSets;
2017-05-26 01:39:40 -06:00
var uniqueGroups = _.uniq(_.flatten(memberSets));
2017-05-25 21:17:20 -04:00
var groupNames = results.groupNames.filter(function (groupName) {
return groupName.indexOf(':privileges:') === -1 && uniqueGroups.indexOf(groupName) !== -1;
});
groupNames = groups.ephemeralGroups.concat(groupNames);
var registeredUsersIndex = groupNames.indexOf('registered-users');
if (registeredUsersIndex !== -1) {
groupNames.splice(0, 0, groupNames.splice(registeredUsersIndex, 1)[0]);
} else {
groupNames = ['registered-users'].concat(groupNames);
}
2017-05-25 21:17:20 -04:00
var adminIndex = groupNames.indexOf('administrators');
if (adminIndex !== -1) {
groupNames.splice(adminIndex, 1);
}
2017-05-25 21:17:20 -04:00
var memberPrivs;
var memberData = groupNames.map(function (member) {
memberPrivs = {};
for (var x = 0, numPrivs = groupPrivileges.length; x < numPrivs; x += 1) {
memberPrivs[groupPrivileges[x]] = memberSets[x].indexOf(member) !== -1;
}
return {
name: member,
privileges: memberPrivs,
};
});
next(null, memberData);
},
function (memberData, next) {
// Grab privacy info for the groups as well
async.map(memberData, function (member, next) {
async.waterfall([
function (next) {
groups.isPrivate(member.name, next);
},
function (isPrivate, next) {
member.isPrivate = isPrivate;
2017-05-28 01:06:26 -04:00
next(null, member);
2017-05-25 21:17:20 -04:00
},
], next);
}, next);
},
], next);
2017-02-17 19:31:21 -07:00
},
2017-05-25 21:17:20 -04:00
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-25 21:17:20 -04:00
function (payload, next) {
// This is a hack because I can't do {labels.users.length} to echo the count in templates.js
payload.columnCount = payload.labels.users.length + 2;
next(null, payload);
},
], callback);
};
privileges.categories.get = function (cid, uid, callback) {
2017-06-16 14:25:40 -04:00
var privs = ['topics:create', 'topics:read', 'topics:tag', 'read'];
2017-03-03 21:04:01 +03:00
async.waterfall([
function (next) {
async.parallel({
privileges: function (next) {
helpers.isUserAllowedTo(privs, uid, cid, next);
},
isAdministrator: function (next) {
user.isAdministrator(uid, next);
},
isModerator: function (next) {
user.isModerator(uid, cid, next);
},
}, next);
2014-05-15 20:49:47 -04:00
},
2017-03-03 21:04:01 +03:00
function (results, next) {
var privData = _.zipObject(privs, results.privileges);
2017-03-03 21:04:01 +03:00
var isAdminOrMod = results.isAdministrator || results.isModerator;
plugins.fireHook('filter:privileges.categories.get', {
'topics:create': privData['topics:create'] || isAdminOrMod,
'topics:read': privData['topics:read'] || isAdminOrMod,
2017-06-16 14:25:40 -04:00
'topics:tag': privData['topics:tag'] || isAdminOrMod,
2017-03-03 21:04:01 +03:00
read: privData.read || isAdminOrMod,
cid: cid,
uid: uid,
editable: isAdminOrMod,
view_deleted: isAdminOrMod,
isAdminOrMod: isAdminOrMod,
}, next);
2017-02-17 19:31:21 -07:00
},
2017-03-03 21:04:01 +03:00
], callback);
2014-05-15 20:49:47 -04:00
};
privileges.categories.isAdminOrMod = function (cid, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, false);
}
2015-09-15 18:21:17 -04:00
helpers.some([
function (next) {
user.isModerator(uid, cid, next);
},
function (next) {
user.isAdministrator(uid, next);
2017-02-17 19:31:21 -07:00
},
2015-09-15 18:21:17 -04:00
], callback);
};
privileges.categories.isUserAllowedTo = function (privilege, cid, uid, callback) {
2015-09-16 08:35:40 -04:00
if (!cid) {
return callback(null, false);
}
helpers.isUserAllowedTo(privilege, uid, [cid], function (err, results) {
2015-09-16 08:35:40 -04:00
callback(err, Array.isArray(results) && results.length ? results[0] : false);
});
};
privileges.categories.can = function (privilege, cid, uid, callback) {
2015-02-24 13:02:58 -05:00
if (!cid) {
return callback(null, false);
}
2017-03-03 21:04:01 +03:00
async.waterfall([
function (next) {
categories.getCategoryField(cid, 'disabled', next);
},
function (disabled, next) {
if (parseInt(disabled, 10) === 1) {
return callback(null, false);
}
helpers.some([
function (next) {
helpers.isUserAllowedTo(privilege, uid, [cid], function (err, results) {
next(err, Array.isArray(results) && results.length ? results[0] : false);
});
},
function (next) {
user.isModerator(uid, cid, next);
},
function (next) {
user.isAdministrator(uid, next);
},
], next);
},
], callback);
};
privileges.categories.filterCids = function (privilege, cids, uid, callback) {
2014-11-09 00:33:26 -05:00
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
}
cids = cids.filter(function (cid, index, array) {
return array.indexOf(cid) === index;
});
2017-03-03 21:04:01 +03:00
async.waterfall([
function (next) {
privileges.categories.getBase(privilege, cids, uid, next);
},
function (results, next) {
cids = cids.filter(function (cid, index) {
return !results.categories[index].disabled &&
(results.allowedTo[index] || results.isAdmin || results.isModerators[index]);
});
2016-04-29 20:35:49 +03:00
2017-03-03 21:04:01 +03:00
next(null, cids.filter(Boolean));
},
], callback);
2016-04-29 20:35:49 +03:00
};
privileges.categories.getBase = function (privilege, cids, uid, callback) {
async.parallel({
categories: function (next) {
2015-09-25 17:38:58 -04:00
categories.getCategoriesFields(cids, ['disabled'], next);
2015-02-25 14:17:30 -05:00
},
allowedTo: function (next) {
helpers.isUserAllowedTo(privilege, uid, cids, next);
},
isModerators: function (next) {
user.isModerator(uid, cids, next);
},
isAdmin: function (next) {
user.isAdministrator(uid, next);
2017-02-17 19:31:21 -07:00
},
2016-04-29 20:35:49 +03:00
}, callback);
};
privileges.categories.filterUids = function (privilege, cid, uids, callback) {
if (!uids.length) {
return callback(null, []);
}
2017-06-25 19:05:13 -04:00
uids = _.uniq(uids);
2017-03-03 21:04:01 +03:00
async.waterfall([
function (next) {
async.parallel({
allowedTo: function (next) {
helpers.isUsersAllowedTo(privilege, uids, cid, next);
},
isModerators: function (next) {
user.isModerator(uids, cid, next);
},
2017-06-25 19:05:13 -04:00
isAdmins: function (next) {
2017-03-03 21:04:01 +03:00
user.isAdministrator(uids, next);
},
}, next);
},
2017-03-03 21:04:01 +03:00
function (results, next) {
uids = uids.filter(function (uid, index) {
2017-06-25 19:05:13 -04:00
return results.allowedTo[index] || results.isModerators[index] || results.isAdmins[index];
2017-03-03 21:04:01 +03:00
});
next(null, uids);
2017-02-17 19:31:21 -07:00
},
2017-03-03 21:04:01 +03:00
], callback);
};
privileges.categories.give = function (privileges, cid, groupName, callback) {
2015-09-27 15:21:23 -04:00
giveOrRescind(groups.join, privileges, cid, groupName, callback);
};
privileges.categories.rescind = function (privileges, cid, groupName, callback) {
2015-09-27 15:21:23 -04:00
giveOrRescind(groups.leave, privileges, cid, groupName, callback);
2015-09-27 15:02:04 -04:00
};
function giveOrRescind(method, privileges, cid, groupName, callback) {
async.each(privileges, function (privilege, next) {
2015-09-27 15:02:04 -04:00
method('cid:' + cid + ':privileges:groups:' + privilege, groupName, next);
2015-01-16 17:03:05 -05:00
}, callback);
2015-09-27 15:02:04 -04:00
}
2015-01-16 17:03:05 -05:00
privileges.categories.canMoveAllTopics = function (currentCid, targetCid, uid, callback) {
2017-03-03 21:04:01 +03:00
async.waterfall([
function (next) {
async.parallel({
isAdministrator: function (next) {
user.isAdministrator(uid, next);
},
moderatorOfCurrent: function (next) {
user.isModerator(uid, currentCid, next);
},
moderatorOfTarget: function (next) {
user.isModerator(uid, targetCid, next);
},
}, next);
2014-05-15 20:49:47 -04:00
},
2017-03-03 21:04:01 +03:00
function (results, next) {
next(null, results.isAdministrator || (results.moderatorOfCurrent && results.moderatorOfTarget));
2017-02-17 19:31:21 -07:00
},
2017-03-03 21:04:01 +03:00
], callback);
2014-05-15 20:49:47 -04:00
};
privileges.categories.userPrivileges = function (cid, uid, callback) {
var tasks = {};
privileges.userPrivilegeList.forEach(function (privilege) {
tasks[privilege] = async.apply(groups.isMember, uid, 'cid:' + cid + ':privileges:' + privilege);
});
async.parallel(tasks, callback);
2014-05-15 20:49:47 -04:00
};
privileges.categories.groupPrivileges = function (cid, groupName, callback) {
var tasks = {};
privileges.groupPrivilegeList.forEach(function (privilege) {
tasks[privilege] = async.apply(groups.isMember, groupName, 'cid:' + cid + ':privileges:' + privilege);
});
async.parallel(tasks, callback);
2014-05-15 20:49:47 -04:00
};
2017-02-18 02:30:48 -07:00
};