2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
module.exports = class CdbRequest {
|
|
|
|
constructor () {
|
|
|
|
// would extract "strk" from "strk.cartodb.com"
|
|
|
|
this.RE_USER_FROM_HOST = new RegExp(global.settings.user_from_host || '^([^\\.]+)\\.');
|
|
|
|
}
|
2013-12-18 18:57:46 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
userByReq (req) {
|
|
|
|
const host = req.headers.host || '';
|
2013-12-18 18:57:46 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
if (req.params.user) {
|
|
|
|
return req.params.user;
|
|
|
|
}
|
2013-12-18 18:57:46 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
const mat = host.match(this.RE_USER_FROM_HOST);
|
2015-02-02 19:09:34 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
if (!mat || mat.length !== 2) {
|
|
|
|
throw new Error(`No username found in hostname '${host}'`);
|
|
|
|
}
|
2015-02-02 19:09:34 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
return mat[1];
|
2015-02-02 19:09:34 +08:00
|
|
|
}
|
2020-06-30 23:42:59 +08:00
|
|
|
};
|