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.
nodebb/src/groups/ownership.js

59 lines
1.6 KiB
JavaScript

'use strict';
8 years ago
var async = require('async');
var db = require('../database');
var plugins = require('../plugins');
module.exports = function (Groups) {
Groups.ownership = {};
Groups.ownership.isOwner = function (uid, groupName, callback) {
6 years ago
if (!(parseInt(uid, 10) > 0)) {
6 years ago
return setImmediate(callback, null, false);
}
db.isSetMember('group:' + groupName + ':owners', uid, callback);
};
Groups.ownership.isOwners = function (uids, groupName, callback) {
10 years ago
if (!Array.isArray(uids)) {
6 years ago
return setImmediate(callback, null, []);
10 years ago
}
db.isSetMembers('group:' + groupName + ':owners', uids, callback);
};
Groups.ownership.grant = function (toUid, groupName, callback) {
// Note: No ownership checking is done here on purpose!
10 years ago
async.waterfall([
function (next) {
10 years ago
db.setAdd('group:' + groupName + ':owners', toUid, next);
},
function (next) {
plugins.fireHook('action:group.grantOwnership', { uid: toUid, groupName: groupName });
10 years ago
next();
},
10 years ago
], callback);
};
Groups.ownership.rescind = function (toUid, groupName, callback) {
// Note: No ownership checking is done here on purpose!
// If the owners set only contains one member, error out!
10 years ago
async.waterfall([
function (next) {
db.setCount('group:' + groupName + ':owners', next);
},
function (numOwners, next) {
if (numOwners <= 1) {
return next(new Error('[[error:group-needs-owner]]'));
}
db.setRemove('group:' + groupName + ':owners', toUid, next);
},
function (next) {
plugins.fireHook('action:group.rescindOwnership', { uid: toUid, groupName: groupName });
10 years ago
next();
},
10 years ago
], callback);
};
};