Files
NodeBB/src/socket.io/admin.js

349 lines
7.6 KiB
JavaScript
Raw Normal View History

2014-01-13 12:05:13 -05:00
"use strict";
2014-01-10 16:00:03 -05:00
var groups = require('../groups'),
meta = require('../meta'),
plugins = require('../plugins'),
user = require('../user'),
topics = require('../topics'),
2014-01-13 12:05:13 -05:00
categories = require('../categories'),
2014-01-10 16:00:03 -05:00
CategoryTools = require('../categoryTools'),
2014-01-13 12:05:13 -05:00
logger = require('../logger'),
2014-01-10 16:00:03 -05:00
admin = {
user: require('../admin/user'),
categories: require('../admin/categories')
},
async = require('async'),
2014-01-13 12:01:42 -05:00
winston = require('winston'),
2014-01-16 17:32:33 -05:00
index = require('./index'),
2014-01-10 16:00:03 -05:00
SocketAdmin = {};
2014-01-16 15:00:49 -05:00
SocketAdmin.before = function(socket, next) {
2014-01-13 12:01:42 -05:00
// Verify administrative privileges
2014-01-16 15:00:49 -05:00
user.isAdministrator(socket.uid, function(err, isAdmin) {
2014-01-13 12:01:42 -05:00
if (isAdmin) {
next();
} else {
2014-01-16 15:00:49 -05:00
winston.warn('[socket.io] Call to admin method blocked (accessed by uid ' + socket.uid + ')');
2014-01-13 12:01:42 -05:00
}
});
};
2014-01-10 16:00:03 -05:00
/* Topics */
SocketAdmin.topics = {};
2014-01-16 15:00:49 -05:00
SocketAdmin.topics.getMore = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
topics.getAllTopics(data.limit, data.after, callback);
2014-01-10 16:00:03 -05:00
};
/* User */
SocketAdmin.user = {};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.makeAdmin = function(socket, theirid) {
admin.user.makeAdmin(socket.uid, theirid, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.removeAdmin = function(socket, theirid) {
admin.user.removeAdmin(socket.uid, theirid, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.createUser = function(socket, user, callback) {
2014-01-17 12:42:19 -05:00
if(!user) {
return callback(new Error('invalid data'));
}
2014-01-16 15:00:49 -05:00
admin.user.createUser(socket.uid, user, callback);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.banUser = function(socket, theirid) {
admin.user.banUser(socket.uid, theirid, socket, function(isBanned) {
if(isBanned) {
var sockets = index.getUserSockets(theirid);
for(var i=0; i<sockets.length; ++i) {
sockets[i].emit('event:banned');
2014-01-10 16:00:03 -05:00
}
2014-01-16 15:00:49 -05:00
module.parent.exports.logoutUser(theirid);
}
});
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.unbanUser = function(socket, theirid) {
admin.user.unbanUser(socket.uid, theirid, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.user.search = function(socket, username, callback) {
user.search(username, function(err, data) {
2014-01-10 16:00:03 -05:00
function isAdmin(userData, next) {
user.isAdministrator(userData.uid, function(err, isAdmin) {
if(err) {
return next(err);
}
userData.administrator = isAdmin?'1':'0';
next();
});
}
async.each(data.users, isAdmin, function(err) {
2014-01-17 00:07:02 -05:00
callback(err, data);
2014-01-10 16:00:03 -05:00
});
});
};
/* Categories */
SocketAdmin.categories = {};
2014-01-16 15:00:49 -05:00
SocketAdmin.categories.create = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
categories.create(data, callback);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.categories.update = function(socket, data) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-16 15:00:49 -05:00
admin.categories.update(data, socket);
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.categories.search = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-16 15:00:49 -05:00
var username = data.username,
cid = data.cid;
user.search(username, function(err, data) {
async.map(data.users, function(userObj, next) {
CategoryTools.privileges(cid, userObj.uid, function(err, privileges) {
2014-01-17 12:42:19 -05:00
if(err) {
return next(err);
2014-01-13 12:05:13 -05:00
}
2014-01-17 12:42:19 -05:00
userObj.privileges = privileges;
next(null, userObj);
2014-01-10 16:00:03 -05:00
});
2014-01-17 12:42:19 -05:00
}, callback);
});
2014-01-10 16:00:03 -05:00
};
2014-01-16 15:00:49 -05:00
SocketAdmin.categories.setPrivilege = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-16 15:00:49 -05:00
var cid = data.cid,
uid = data.uid,
privilege = data.privilege,
set = data.set,
cb = function(err) {
2014-01-17 12:42:19 -05:00
if(err) {
return callback(err);
}
2014-01-16 15:00:49 -05:00
CategoryTools.privileges(cid, uid, callback);
};
2014-01-10 16:00:03 -05:00
if (set) {
groups.joinByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb);
} else {
groups.leaveByGroupName('cid:' + cid + ':privileges:' + privilege, uid, cb);
}
};
2014-01-16 15:13:22 -05:00
SocketAdmin.categories.getPrivilegeSettings = function(socket, cid, callback) {
2014-01-10 16:00:03 -05:00
async.parallel({
"+r": function(next) {
groups.getByGroupName('cid:' + cid + ':privileges:+r', { expand: true }, function(err, groupObj) {
if (!err) {
next.apply(this, arguments);
} else {
next(null, {
members: []
});
}
});
},
"+w": function(next) {
groups.getByGroupName('cid:' + cid + ':privileges:+w', { expand: true }, function(err, groupObj) {
if (!err) {
next.apply(this, arguments);
} else {
next(null, {
members: []
});
}
});
}
}, function(err, data) {
2014-01-17 12:42:19 -05:00
if(err) {
return callback(err);
}
2014-01-10 16:00:03 -05:00
callback(null, {
"+r": data['+r'].members,
"+w": data['+w'].members
});
});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.categories.setGroupPrivilege = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-16 15:13:22 -05:00
if (data.set) {
groups.joinByGroupName('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
2014-01-10 16:00:03 -05:00
} else {
2014-01-16 15:13:22 -05:00
groups.leaveByGroupName('cid:' + data.cid + ':privileges:' + data.privilege, data.gid, callback);
2014-01-10 16:00:03 -05:00
}
};
2014-01-16 15:13:22 -05:00
SocketAdmin.categories.groupsList = function(socket, cid, callback) {
async.parallel({
groups: function(next) {
groups.list({expand:false}, next);
},
system: function(next) {
groups.listSystemGroups({expand: false}, next);
}
}, function(err, results) {
2014-01-17 12:42:19 -05:00
if(err) {
return callback(err);
}
var data = results.groups.concat(results.system);
2014-01-10 16:00:03 -05:00
async.map(data, function(groupObj, next) {
CategoryTools.groupPrivileges(cid, groupObj.gid, function(err, privileges) {
2014-01-17 12:42:19 -05:00
if(err) {
return next(err);
2014-01-10 16:00:03 -05:00
}
2014-01-17 12:42:19 -05:00
groupObj.privileges = privileges;
2014-01-10 16:00:03 -05:00
next(null, groupObj);
});
2014-01-17 12:42:19 -05:00
}, callback);
2014-01-10 16:00:03 -05:00
});
};
/* Themes & Plugins */
SocketAdmin.themes = {};
SocketAdmin.plugins = {};
2014-01-16 15:13:22 -05:00
SocketAdmin.themes.getInstalled = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
meta.themes.get(callback);
2014-01-10 16:00:03 -05:00
};
2014-01-17 12:42:19 -05:00
SocketAdmin.themes.set = function(socket, data, callback) {
if(!data) {
return callback(new Error('invalid data'));
}
meta.themes.set(data, callback);
}
2014-01-10 16:00:03 -05:00
2014-01-16 15:13:22 -05:00
SocketAdmin.plugins.toggle = function(socket, plugin_id) {
2014-01-10 16:00:03 -05:00
plugins.toggleActive(plugin_id, function(status) {
2014-01-16 15:18:13 -05:00
socket.emit('admin.plugins.toggle', status);
2014-01-10 16:00:03 -05:00
});
};
2014-01-13 12:05:13 -05:00
/* Configs */
SocketAdmin.config = {};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.get = function(socket, data, callback) {
2014-01-16 18:06:19 -05:00
meta.configs.list(callback);
2014-01-13 12:05:13 -05:00
};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.set = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
2014-01-13 12:05:13 -05:00
meta.configs.set(data.key, data.value, function(err) {
2014-01-16 18:06:19 -05:00
if(err) {
return callback(err);
2014-01-13 12:05:13 -05:00
}
2014-01-16 18:06:19 -05:00
callback(null);
plugins.fireHook('action:config.set', {
key: data.key,
value: data.value
});
2014-01-16 17:32:33 -05:00
logger.monitorConfig({io: index.server}, data);
2014-01-13 12:05:13 -05:00
});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.remove = function(socket, key) {
2014-01-13 12:05:13 -05:00
meta.configs.remove(key);
};
/* Groups */
SocketAdmin.groups = {};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.create = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
groups.create(data.name, data.description, function(err, groupObj) {
2014-01-17 12:42:19 -05:00
callback(err, groupObj || undefined);
});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.delete = function(socket, gid, callback) {
2014-01-17 12:42:19 -05:00
groups.destroy(gid, callback);
};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.get = function(socket, gid, callback) {
groups.get(gid, {
expand: true
}, function(err, groupObj) {
2014-01-17 12:42:19 -05:00
callback(err, groupObj || undefined);
});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.join = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
groups.join(data.gid, data.uid, callback);
};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.leave = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
groups.leave(data.gid, data.uid, callback);
};
2014-01-16 15:13:22 -05:00
SocketAdmin.groups.update = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
return callback(new Error('invalid data'));
}
groups.update(data.gid, data.values, function(err) {
callback(err ? err.message : null);
});
};
2014-01-10 16:00:03 -05:00
module.exports = SocketAdmin;