Files
NodeBB/src/middleware/admin.js

131 lines
3.3 KiB
JavaScript
Raw Normal View History

"use strict";
var app,
middleware = {},
nconf = require('nconf'),
async = require('async'),
winston = require('winston'),
2014-10-27 00:54:26 -04:00
user = require('../user'),
meta = require('../meta'),
plugins = require('../plugins'),
controllers = {
2014-12-01 21:07:23 -05:00
api: require('../controllers/api'),
helpers: require('../controllers/helpers')
};
middleware.isAdmin = function(req, res, next) {
2014-12-01 23:22:02 -05:00
winston.warn('[middleware.admin.isAdmin] deprecation warning, no need to use this from plugins!');
if (!req.user) {
2014-12-07 16:37:05 -05:00
return controllers.helpers.notAllowed(req, res);
}
2014-12-07 16:37:05 -05:00
user.isAdministrator(req.user.uid, function (err, isAdmin) {
if (err || isAdmin) {
return next(err);
}
controllers.helpers.notAllowed(req, res);
});
};
middleware.buildHeader = function(req, res, next) {
2015-03-06 19:38:10 -05:00
res.locals.renderAdminHeader = true;
async.parallel({
config: function(next) {
controllers.api.getConfig(req, res, next);
},
footer: function(next) {
app.render('admin/footer', {}, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
res.locals.config = results.config;
res.locals.adminFooter = results.footer;
next();
});
};
middleware.renderHeader = function(req, res, data, next) {
2015-03-06 19:38:10 -05:00
var custom_header = {
'plugins': [],
'authentication': []
};
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], function(err, userData) {
2015-03-06 19:38:10 -05:00
if (err) {
return next(err);
}
userData.uid = req.uid;
2015-03-06 19:38:10 -05:00
userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
async.parallel({
scripts: function(next) {
plugins.fireHook('filter:admin.scripts.get', [], function(err, scripts) {
2014-10-27 00:54:26 -04:00
if (err) {
return next(err);
}
2015-03-06 19:38:10 -05:00
var arr = [];
scripts.forEach(function(script) {
2016-01-18 15:22:18 -05:00
arr.push({src: script});
});
2015-03-06 19:38:10 -05:00
next(null, arr);
});
2015-03-06 19:38:10 -05:00
},
custom_header: function(next) {
plugins.fireHook('filter:admin.header.build', custom_header, next);
},
config: function(next) {
controllers.api.getConfig(req, res, next);
2015-09-22 17:13:05 -04:00
},
configs: function(next) {
meta.configs.list(next);
2015-03-06 19:38:10 -05:00
}
}, function(err, results) {
if (err) {
return next(err);
}
res.locals.config = results.config;
2015-10-28 20:35:56 -04:00
var acpPath = req.path.slice(1).split('/');
acpPath.forEach(function(path, i) {
acpPath[i] = path.charAt(0).toUpperCase() + path.slice(1);
});
acpPath = acpPath.join(' > ');
var templateValues = {
2015-08-27 19:22:51 -04:00
config: results.config,
2015-03-06 19:38:10 -05:00
configJSON: JSON.stringify(results.config),
2015-08-27 19:22:51 -04:00
relative_path: results.config.relative_path,
2015-09-22 17:13:05 -04:00
adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)),
2015-03-06 19:38:10 -05:00
user: userData,
2015-06-04 13:42:10 -04:00
userJSON: JSON.stringify(userData).replace(/'/g, "\\'"),
2015-03-06 19:38:10 -05:00
plugins: results.custom_header.plugins,
authentication: results.custom_header.authentication,
scripts: results.scripts,
'cache-buster': meta.config['cache-buster'] ? 'v=' + meta.config['cache-buster'] : '',
2015-10-28 20:35:56 -04:00
env: process.env.NODE_ENV ? true : false,
2016-04-29 13:46:07 -04:00
title: (acpPath || 'Dashboard') + ' | NodeBB Admin Control Panel',
bodyClass: data.bodyClass
2015-03-06 19:38:10 -05:00
};
templateValues.template = {name: res.locals.template};
templateValues.template[res.locals.template] = true;
2015-03-06 19:38:10 -05:00
app.render('admin/header', templateValues, next);
2015-03-06 19:38:10 -05:00
});
});
};
module.exports = function(webserver) {
app = webserver;
return middleware;
2014-04-10 20:31:57 +01:00
};