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

144 lines
3.3 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'),
widgets = require('../widgets'),
2014-01-10 16:00:03 -05:00
user = require('../user'),
topics = require('../topics'),
2014-01-13 12:05:13 -05:00
categories = require('../categories'),
logger = require('../logger'),
events = require('../events'),
2014-07-30 17:12:07 -04:00
emailer = require('../emailer'),
db = require('../database'),
2014-01-10 16:00:03 -05:00
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-04-15 02:40:18 -04:00
user: require('./admin/user'),
categories: require('./admin/categories'),
groups: require('./admin/groups'),
2014-08-17 19:26:24 -04:00
tags: require('./admin/tags'),
themes: {},
plugins: {},
widgets: {},
config: {},
2014-07-30 17:12:07 -04:00
settings: {},
email: {}
};
2014-01-10 16:00:03 -05:00
2014-06-03 16:10:24 -04:00
SocketAdmin.before = function(socket, method, next) {
2014-01-16 15:00:49 -05:00
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (!err && isAdmin) {
2014-01-13 12:01:42 -05:00
next();
} else {
2014-06-03 16:10:24 -04:00
winston.warn('[socket.io] Call to admin method ( ' + method + ' ) blocked (accessed by uid ' + socket.uid + ')');
2014-01-13 12:01:42 -05:00
}
});
};
2014-08-25 10:46:48 -04:00
SocketAdmin.reload = function(socket, data, callback) {
meta.reload(callback);
};
2014-02-22 03:11:13 -05:00
SocketAdmin.restart = function(socket, data, callback) {
meta.restart();
};
SocketAdmin.fireEvent = function(socket, data, callback) {
index.server.sockets.emit(data.name, data.payload || {});
};
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) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:42:19 -05:00
}
2014-03-31 16:19:57 -04:00
2014-06-16 13:01:37 +07:00
var wrappedCallback = function(err) {
2014-03-31 16:19:57 -04:00
meta.themes.set(data, function() {
callback();
});
2014-06-16 13:01:37 +07:00
};
if (data.type == 'bootswatch') {
wrappedCallback();
} else {
widgets.reset(wrappedCallback);
}
};
2014-01-10 16:00:03 -05:00
2014-05-14 18:05:17 -04:00
SocketAdmin.themes.updateBranding = function(socket, data, callback) {
meta.css.updateBranding();
};
SocketAdmin.plugins.toggleActive = function(socket, plugin_id, callback) {
plugins.toggleActive(plugin_id, callback);
};
SocketAdmin.plugins.toggleInstall = function(socket, plugin_id, callback) {
plugins.toggleInstall(plugin_id, callback);
2014-01-10 16:00:03 -05:00
};
SocketAdmin.widgets.set = function(socket, data, callback) {
if(!data) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
widgets.setArea(data, callback);
};
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) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:42:19 -05:00
}
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);
};
SocketAdmin.settings.get = function(socket, data, callback) {
meta.settings.get(data.hash, callback);
};
SocketAdmin.settings.set = function(socket, data, callback) {
meta.settings.set(data.hash, data.values, callback);
};
2014-07-30 17:12:07 -04:00
SocketAdmin.email.test = function(socket, data, callback) {
if (plugins.hasListeners('action:email.send')) {
emailer.send('test', socket.uid, {
subject: '[NodeBB] Test Email',
site_title: meta.config.site_title || 'NodeBB'
});
callback();
} else {
callback(new Error('[[error:no-emailers-configured]]'));
}
};
2014-02-28 12:58:05 -05:00
module.exports = SocketAdmin;