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.

106 lines
2.5 KiB
JavaScript

'use strict';
var async = require('async'),
topics = require('../topics'),
user = require('../user'),
helpers = require('./helpers'),
groups = require('../groups'),
categories = require('../categories');
module.exports = function(privileges) {
privileges.topics = {};
privileges.topics.get = function(tid, uid, callback) {
11 years ago
topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
11 years ago
async.parallel({
'topics:reply': function(next) {
helpers.allowedTo('topics:reply', uid, cid, next);
},
read: function(next) {
helpers.allowedTo('read', uid, cid, next);
},
11 years ago
isOwner: function(next) {
topics.isOwner(tid, uid, next);
},
manage_topic: function(next) {
helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next);
},
isAdministrator: function(next) {
user.isAdministrator(uid, next);
},
isModerator: function(next) {
user.isModerator(uid, cid, next);
}
}, function(err, results) {
if(err) {
return callback(err);
}
11 years ago
var isAdminOrMod = results.isAdministrator || results.isModerator;
var editable = isAdminOrMod || results.manage_topic;
var deletable = isAdminOrMod || results.isOwner;
callback(null, {
11 years ago
'topics:reply': results['topics:reply'],
11 years ago
read: results.read,
view_thread_tools: editable || deletable,
11 years ago
editable: editable,
11 years ago
deletable: deletable,
view_deleted: isAdminOrMod || results.manage_topic || results.isOwner
});
});
});
};
privileges.topics.can = function(privilege, tid, uid, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return callback(err);
}
privileges.categories.can(privilege, cid, uid, callback);
});
};
privileges.topics.canEdit = function(tid, uid, callback) {
helpers.some([
function(next) {
11 years ago
topics.isOwner(tid, uid, next);
},
function(next) {
11 years ago
helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next);
},
function(next) {
11 years ago
isAdminOrMod(tid, uid, next);
}
], callback);
};
privileges.topics.canMove = function(tid, uid, callback) {
11 years ago
isAdminOrMod(tid, uid, callback);
};
function isAdminOrMod(tid, uid, callback) {
helpers.some([
function(next) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if (err) {
return next(err);
}
user.isModerator(uid, cid, next);
});
},
function(next) {
user.isAdministrator(uid, next);
}
], callback);
11 years ago
}
};