From 4f180704762faff5cc7407ee62013873734ded80 Mon Sep 17 00:00:00 2001 From: Garren Smith Date: Tue, 21 Jan 2014 21:57:23 +0200 Subject: Fauxton: block user from deleting _id and _rev from doc --- src/fauxton/app/addons/documents/views.js | 33 ++++++++++++++++++++++++++++ src/fauxton/app/addons/fauxton/components.js | 4 ++++ 2 files changed, 37 insertions(+) diff --git a/src/fauxton/app/addons/documents/views.js b/src/fauxton/app/addons/documents/views.js index d5319f39d..702beeeaa 100644 --- a/src/fauxton/app/addons/documents/views.js +++ b/src/fauxton/app/addons/documents/views.js @@ -959,6 +959,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum this.editor = new Components.Editor({ editorId: "editor-container", + forceMissingId: true, commands: [{ name: 'save', bindKey: {win: 'Ctrl-S', mac: 'Ctrl-S'}, @@ -970,6 +971,38 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum }); this.editor.render(); this.model.on("sync", this.updateValues, this); + + var editor = this.editor, + model = this.model; + + editor.editor.on("change", function (event) { + //if (event.data.action !== 'removeText') { return; } + //if (!event.data.text.match(/_id/) && !event.data.text.match(/_rev/)) { return; } + + var changedDoc; + try { + changedDoc = JSON.parse(editor.getValue()); + } catch(exception) { + //not complete doc. Cannot work with it + return; + } + + var keyChecked = ["_id"]; + if (model.get("_rev")) { keyChecked.push("_rev");} + + //check the changedDoc has all the required standard keys + if (_.isEmpty(_.difference(keyChecked, _.keys(changedDoc)))) { return; } + + editor.setReadOnly(true); + setTimeout(function () { editor.setReadOnly(false);}, 400); + // use extend so that _id stays at the top of the object with displaying the doc + changedDoc = _.extend({_id: model.id, _rev: model.get("_rev")}, changedDoc); + editor.setValue(JSON.stringify(changedDoc, null, " ")); + FauxtonAPI.addNotification({ + type: "error", + msg: "Cannot remove a documents Id or Revision." + }); + }); }, cleanup: function () { diff --git a/src/fauxton/app/addons/fauxton/components.js b/src/fauxton/app/addons/fauxton/components.js index cda61c590..6ff0ce372 100644 --- a/src/fauxton/app/addons/fauxton/components.js +++ b/src/fauxton/app/addons/fauxton/components.js @@ -313,6 +313,10 @@ function(app, FauxtonAPI, ace) { this.edited = false; }, + setReadOnly: function (value) { + return this.editor.setReadOnly(value); + }, + setValue: function (data, lineNumber) { lineNumber = lineNumber ? lineNumber : -1; this.editor.setValue(data, lineNumber); -- cgit v1.2.1