mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 02:25:55 +01:00
feat: convert middleware.isAdmin to async/await
This commit is contained in:
@@ -198,49 +198,44 @@ module.exports = function (middleware) {
|
||||
], next);
|
||||
};
|
||||
|
||||
middleware.isAdmin = function isAdmin(req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.isAdministrator(req.uid, next);
|
||||
},
|
||||
function (isAdmin, next) {
|
||||
if (!isAdmin) {
|
||||
return controllers.helpers.notAllowed(req, res);
|
||||
}
|
||||
user.hasPassword(req.uid, next);
|
||||
},
|
||||
function (hasPassword, next) {
|
||||
if (!hasPassword) {
|
||||
return next();
|
||||
}
|
||||
middleware.isAdmin = async function isAdmin(req, res, next) {
|
||||
const isAdmin = await user.isAdministrator(req.uid);
|
||||
if (!isAdmin) {
|
||||
controllers.helpers.notAllowed(req, res);
|
||||
return;
|
||||
}
|
||||
const hasPassword = await user.hasPassword(req.uid);
|
||||
if (!hasPassword) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
var loginTime = req.session.meta ? req.session.meta.datetime : 0;
|
||||
var adminReloginDuration = meta.config.adminReloginDuration * 60000;
|
||||
var disabled = meta.config.adminReloginDuration === 0;
|
||||
if (disabled || (loginTime && parseInt(loginTime, 10) > Date.now() - adminReloginDuration)) {
|
||||
var timeLeft = parseInt(loginTime, 10) - (Date.now() - adminReloginDuration);
|
||||
if (req.session.meta && timeLeft < Math.min(300000, adminReloginDuration)) {
|
||||
req.session.meta.datetime += Math.min(300000, adminReloginDuration);
|
||||
}
|
||||
const loginTime = req.session.meta ? req.session.meta.datetime : 0;
|
||||
const adminReloginDuration = meta.config.adminReloginDuration * 60000;
|
||||
const disabled = meta.config.adminReloginDuration === 0;
|
||||
if (disabled || (loginTime && parseInt(loginTime, 10) > Date.now() - adminReloginDuration)) {
|
||||
const timeLeft = parseInt(loginTime, 10) - (Date.now() - adminReloginDuration);
|
||||
if (req.session.meta && timeLeft < Math.min(300000, adminReloginDuration)) {
|
||||
req.session.meta.datetime += Math.min(300000, adminReloginDuration);
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
var returnTo = req.path;
|
||||
if (nconf.get('relative_path')) {
|
||||
returnTo = req.path.replace(new RegExp('^' + nconf.get('relative_path')), '');
|
||||
}
|
||||
returnTo = returnTo.replace(/^\/api/, '');
|
||||
let returnTo = req.path;
|
||||
if (nconf.get('relative_path')) {
|
||||
returnTo = req.path.replace(new RegExp('^' + nconf.get('relative_path')), '');
|
||||
}
|
||||
returnTo = returnTo.replace(/^\/api/, '');
|
||||
|
||||
req.session.returnTo = returnTo;
|
||||
req.session.forceLogin = 1;
|
||||
if (res.locals.isAPI) {
|
||||
res.status(401).json({});
|
||||
} else {
|
||||
res.redirect(nconf.get('relative_path') + '/login?local=1');
|
||||
}
|
||||
},
|
||||
], next);
|
||||
req.session.returnTo = returnTo;
|
||||
req.session.forceLogin = 1;
|
||||
if (res.locals.isAPI) {
|
||||
res.status(401).json({});
|
||||
} else {
|
||||
res.redirect(nconf.get('relative_path') + '/login?local=1');
|
||||
}
|
||||
};
|
||||
|
||||
middleware.requireUser = function (req, res, next) {
|
||||
|
||||
Reference in New Issue
Block a user