all files / src/groups/ ownership.js

37.5% Statements 9/24
16.67% Branches 1/6
20% Functions 2/10
37.5% Lines 9/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                                                                     
'use strict';
 
var	async = require('async'),
	db = require('../database'),
	plugins = require('../plugins');
 
module.exports = function(Groups) {
 
	Groups.ownership = {};
 
	Groups.ownership.isOwner = function(uid, groupName, callback) {
		Eif (!uid) {
			return callback(null, false);
		}
		db.isSetMember('group:' + groupName + ':owners', uid, callback);
	};
 
	Groups.ownership.isOwners = function(uids, groupName, callback) {
		if (!Array.isArray(uids)) {
			return callback(null, []);
		}
 
		db.isSetMembers('group:' + groupName + ':owners', uids, callback);
	};
 
	Groups.ownership.grant = function(toUid, groupName, callback) {
		// Note: No ownership checking is done here on purpose!
		async.waterfall([
			function(next) {
				db.setAdd('group:' + groupName + ':owners', toUid, next);
			},
			function(next) {
				plugins.fireHook('action:group.grantOwnership', {uid: toUid, groupName: groupName});
				next();
			}
		], 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!
		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});
				next();
			}
		], callback);
	};
};