2018-02-17 01:21:06 +08:00
|
|
|
const CdbRequest = require('../models/cartodb_request');
|
2016-10-04 21:19:31 +08:00
|
|
|
|
2018-05-29 19:23:50 +08:00
|
|
|
module.exports = function user(metadataBackend) {
|
2018-02-17 01:21:06 +08:00
|
|
|
const cdbRequest = new CdbRequest();
|
|
|
|
|
|
|
|
return function userMiddleware (req, res, next) {
|
2018-05-29 19:23:50 +08:00
|
|
|
res.locals.user = getUserNameFromRequest(req, cdbRequest);
|
|
|
|
|
2018-06-06 21:48:22 +08:00
|
|
|
checkUserExists(metadataBackend, res.locals.user, function(err, userExists) {
|
|
|
|
if (err || !userExists) {
|
2018-05-29 19:23:50 +08:00
|
|
|
const error = new Error('Unauthorized');
|
|
|
|
error.type = 'auth';
|
|
|
|
error.subtype = 'user-not-found';
|
|
|
|
error.http_status = 404;
|
|
|
|
error.message = errorUserNotFoundMessageTemplate(res.locals.user);
|
|
|
|
next(error);
|
|
|
|
}
|
2018-06-06 21:48:22 +08:00
|
|
|
|
|
|
|
return next();
|
2018-05-29 19:23:50 +08:00
|
|
|
});
|
2018-02-17 01:21:06 +08:00
|
|
|
};
|
2016-10-04 21:19:31 +08:00
|
|
|
};
|
2018-05-29 19:23:50 +08:00
|
|
|
|
|
|
|
function getUserNameFromRequest(req, cdbRequest) {
|
|
|
|
return cdbRequest.userByReq(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkUserExists(metadataBackend, userName, callback) {
|
|
|
|
metadataBackend.getUserId(userName, function(err) {
|
2018-06-06 21:48:22 +08:00
|
|
|
callback(err, !err);
|
2018-05-29 19:23:50 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorUserNotFoundMessageTemplate(user) {
|
|
|
|
return `Sorry, we can't find CARTO user '${user}'. Please check that you have entered the correct domain.`;
|
|
|
|
}
|