diff --git a/lib/api/api-router.js b/lib/api/api-router.js index ad89d0a3..bc01899c 100644 --- a/lib/api/api-router.js +++ b/lib/api/api-router.js @@ -200,6 +200,7 @@ module.exports = class ApiRouter { this.mapRouter = new MapRouter({ collaborators }); this.templateRouter = new TemplateRouter({ collaborators }); + this.metadataBackend = metadataBackend; } route (app, routes) { @@ -224,7 +225,7 @@ module.exports = class ApiRouter { })); apiRouter.use(lzmaMiddleware()); apiRouter.use(cors()); - apiRouter.use(user()); + apiRouter.use(user(this.metadataBackend)); this.templateRouter.route(apiRouter, route.template); this.mapRouter.route(apiRouter, route.map); diff --git a/lib/api/middlewares/user.js b/lib/api/middlewares/user.js index 7ce3a40a..cd3fc85c 100644 --- a/lib/api/middlewares/user.js +++ b/lib/api/middlewares/user.js @@ -2,12 +2,30 @@ const CdbRequest = require('../../models/cdb-request'); -module.exports = function user () { +module.exports = function user (metadataBackend) { const cdbRequest = new CdbRequest(); return function userMiddleware (req, res, next) { - res.locals.user = cdbRequest.userByReq(req); + res.locals.user = getUserNameFromRequest(req, cdbRequest); - next(); + getUserId(metadataBackend, res.locals.user, function (userId) { + if (userId) { + res.locals.userId = userId; + } + return next(); + }); }; }; + +function getUserNameFromRequest (req, cdbRequest) { + return cdbRequest.userByReq(req); +} + +function getUserId (metadataBackend, userName, callback) { + metadataBackend.getUserId(userName, function (err, userId) { + if (err) { + return callback(); + } + return callback(userId); + }); +}