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

85 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict';
2015-07-11 01:10:55 +08:00
/**
*
* @param metadataBackend
* @param options
* @constructor
* @type {UserLimitsBackend}
2015-07-11 01:10:55 +08:00
*/
2019-10-22 01:07:24 +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) {
2018-07-30 23:21:19 +08:00
isAuthorized(this.metadataBackend, username, apiKey, (err, authorized) => {
2018-07-30 23:15:04 +08:00
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:21:19 +08:00
this.metadataBackend.getUserTimeoutRenderLimits(username, (err, timeoutRenderLimit) => {
if (err) {
2018-07-30 23:18:35 +08:00
return callback(err);
}
2017-07-18 17:56:54 +08:00
2018-07-31 21:19:41 +08:00
return callback(
2018-07-30 23:18:35 +08:00
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
};
2019-10-22 01:07:24 +08:00
function isAuthorized (metadataBackend, username, apiKey, callback) {
2018-07-30 23:15:04 +08:00
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);
};