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) {
|
2018-03-01 22:42:46 +08:00
|
|
|
const cdbRequest = new CdbRequest();
|
2017-10-03 23:47:57 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
return function userMiddleware (req, res, next) {
|
2020-02-20 18:47:44 +08:00
|
|
|
res.locals.user = getUserNameFromRequest(req, cdbRequest);
|
2018-03-01 22:42:03 +08:00
|
|
|
|
2020-02-20 18:47:44 +08:00
|
|
|
getUserId(metadataBackend, res.locals.user, function (userId) {
|
|
|
|
if (userId) {
|
|
|
|
res.locals.userId = userId;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUserId (metadataBackend, userName, callback) {
|
|
|
|
metadataBackend.getUserId(userName, function (err, userId) {
|
|
|
|
if (err) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
return callback(userId);
|
|
|
|
});
|
|
|
|
}
|