From a300bb6e9268f047f990780edfda37cd9ae1c7c0 Mon Sep 17 00:00:00 2001 From: booo Date: Sat, 28 Jan 2012 13:24:58 +0100 Subject: [PATCH] move randomString function into module --- bin/convert.js | 16 +--------------- node/db/AuthorManager.js | 17 ++--------------- node/db/GroupManager.js | 16 +--------------- node/db/ReadOnlyManager.js | 17 ++--------------- node/db/SecurityManager.js | 2 ++ node/db/SessionManager.js | 16 +--------------- node/handler/APIHandler.js | 16 +--------------- node/utils/randomstring.js | 16 ++++++++++++++++ 8 files changed, 26 insertions(+), 90 deletions(-) create mode 100644 node/utils/randomstring.js diff --git a/bin/convert.js b/bin/convert.js index d410f7a4a..4302114c6 100644 --- a/bin/convert.js +++ b/bin/convert.js @@ -4,6 +4,7 @@ var ueberDB = require("ueberDB"); var mysql = require("mysql"); var async = require("async"); var Changeset = require("../node/utils/Changeset"); +var randomString = require("../node/utils/randomstring"); var AttributePoolFactory = require("../node/utils/AttributePoolFactory"); var settingsFile = process.argv[2]; @@ -450,18 +451,3 @@ function parsePage(array, pageStart, offsets, data, json) start+=unitLength; } } - -/** - * Generates a random String with the given length. Is needed to generate the Author Ids - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} diff --git a/node/db/AuthorManager.js b/node/db/AuthorManager.js index f4f42d112..7c054a56f 100644 --- a/node/db/AuthorManager.js +++ b/node/db/AuthorManager.js @@ -22,6 +22,8 @@ var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); +var randomString = require("../utils/randomstring"); + /** * Checks if the author exists */ @@ -177,18 +179,3 @@ exports.setAuthorName = function (author, name, callback) { db.setSub("globalAuthor:" + author, ["name"], name, callback); } - -/** - * Generates a random String with the given length. Is needed to generate the Author Ids - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 473ea9b77..7e3b7d6d1 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -20,6 +20,7 @@ var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); +var randomString = require("../utils/randomstring"); var db = require("./DB").db; var async = require("async"); var padManager = require("./PadManager"); @@ -255,18 +256,3 @@ exports.listPads = function(groupID, callback) } }); } - -/** - * Generates a random String with the given length. Is needed to generate the Author Ids - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} diff --git a/node/db/ReadOnlyManager.js b/node/db/ReadOnlyManager.js index 73b3be9ec..1e5079c52 100644 --- a/node/db/ReadOnlyManager.js +++ b/node/db/ReadOnlyManager.js @@ -22,6 +22,8 @@ var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); +var randomString = require("../utils/randomstring"); + /** * returns a read only id for a pad * @param {String} padId the id of the pad @@ -70,18 +72,3 @@ exports.getPadId = function(readOnlyId, callback) { db.get("readonly2pad:" + readOnlyId, callback); } - -/** - * Generates a random String with the given length. Is needed to generate the read only ids - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} diff --git a/node/db/SecurityManager.js b/node/db/SecurityManager.js index 52d5afcbe..4b86d868a 100644 --- a/node/db/SecurityManager.js +++ b/node/db/SecurityManager.js @@ -26,6 +26,8 @@ var padManager = require("./PadManager"); var sessionManager = require("./SessionManager"); var settings = require("../utils/Settings") +var randomString = require("../utils/randomstring"); + /** * This function controlls the access to a pad, it checks if the user can access a pad. * @param padID the pad the user wants to access diff --git a/node/db/SessionManager.js b/node/db/SessionManager.js index a394f5442..084d4a695 100644 --- a/node/db/SessionManager.js +++ b/node/db/SessionManager.js @@ -20,6 +20,7 @@ var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); +var randomString = require("../utils/randomstring"); var db = require("./DB").db; var async = require("async"); var groupMangager = require("./GroupManager"); @@ -358,21 +359,6 @@ function listSessionsWithDBKey (dbkey, callback) }); } -/** - * Generates a random String with the given length. Is needed to generate the SessionIDs - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} - //checks if a number is an int function is_int(value) { diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index 0cd9cb58e..ca45e1f8f 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -22,6 +22,7 @@ var ERR = require("async-stacktrace"); var fs = require("fs"); var api = require("../db/API"); var padManager = require("../db/PadManager"); +var randomString = require("../utils/randomstring"); //ensure we have an apikey var apikey = null; @@ -157,18 +158,3 @@ function callAPI(functionName, fields, req, res) //call the api function api[functionName](functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]); } - -/** - * Generates a random String with the given length. Is needed to generate the Author Ids - */ -function randomString(len) -{ - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var randomstring = ''; - for (var i = 0; i < len; i++) - { - var rnum = Math.floor(Math.random() * chars.length); - randomstring += chars.substring(rnum, rnum + 1); - } - return randomstring; -} diff --git a/node/utils/randomstring.js b/node/utils/randomstring.js new file mode 100644 index 000000000..4c1bba244 --- /dev/null +++ b/node/utils/randomstring.js @@ -0,0 +1,16 @@ +/** + * Generates a random String with the given length. Is needed to generate the Author, Group, readonly, session Ids + */ +var randomString = function randomString(len) +{ + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + var randomstring = ''; + for (var i = 0; i < len; i++) + { + var rnum = Math.floor(Math.random() * chars.length); + randomstring += chars.substring(rnum, rnum + 1); + } + return randomstring; +}; + +module.exports = randomString;