2017-07-08 13:48:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const passport = require('./passport');
|
2017-07-08 18:32:04 +00:00
|
|
|
const config = require('config');
|
2017-07-24 04:03:32 +00:00
|
|
|
const permissions = require('./permissions');
|
2017-07-08 13:48:34 +00:00
|
|
|
|
2017-07-24 04:03:32 +00:00
|
|
|
function getAnonymousConfig(context) {
|
2017-07-08 13:48:34 +00:00
|
|
|
return {
|
|
|
|
authMethod: passport.authMethod,
|
2017-07-08 18:32:04 +00:00
|
|
|
isAuthMethodLocal: passport.isAuthMethodLocal,
|
2017-07-09 13:41:53 +00:00
|
|
|
externalPasswordResetLink: config.ldap.passwordresetlink,
|
|
|
|
language: config.language || 'en',
|
2017-07-24 04:03:32 +00:00
|
|
|
isAuthenticated: !!context.user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAuthenticatedConfig(context) {
|
|
|
|
const roles = {};
|
|
|
|
for (const entityTypeId in config.roles) {
|
|
|
|
const rolesPerEntityType = {};
|
|
|
|
for (const roleId in config.roles[entityTypeId]) {
|
|
|
|
const roleSpec = config.roles[entityTypeId][roleId];
|
|
|
|
|
|
|
|
rolesPerEntityType[roleId] = {
|
|
|
|
name: roleSpec.name,
|
|
|
|
description: roleSpec.description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roles[entityTypeId] = rolesPerEntityType;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
userId: context.user.id,
|
|
|
|
roles
|
2017-07-08 13:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
function registerRootRoute(router, entryPoint, title) {
|
2017-07-08 13:48:34 +00:00
|
|
|
router.get('/*', passport.csrfProtection, (req, res) => {
|
2017-07-24 04:03:32 +00:00
|
|
|
const mailtrainConfig = getAnonymousConfig(req.context);
|
|
|
|
if (req.user) {
|
|
|
|
Object.assign(mailtrainConfig, getAuthenticatedConfig(req.context));
|
|
|
|
}
|
|
|
|
|
2017-07-08 13:48:34 +00:00
|
|
|
res.render('react-root', {
|
|
|
|
title,
|
|
|
|
reactEntryPoint: entryPoint,
|
|
|
|
reactCsrfToken: req.csrfToken(),
|
2017-07-24 04:03:32 +00:00
|
|
|
mailtrainConfig: JSON.stringify(mailtrainConfig)
|
2017-07-08 13:48:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-07-24 04:03:32 +00:00
|
|
|
registerRootRoute,
|
|
|
|
getAuthenticatedConfig
|
2017-07-08 13:48:34 +00:00
|
|
|
};
|
|
|
|
|