|
|
@ -1,6 +1,5 @@
|
|
|
|
'use strict';
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
const nconf = require('nconf');
|
|
|
|
const nconf = require('nconf');
|
|
|
|
const winston = require('winston');
|
|
|
|
const winston = require('winston');
|
|
|
|
|
|
|
|
|
|
|
@ -8,7 +7,7 @@ const meta = require('../meta');
|
|
|
|
const user = require('../user');
|
|
|
|
const user = require('../user');
|
|
|
|
const privileges = require('../privileges');
|
|
|
|
const privileges = require('../privileges');
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
|
|
|
const helpers = require('./helpers');
|
|
|
|
const auth = require('../routes/authentication');
|
|
|
|
const auth = require('../routes/authentication');
|
|
|
|
|
|
|
|
|
|
|
|
const controllers = {
|
|
|
|
const controllers = {
|
|
|
@ -16,9 +15,9 @@ const controllers = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function (middleware) {
|
|
|
|
module.exports = function (middleware) {
|
|
|
|
async function authenticate(req, res, next, callback) {
|
|
|
|
async function authenticate(req, res) {
|
|
|
|
if (req.loggedIn) {
|
|
|
|
if (req.loggedIn) {
|
|
|
|
return next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await plugins.fireHook('response:middleware.authenticate', {
|
|
|
|
await plugins.fireHook('response:middleware.authenticate', {
|
|
|
@ -28,35 +27,35 @@ module.exports = function (middleware) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!res.headersSent) {
|
|
|
|
if (!res.headersSent) {
|
|
|
|
auth.setAuthVars(req, res, function () {
|
|
|
|
auth.setAuthVars(req);
|
|
|
|
if (req.loggedIn && req.user && req.user.uid) {
|
|
|
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !res.headersSent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
middleware.authenticate = function middlewareAuthenticate(req, res, next) {
|
|
|
|
middleware.authenticate = helpers.try(async function middlewareAuthenticate(req, res, next) {
|
|
|
|
authenticate(req, res, next, function () {
|
|
|
|
if (!await authenticate(req, res)) {
|
|
|
|
controllers.helpers.notAllowed(req, res, next);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (!req.loggedIn) {
|
|
|
|
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
const authenticateAsync = util.promisify(middleware.authenticate);
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.authenticateOrGuest = function authenticateOrGuest(req, res, next) {
|
|
|
|
middleware.authenticateOrGuest = helpers.try(async function authenticateOrGuest(req, res, next) {
|
|
|
|
authenticate(req, res, next, next);
|
|
|
|
if (!await authenticate(req, res)) {
|
|
|
|
};
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.ensureSelfOrGlobalPrivilege = function ensureSelfOrGlobalPrivilege(req, res, next) {
|
|
|
|
middleware.ensureSelfOrGlobalPrivilege = helpers.try(async function ensureSelfOrGlobalPrivilege(req, res, next) {
|
|
|
|
ensureSelfOrMethod(user.isAdminOrGlobalMod, req, res, next);
|
|
|
|
await ensureSelfOrMethod(user.isAdminOrGlobalMod, req, res, next);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.ensureSelfOrPrivileged = function ensureSelfOrPrivileged(req, res, next) {
|
|
|
|
middleware.ensureSelfOrPrivileged = helpers.try(async function ensureSelfOrPrivileged(req, res, next) {
|
|
|
|
ensureSelfOrMethod(user.isPrivileged, req, res, next);
|
|
|
|
await ensureSelfOrMethod(user.isPrivileged, req, res, next);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function ensureSelfOrMethod(method, req, res, next) {
|
|
|
|
async function ensureSelfOrMethod(method, req, res, next) {
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -67,7 +66,7 @@ module.exports = function (middleware) {
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (req.uid === parseInt(res.locals.uid, 10)) {
|
|
|
|
if (req.uid === parseInt(res.locals.uid, 10)) {
|
|
|
|
return setImmediate(next);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const allowed = await method(req.uid);
|
|
|
|
const allowed = await method(req.uid);
|
|
|
|
if (!allowed) {
|
|
|
|
if (!allowed) {
|
|
|
@ -77,12 +76,12 @@ module.exports = function (middleware) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
middleware.checkGlobalPrivacySettings = function checkGlobalPrivacySettings(req, res, next) {
|
|
|
|
middleware.checkGlobalPrivacySettings = helpers.try(async function checkGlobalPrivacySettings(req, res, next) {
|
|
|
|
winston.warn('[middleware], checkGlobalPrivacySettings deprecated, use canViewUsers or canViewGroups');
|
|
|
|
winston.warn('[middleware], checkGlobalPrivacySettings deprecated, use canViewUsers or canViewGroups');
|
|
|
|
middleware.canViewUsers(req, res, next);
|
|
|
|
await middleware.canViewUsers(req, res, next);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.canViewUsers = async function canViewUsers(req, res, next) {
|
|
|
|
middleware.canViewUsers = helpers.try(async function canViewUsers(req, res, next) {
|
|
|
|
if (parseInt(res.locals.uid, 10) === req.uid) {
|
|
|
|
if (parseInt(res.locals.uid, 10) === req.uid) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -91,19 +90,24 @@ module.exports = function (middleware) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.canViewGroups = async function canViewGroups(req, res, next) {
|
|
|
|
middleware.canViewGroups = helpers.try(async function canViewGroups(req, res, next) {
|
|
|
|
const canView = await privileges.global.can('view:groups', req.uid);
|
|
|
|
const canView = await privileges.global.can('view:groups', req.uid);
|
|
|
|
if (canView) {
|
|
|
|
if (canView) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.checkAccountPermissions = async function checkAccountPermissions(req, res, next) {
|
|
|
|
middleware.checkAccountPermissions = helpers.try(async function checkAccountPermissions(req, res, next) {
|
|
|
|
// This middleware ensures that only the requested user and admins can pass
|
|
|
|
// This middleware ensures that only the requested user and admins can pass
|
|
|
|
await authenticateAsync(req, res);
|
|
|
|
if (!await authenticate(req, res)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.loggedIn) {
|
|
|
|
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
|
|
|
|
}
|
|
|
|
const uid = await user.getUidByUserslug(req.params.userslug);
|
|
|
|
const uid = await user.getUidByUserslug(req.params.userslug);
|
|
|
|
let allowed = await privileges.users.canEdit(req.uid, uid);
|
|
|
|
let allowed = await privileges.users.canEdit(req.uid, uid);
|
|
|
|
if (allowed) {
|
|
|
|
if (allowed) {
|
|
|
@ -117,17 +121,17 @@ module.exports = function (middleware) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
controllers.helpers.notAllowed(req, res);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.redirectToAccountIfLoggedIn = async function redirectToAccountIfLoggedIn(req, res, next) {
|
|
|
|
middleware.redirectToAccountIfLoggedIn = helpers.try(async function redirectToAccountIfLoggedIn(req, res, next) {
|
|
|
|
if (req.session.forceLogin || req.uid <= 0) {
|
|
|
|
if (req.session.forceLogin || req.uid <= 0) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const userslug = await user.getUserField(req.uid, 'userslug');
|
|
|
|
const userslug = await user.getUserField(req.uid, 'userslug');
|
|
|
|
controllers.helpers.redirect(res, '/user/' + userslug);
|
|
|
|
controllers.helpers.redirect(res, '/user/' + userslug);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.redirectUidToUserslug = async function redirectUidToUserslug(req, res, next) {
|
|
|
|
middleware.redirectUidToUserslug = helpers.try(async function redirectUidToUserslug(req, res, next) {
|
|
|
|
const uid = parseInt(req.params.uid, 10);
|
|
|
|
const uid = parseInt(req.params.uid, 10);
|
|
|
|
if (uid <= 0) {
|
|
|
|
if (uid <= 0) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
@ -140,18 +144,18 @@ module.exports = function (middleware) {
|
|
|
|
.replace('uid', 'user')
|
|
|
|
.replace('uid', 'user')
|
|
|
|
.replace(uid, function () { return userslug; });
|
|
|
|
.replace(uid, function () { return userslug; });
|
|
|
|
controllers.helpers.redirect(res, path);
|
|
|
|
controllers.helpers.redirect(res, path);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.redirectMeToUserslug = async function redirectMeToUserslug(req, res) {
|
|
|
|
middleware.redirectMeToUserslug = helpers.try(async function redirectMeToUserslug(req, res) {
|
|
|
|
const userslug = await user.getUserField(req.uid, 'userslug');
|
|
|
|
const userslug = await user.getUserField(req.uid, 'userslug');
|
|
|
|
if (!userslug) {
|
|
|
|
if (!userslug) {
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const path = req.path.replace(/^(\/api)?\/me/, '/user/' + userslug);
|
|
|
|
const path = req.path.replace(/^(\/api)?\/me/, '/user/' + userslug);
|
|
|
|
controllers.helpers.redirect(res, path);
|
|
|
|
controllers.helpers.redirect(res, path);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.isAdmin = async function isAdmin(req, res, next) {
|
|
|
|
middleware.isAdmin = helpers.try(async function isAdmin(req, res, next) {
|
|
|
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
|
|
const isAdmin = await user.isAdministrator(req.uid);
|
|
|
|
if (!isAdmin) {
|
|
|
|
if (!isAdmin) {
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
|
return controllers.helpers.notAllowed(req, res);
|
|
|
@ -186,7 +190,7 @@ module.exports = function (middleware) {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
res.redirect(nconf.get('relative_path') + '/login?local=1');
|
|
|
|
res.redirect(nconf.get('relative_path') + '/login?local=1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
middleware.requireUser = function (req, res, next) {
|
|
|
|
middleware.requireUser = function (req, res, next) {
|
|
|
|
if (req.loggedIn) {
|
|
|
|
if (req.loggedIn) {
|
|
|
|