2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 15:58:21 +08:00
|
|
|
const CdbRequest = require('../../models/cdb-request');
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2020-02-20 18:47:44 +08:00
|
|
|
module.exports = function user (metadataBackend) {
|
2020-06-03 21:39:02 +08:00
|
|
|
const cdbRequest = new CdbRequest();
|
2020-06-02 18:31:18 +08:00
|
|
|
|
2020-06-03 21:39:02 +08:00
|
|
|
return function userMiddleware (req, res, next) {
|
2020-06-12 00:21:13 +08:00
|
|
|
const { logger } = res.locals;
|
2020-06-03 21:39:02 +08:00
|
|
|
try {
|
|
|
|
res.locals.user = getUserNameFromRequest(req, cdbRequest);
|
2020-06-12 00:21:13 +08:00
|
|
|
logger.info({ user: res.locals.user });
|
2020-06-03 21:39:02 +08:00
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2018-03-01 22:42:03 +08:00
|
|
|
|
2020-04-27 16:23:11 +08:00
|
|
|
metadataBackend.getUserId(res.locals.user, (err, userId) => {
|
|
|
|
if (err || !userId) {
|
|
|
|
return next();
|
2020-02-20 18:47:44 +08:00
|
|
|
}
|
2020-04-27 16:23:11 +08:00
|
|
|
|
|
|
|
res.locals.userId = userId;
|
2020-06-04 23:45:15 +08:00
|
|
|
|
2020-04-27 17:06:09 +08:00
|
|
|
return next();
|
2020-02-20 18:47:44 +08:00
|
|
|
});
|
2018-03-01 22:42:03 +08:00
|
|
|
};
|
2015-09-30 23:17:01 +08:00
|
|
|
};
|
2020-02-20 18:47:44 +08:00
|
|
|
|
|
|
|
function getUserNameFromRequest (req, cdbRequest) {
|
|
|
|
return cdbRequest.userByReq(req);
|
|
|
|
}
|