Fixed issue 21674: Added 'OB.Utilities.generateRandomString' function
authorDavid Baz Fayos <david.baz@openbravo.com>
Mon, 17 Sep 2012 18:52:35 +0200
changeset 17970 f7344c95ba24
parent 17969 06e1886fd04d
child 17971 1ba52792abc2
Fixed issue 21674: Added 'OB.Utilities.generateRandomString' function
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js	Mon Sep 17 18:18:41 2012 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/toolbar/ob-toolbar.js	Mon Sep 17 18:52:35 2012 +0200
@@ -523,19 +523,7 @@
 
     this.Super('initWidget', arguments);
 
-    function getRandomId() {
-      var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz',
-          stringLength = 8,
-          randomString = '',
-          i, rnum;
-      for (i = 0; i < stringLength; i++) {
-        rnum = Math.floor(Math.random() * chars.length);
-        randomString += chars.substring(rnum, rnum + 1);
-      }
-      return randomString;
-    }
-
-    this.randomId = getRandomId();
+    this.randomId = OB.Utilities.generateRandomString(8, true, true, true, false);
 
     this.members = null;
 
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js	Mon Sep 17 18:18:41 2012 +0200
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities.js	Mon Sep 17 18:52:35 2012 +0200
@@ -922,6 +922,51 @@
   return object[property];
 };
 
+//** {{{ OB.Utilities.generateRandomString }}} **
+//
+// Generates a random string based on the arguments
+// Parameters:
+//  * {{{stringLength}}} Length of the generated random string
+//  * {{{allowLowerCaseChars}}} Boolean to check if lower case characters are allowed (true by default)
+//  * {{{allowUpperCaseChars}}} Boolean to check if upper case characters are allowed (true by default)
+//  * {{{allowDigits}}} Boolean to check if digits are allowed (false by default)
+//  * {{{allowSpecialChars}}} Boolean to check if special characters are allowed (false by default)
+OB.Utilities.generateRandomString = function (stringLength, allowLowerCaseChars, allowUpperCaseChars, allowDigits, allowSpecialChars) {
+  stringLength = parseInt(stringLength, 10);
+  if (!stringLength) {
+    stringLength = 1;
+  }
+  allowLowerCaseChars = (allowLowerCaseChars !== false ? true : false);
+  allowUpperCaseChars = (allowUpperCaseChars !== false ? true : false);
+  allowDigits = (allowDigits !== true ? false : true);
+  allowSpecialChars = (allowSpecialChars !== true ? false : true);
+
+  var chars = '',
+      randomString = '',
+      i, rnum;
+  if (allowLowerCaseChars) {
+    chars += 'abcdefghijklmnopqrstuvwxyz';
+  }
+  if (allowUpperCaseChars) {
+    chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+  }
+  if (allowDigits) {
+    chars += '0123456789';
+  }
+  if (allowSpecialChars) {
+    chars += '!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_';
+  }
+  if (chars === '') {
+    return '';
+  }
+
+  for (i = 0; i < stringLength; i++) {
+    rnum = Math.floor(Math.random() * chars.length);
+    randomString += chars.substring(rnum, rnum + 1);
+  }
+  return randomString;
+};
+
 /* This function will return true if it receives a string parameter, and 
  * which complies with the OB UUID format (that is, its a
  * hexadecimal number of length 32)