Windshaft-cartodb/lib/cartodb/backends/user-limits.js

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-07-11 01:10:55 +08:00
/**
*
* @param metadataBackend
* @param options
* @constructor
* @type {UserLimitsBackend}
2015-07-11 01:10:55 +08:00
*/
function UserLimitsBackend(metadataBackend, options) {
2015-07-11 01:10:55 +08:00
this.metadataBackend = metadataBackend;
this.options = options || {};
this.options.limits = this.options.limits || {};
this.preprareRateLimit();
2015-07-11 01:10:55 +08:00
}
module.exports = UserLimitsBackend;
2015-07-11 01:10:55 +08:00
UserLimitsBackend.prototype.getRenderLimits = function (username, apiKey, callback) {
2015-07-11 01:10:55 +08:00
var self = this;
var limits = {
cacheOnTimeout: self.options.limits.cacheOnTimeout || false,
render: self.options.limits.render || 0
};
2017-08-10 21:58:25 +08:00
self.getTimeoutRenderLimit(username, apiKey, function (err, timeoutRenderLimit) {
if (err) {
return callback(err);
2015-07-11 01:10:55 +08:00
}
2017-08-10 21:58:25 +08:00
if (timeoutRenderLimit && timeoutRenderLimit.render) {
if (Number.isFinite(timeoutRenderLimit.render)) {
limits.render = timeoutRenderLimit.render;
}
}
return callback(null, limits);
});
};
2015-07-11 01:10:55 +08:00
UserLimitsBackend.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) {
var self = this;
2018-07-30 23:15:04 +08:00
isAuthorized(self.metadataBackend, username, apiKey, (err, authorized) => {
if (err) {
return callback(err);
2018-07-30 23:09:34 +08:00
}
2017-07-18 17:56:54 +08:00
2018-07-30 23:09:34 +08:00
self.metadataBackend.getUserTimeoutRenderLimits(username, function (err, timeoutRenderLimit) {
if (err) {
return next(err);
}
2017-07-18 17:56:54 +08:00
2018-07-30 23:09:34 +08:00
next(null, {
render: authorized ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic
});
2018-07-30 23:09:34 +08:00
});
2018-07-30 23:16:27 +08:00
});
2015-07-11 01:10:55 +08:00
};
2018-07-30 23:15:04 +08:00
function isAuthorized(metadataBackend, username, apiKey, callback) {
if (!apiKey) {
return callback(null, false);
}
metadataBackend.getUserMapKey(username, function (err, userApiKey) {
if (err) {
return callback(err);
}
return callback(null, userApiKey === apiKey);
});
}
UserLimitsBackend.prototype.preprareRateLimit = function () {
if (this.options.limits.rateLimitsEnabled) {
2018-03-03 21:46:39 +08:00
this.metadataBackend.loadRateLimitsScript();
}
};
UserLimitsBackend.prototype.getRateLimit = function (user, endpointGroup, callback) {
2018-03-03 21:46:39 +08:00
this.metadataBackend.getRateLimit(user, 'maps', endpointGroup, callback);
};