Files
NodeBB/src/middleware/admin.js

123 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
var async = require('async');
var winston = require('winston');
var user = require('../user');
var meta = require('../meta');
var plugins = require('../plugins');
var controllers = {
api: require('../controllers/api'),
2017-02-17 19:31:21 -07:00
helpers: require('../controllers/helpers'),
};
module.exports = function (middleware) {
middleware.admin = {};
middleware.admin.isAdmin = function (req, res, next) {
winston.warn('[middleware.admin.isAdmin] deprecation warning, no need to use this from plugins!');
2015-03-06 19:38:10 -05:00
2017-05-12 17:53:23 -04:00
async.waterfall([
function (next) {
user.isAdministrator(req.uid, next);
},
function (isAdmin, next) {
if (!isAdmin) {
return controllers.helpers.notAllowed(req, res);
}
next();
},
], next);
2015-03-06 19:38:10 -05:00
};
middleware.admin.buildHeader = function (req, res, next) {
res.locals.renderAdminHeader = true;
2015-03-06 19:38:10 -05:00
controllers.api.getConfig(req, res, function (err, config) {
2015-03-06 19:38:10 -05:00
if (err) {
return next(err);
}
2016-10-05 15:22:35 +03:00
res.locals.config = config;
next();
});
};
middleware.admin.renderHeader = function (req, res, data, next) {
var custom_header = {
2017-02-18 01:19:20 -07:00
plugins: [],
authentication: [],
};
2015-03-06 19:38:10 -05:00
user.getUserFields(req.uid, ['username', 'userslug', 'email', 'picture', 'email:confirmed'], function (err, userData) {
if (err) {
return next(err);
}
userData.uid = req.uid;
userData['email:confirmed'] = parseInt(userData['email:confirmed'], 10) === 1;
async.parallel({
scripts: function (next) {
plugins.fireHook('filter:admin.scripts.get', [], function (err, scripts) {
if (err) {
return next(err);
}
var arr = [];
scripts.forEach(function (script) {
2017-02-18 12:30:49 -07:00
arr.push({ src: script });
});
next(null, arr);
});
},
custom_header: function (next) {
plugins.fireHook('filter:admin.header.build', custom_header, next);
},
config: function (next) {
controllers.api.getConfig(req, res, next);
},
configs: function (next) {
meta.configs.list(next);
2017-02-17 19:31:21 -07:00
},
}, function (err, results) {
if (err) {
return next(err);
}
res.locals.config = results.config;
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 = {
config: results.config,
configJSON: JSON.stringify(results.config),
relative_path: results.config.relative_path,
adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)),
user: userData,
userJSON: JSON.stringify(userData).replace(/'/g, "\\'"),
plugins: results.custom_header.plugins,
authentication: results.custom_header.authentication,
scripts: results.scripts,
'cache-buster': meta.config['cache-buster'] || '',
env: !!process.env.NODE_ENV,
title: (acpPath || 'Dashboard') + ' | NodeBB Admin Control Panel',
2017-02-17 19:31:21 -07:00
bodyClass: data.bodyClass,
};
2017-02-18 12:30:49 -07:00
templateValues.template = { name: res.locals.template };
templateValues.template[res.locals.template] = true;
req.app.render('admin/header', templateValues, next);
2015-10-28 20:35:56 -04:00
});
2015-03-06 19:38:10 -05:00
});
};
2016-10-05 15:22:35 +03:00
middleware.admin.renderFooter = function (req, res, data, next) {
2016-10-05 15:22:35 +03:00
req.app.render('admin/footer', data, next);
};
2014-04-10 20:31:57 +01:00
};