Windshaft-cartodb/lib/cartodb/api/user_limits_api.js

80 lines
2.0 KiB
JavaScript
Raw Normal View History

var step = require('step');
2015-07-11 01:10:55 +08:00
/**
*
* @param metadataBackend
* @param options
* @constructor
* @type {UserLimitsApi}
*/
function UserLimitsApi(metadataBackend, options) {
this.metadataBackend = metadataBackend;
this.options = options || {};
this.options.limits = this.options.limits || {};
}
module.exports = UserLimitsApi;
UserLimitsApi.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
UserLimitsApi.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) {
var self = this;
step(
function isAuthorized() {
var next = this;
if (!apiKey) {
return next(null, false);
}
self.metadataBackend.getUserMapKey(username, function (err, userApiKey) {
if (err) {
return next(err);
}
return next(null, userApiKey === apiKey);
});
},
function getUserTimeoutRenderLimits(err, authorized) {
2017-07-18 17:56:54 +08:00
var next = this;
if (err) {
return next(err);
}
2017-07-18 17:56:54 +08:00
self.metadataBackend.getUserTimeoutRenderLimits(username, function (err, timeoutRenderLimit) {
if (err) {
2017-08-02 17:07:44 +08:00
return next(err);
}
next(null, {
render: authorized ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic
});
});
},
callback
);
2015-07-11 01:10:55 +08:00
};