parent
ec5cdd178a
commit
4234057821
@ -0,0 +1,8 @@
|
||||
|
||||
|
||||
var privileges = {};
|
||||
|
||||
require('./privileges/posts')(privileges);
|
||||
|
||||
|
||||
module.exports = privileges;
|
@ -0,0 +1,66 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
|
||||
meta = require('../meta'),
|
||||
user = require('../user'),
|
||||
groups = require('../groups'),
|
||||
categories = require('../categories');
|
||||
|
||||
var helpers = {};
|
||||
|
||||
helpers.allowedTo = function(privilege, uid, cid, callback) {
|
||||
categories.getCategoryField(cid, 'disabled', function(err, disabled) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (parseInt(disabled, 10) === 1) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
hasUserPrivilege: function(next) {
|
||||
hasPrivilege(groups.isMember, 'cid:' + cid + ':privileges:' + privilege, uid, next);
|
||||
},
|
||||
hasGroupPrivilege: function(next) {
|
||||
hasPrivilege(groups.isMemberOfGroupList, 'cid:' + cid + ':privileges:groups:' + privilege, uid, next);
|
||||
},
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(null, results.hasUserPrivilege && results.hasGroupPrivilege);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function hasPrivilege(method, group, uid, callback) {
|
||||
groups.exists(group, function(err, exists) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!exists) {
|
||||
return callback(null, true);
|
||||
}
|
||||
|
||||
method(group, uid, callback);
|
||||
});
|
||||
}
|
||||
|
||||
helpers.hasEnoughReputationFor = function(privilege, uid, callback) {
|
||||
if (parseInt(meta.config['privileges:disabled'], 10)) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
user.getUserField(uid, 'reputation', function(err, reputation) {
|
||||
if (err) {
|
||||
return callback(null, false);
|
||||
}
|
||||
callback(null, parseInt(reputation, 10) >= parseInt(meta.config[privilege], 10));
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = helpers;
|
@ -0,0 +1,112 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
|
||||
posts = require('../posts'),
|
||||
user = require('../user'),
|
||||
helpers = require('./helpers'),
|
||||
groups = require('../groups'),
|
||||
categories = require('../categories');
|
||||
|
||||
module.exports = function(privileges) {
|
||||
|
||||
privileges.posts = {};
|
||||
|
||||
privileges.posts.get = function(pid, uid, callback) {
|
||||
|
||||
async.parallel({
|
||||
isOwner: function(next) {
|
||||
posts.isOwner(pid, uid, next);
|
||||
},
|
||||
manage_content: function(next) {
|
||||
helpers.hasEnoughReputationFor('privileges:manage_content', uid, next);
|
||||
},
|
||||
manage_topic: function(next) {
|
||||
helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next);
|
||||
},
|
||||
isAdministrator: function(next) {
|
||||
user.isAdministrator(uid, next);
|
||||
},
|
||||
isModerator: function(next) {
|
||||
posts.getCidByPid(pid, function(err, cid) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
user.isModerator(uid, cid, next);
|
||||
});
|
||||
}
|
||||
}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var editable = results.isAdministrator || results.isModerator || results.manage_content || results.manage_topic || results.isOwner;
|
||||
|
||||
callback(null, {
|
||||
meta: {
|
||||
editable: editable,
|
||||
view_deleted: editable,
|
||||
move: results.isAdministrator || results.isModerator
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
privileges.posts.canRead = function(pid, uid, callback) {
|
||||
posts.getCidByPid(pid, function(err, cid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.some([
|
||||
function(next) {
|
||||
helpers.allowedTo('read', uid, cid, next);
|
||||
},
|
||||
function(next) {
|
||||
user.isModerator(uid, cid, next);
|
||||
},
|
||||
function(next) {
|
||||
user.isAdministrator(uid, next);
|
||||
}
|
||||
], function(task, next) {
|
||||
task(function(err, result) {
|
||||
next(!err && result);
|
||||
});
|
||||
}, function(result) {
|
||||
callback(null, result);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
privileges.posts.canEdit = function(pid, uid, callback) {
|
||||
async.some([
|
||||
function(next) {
|
||||
posts.isOwner(pid, uid, next);
|
||||
},
|
||||
function(next) {
|
||||
helpers.hasEnoughReputationFor('privileges:manage_content', uid, next);
|
||||
},
|
||||
function(next) {
|
||||
helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next);
|
||||
},
|
||||
function(next) {
|
||||
posts.getCidByPid(pid, function(err, cid) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
user.isModerator(uid, cid, next);
|
||||
});
|
||||
},
|
||||
function(next) {
|
||||
user.isAdministrator(uid, next);
|
||||
}
|
||||
], function(task, next) {
|
||||
task(function(err, result) {
|
||||
next(!err && result);
|
||||
});
|
||||
}, function(result) {
|
||||
callback(null, result);
|
||||
});
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue