2014-03-20 09:12:49 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2013-11-27 11:27:20 -05:00
|
|
|
var Groups = require('./groups'),
|
|
|
|
|
User = require('./user'),
|
2014-04-16 14:43:25 -04:00
|
|
|
categories = require('./categories'),
|
2013-11-27 11:27:20 -05:00
|
|
|
|
|
|
|
|
async = require('async'),
|
2014-04-08 20:15:40 +01:00
|
|
|
db = require('./database');
|
2013-11-27 11:27:20 -05:00
|
|
|
|
2014-04-08 20:15:40 +01:00
|
|
|
var internals = {
|
|
|
|
|
isMember: function(key, candidate, next){
|
|
|
|
|
Groups.exists(key, function(err, exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
Groups.isMember(candidate, key, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isMemberOfGroupList: function(key, candidate, next){
|
|
|
|
|
Groups.exists(key, function(err, exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
Groups.isMemberOfGroupList(candidate, key, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var CategoryTools = {};
|
2013-11-27 11:27:20 -05:00
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
CategoryTools.exists = function(cid, callback) {
|
|
|
|
|
db.isSortedSetMember('categories:cid', cid, callback);
|
|
|
|
|
};
|
|
|
|
|
|
2013-11-27 11:27:20 -05:00
|
|
|
CategoryTools.privileges = function(cid, uid, callback) {
|
|
|
|
|
async.parallel({
|
2014-05-11 23:06:40 -04:00
|
|
|
"disabled": function(next) {
|
2014-04-16 14:43:25 -04:00
|
|
|
categories.getCategoryField(cid, 'disabled', next);
|
|
|
|
|
},
|
2014-05-08 16:51:30 -04:00
|
|
|
read: function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:read', uid, next);
|
2013-11-27 11:27:20 -05:00
|
|
|
},
|
2014-05-08 16:51:30 -04:00
|
|
|
"topics:create": function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:topics:create', uid, next);
|
2013-11-27 11:27:20 -05:00
|
|
|
},
|
2014-05-08 16:51:30 -04:00
|
|
|
"topics:reply": function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:topics:reply', uid, next);
|
2014-01-08 21:24:43 -05:00
|
|
|
},
|
2014-05-11 23:06:40 -04:00
|
|
|
"groups:read": function(next) {
|
|
|
|
|
internals.isMemberOfGroupList('cid:' + cid + ':privileges:groups:read', uid, next);
|
|
|
|
|
},
|
|
|
|
|
"groups:topics:create": function(next) {
|
|
|
|
|
internals.isMemberOfGroupList('cid:' + cid + ':privileges:groups:topics:create', uid, next);
|
|
|
|
|
},
|
|
|
|
|
"groups:topics:reply": function(next) {
|
|
|
|
|
internals.isMemberOfGroupList('cid:' + cid + ':privileges:groups:topics:reply', uid, next);
|
|
|
|
|
},
|
|
|
|
|
mods: function(next) {
|
2013-11-27 11:27:20 -05:00
|
|
|
User.isModerator(uid, cid, next);
|
|
|
|
|
},
|
|
|
|
|
admin: function(next) {
|
|
|
|
|
User.isAdministrator(uid, next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, privileges) {
|
2014-05-11 23:06:40 -04:00
|
|
|
if (privileges) {
|
|
|
|
|
privileges.meta = {
|
|
|
|
|
read: (
|
|
|
|
|
(
|
|
|
|
|
parseInt(privileges.disabled, 10) !== 1 &&
|
|
|
|
|
(privileges.read || privileges.read === null) &&
|
|
|
|
|
(privileges['groups:read'] || privileges['groups:read'] === null)
|
|
|
|
|
) ||
|
|
|
|
|
privileges.mods ||
|
|
|
|
|
privileges.admin
|
|
|
|
|
),
|
|
|
|
|
editable: privileges.mods || privileges.admin,
|
|
|
|
|
view_deleted: privileges.mods || privileges.admin
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(err, privileges || null);
|
2013-11-27 11:27:20 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-20 16:26:00 -04:00
|
|
|
CategoryTools.groupPrivileges = function(cid, groupName, callback) {
|
2013-12-17 20:34:21 +00:00
|
|
|
async.parallel({
|
2014-05-08 16:51:30 -04:00
|
|
|
"groups:read":function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:groups:read', groupName, function(err, isMember){
|
2014-04-08 20:15:40 +01:00
|
|
|
next(err, !!isMember);
|
2013-12-17 20:34:21 +00:00
|
|
|
});
|
|
|
|
|
},
|
2014-05-08 16:51:30 -04:00
|
|
|
"groups:topics:create":function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:groups:topics:create', groupName, function(err, isMember){
|
|
|
|
|
next(err, !!isMember);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
"groups:topics:reply":function(next) {
|
|
|
|
|
internals.isMember('cid:' + cid + ':privileges:groups:topics:reply', groupName, function(err, isMember){
|
2014-04-08 20:15:40 +01:00
|
|
|
next(err, !!isMember);
|
2013-12-17 20:34:21 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, function(err, privileges) {
|
2014-05-08 16:51:30 -04:00
|
|
|
callback(err, privileges || null);
|
2013-12-17 20:34:21 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = CategoryTools;
|