diff --git a/lib/cartodb/carto_data.js b/lib/cartodb/carto_data.js index 451f6bde..9d31311f 100644 --- a/lib/cartodb/carto_data.js +++ b/lib/cartodb/carto_data.js @@ -19,6 +19,7 @@ module.exports = function() { user_metadata_db: 5, table_metadata_db: 0, user_key: "rails:users:<%= username %>", + map_key: "rails:users:<%= username %>:map_key", table_key: "rails:<%= database_name %>:<%= table_name %>" }; @@ -48,7 +49,7 @@ module.exports = function() { */ me.getId= function(req, callback) { // strip subdomain from header host - var username = req.headers.host.split('.')[0] + var username = req.headers.host.split('.')[0]; var redisKey = _.template(this.user_key, {username: username}); this.retrieve(this.user_metadata_db, redisKey, 'id', callback); @@ -60,12 +61,11 @@ module.exports = function() { * @param req - standard express req object. importantly contains host information * @param callback */ - me.getMapKey = function(req, callback) { + me.checkMapKey = function(req, callback) { // strip subdomain from header host - var username = req.headers.host.split('.')[0] - var redisKey = _.template(this.user_key, {username: username}); - - this.retrieve(this.user_metadata_db, redisKey, 'map_key', callback); + var username = req.headers.host.split('.')[0]; + var redisKey = _.template(this.map_key, {username: username}); + this.inSet(this.user_metadata_db, redisKey, req.query.map_key, callback); }; /** @@ -79,12 +79,12 @@ module.exports = function() { Step( function(){ - that.getMapKey(req, this); + that.checkMapKey(req, this); }, - function checkIfInternal(err, data){ + function checkIfInternal(err, check_result){ if (err) throw err; - - if (data === req.query.map_key){ + + if (check_result === 1){ callback(err, true); // Internal access so early exit with access. } else { return true; // continue to check if the table is public/private @@ -175,24 +175,35 @@ module.exports = function() { ); }; + // Redis Hash lookup + me.retrieve = function(db, redisKey, hashKey, callback) { + this.redisCmd(db,'HGET',[redisKey, hashKey], callback); + }; + + // Redis Set member check + me.inSet = function(db, setKey, member, callback) { + this.redisCmd(db,'SISMEMBER',[setKey, member], callback); + }; /** - * Make a HASH data access call to Redis + * Use Redis * - * @param redisKey - the base redis key where the metadata hash lives - * @param hashKey - the specific metadata you want to retrieve - * @param callback - function to pass metadata too. err,data args + * @param db - redis database number + * @param redisFunc - the redis function to execute + * @param redisArgs - the arguments for the redis function in an array + * @param callback - function to pass results too. */ - me.retrieve = function(db, redisKey, hashKey, callback) { + me.redisCmd = function(db, redisFunc, redisArgs, callback) { var redisClient; Step( function getRedisClient() { redis_pool.acquire(db, this); }, - function lookupMetadata(err, data) { + function executeQuery(err, data) { redisClient = data; - redisClient.HGET(redisKey, hashKey, this); + redisArgs.push(this); + redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); }, function releaseRedisClient(err, data) { if (err) throw err;