Add object key/ID validation. Centralize key generation/checking.

This commit is contained in:
Aurimas Vinckevicius 2014-08-12 00:20:21 -05:00
parent e1f59482c4
commit dcd65d087c
5 changed files with 33 additions and 27 deletions

View file

@ -1008,8 +1008,3 @@ Zotero.Collection.prototype._refreshChildItems = Zotero.Promise.coroutine(functi
return this.loadChildItems(true); return this.loadChildItems(true);
} }
}); });
Zotero.Collection.prototype._generateKey = function () {
return Zotero.Utilities.generateObjectKey();
}

View file

@ -27,17 +27,32 @@
Zotero.DataObjectUtilities = { Zotero.DataObjectUtilities = {
"checkLibraryID": function (libraryID) { "checkLibraryID": function (libraryID) {
if (libraryID === null) { if (libraryID === null) {
Zotero.debug("Deprecated: libraryID cannot be NULL\n\n" + Components.stack, 2); Zotero.debug("Deprecated: libraryID cannot be NULL", 2, 1);
} }
else { else {
var intValue = parseInt(libraryID); var intValue = parseInt(libraryID);
if (libraryID != intValue) { if (libraryID != intValue || intValue < 0) {
throw new Error("libraryID must be an integer"); throw new Error("libraryID must be a positive integer");
} }
} }
return intValue; return intValue;
}, },
"checkDataID": function(dataID) {
var intValue = parseInt(dataID);
if (dataID != intValue || dataID < 0)
throw new Error("id must be a positive integer");
return intValue;
},
"checkKey": function(key) {
if (!key) return null;
if (!Zotero.Utilities.isValidObjectKey(key)) {
throw new Error("key is not valid");
}
return key;
},
"getObjectTypePlural": function getObjectTypePlural(objectType) { "getObjectTypePlural": function getObjectTypePlural(objectType) {
return objectType == 'search' ? 'searches' : objectType + 's'; return objectType == 'search' ? 'searches' : objectType + 's';
}, },

View file

@ -83,13 +83,6 @@ Zotero.ID_Tracker = function () {
} }
}); });
this.isValidKey = function (value) {
var re = /^[23456789ABCDEFGHIJKLMNPQRSTUVWXYZ]{8}$/
return re.test(value);
}
function getBigInt(max) { function getBigInt(max) {
if (!max) { if (!max) {
max = 9007199254740991; max = 9007199254740991;

View file

@ -1642,13 +1642,6 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
this._sqlParams = sqlParams.length ? sqlParams : false; this._sqlParams = sqlParams.length ? sqlParams : false;
}); });
Zotero.Search.prototype._generateKey = function () {
return Zotero.Utilities.generateObjectKey();
}
Zotero.Searches = new function(){ Zotero.Searches = new function(){
Zotero.DataObjects.apply(this, ['search', 'searches', 'savedSearch', 'savedSearches']); Zotero.DataObjects.apply(this, ['search', 'searches', 'savedSearch', 'savedSearches']);
this.constructor.prototype = new Zotero.DataObjects(); this.constructor.prototype = new Zotero.DataObjects();

View file

@ -1812,16 +1812,26 @@ Zotero.Utilities = {
return Zotero.ItemTypes.getImageSrc(attachment.mimeType === "application/pdf" return Zotero.ItemTypes.getImageSrc(attachment.mimeType === "application/pdf"
? "attachment-pdf" : "attachment-snapshot"); ? "attachment-pdf" : "attachment-snapshot");
}, },
"allowedKeyChars": "23456789ABCDEFGHIJKLMNPQRSTUVWXYZ",
/** /**
* Generates a valid object key for the server API * Generates a valid object key for the server API
*/ */
"generateObjectKey":function generateObjectKey() { "generateObjectKey":function generateObjectKey() {
// TODO: add 'L' and 'Y' after 3.0.11 cut-off return Zotero.Utilities.randomString(8, Zotero.Utilities.allowedKeyChars);
var baseString = "23456789ABCDEFGHIJKMNPQRSTUVWXZ";
return Zotero.Utilities.randomString(8, baseString);
}, },
/**
* Check if an object key is in a valid format
*/
"isValidObjectKey":function(key) {
if (!Zotero.Utilities.objectKeyRegExp) {
Zotero.Utilities.objectKeyRegExp = new RegExp('^[' + Zotero.Utilities.allowedKeyChars + ']{8}$');
}
return Zotero.Utilities.objectKeyRegExp.test(key);
},
/** /**
* Provides unicode support and other additional features for regular expressions * Provides unicode support and other additional features for regular expressions
* See https://github.com/slevithan/xregexp for usage * See https://github.com/slevithan/xregexp for usage