Updated Branches:
  refs/heads/id-ace-warning [created] 4f1807047

Fauxton: block user from deleting _id and _rev from doc


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/4f180704
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/4f180704
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/4f180704

Branch: refs/heads/id-ace-warning
Commit: 4f180704762faff5cc7407ee62013873734ded80
Parents: 15f4fff
Author: Garren Smith <[email protected]>
Authored: Tue Jan 21 21:57:23 2014 +0200
Committer: Garren Smith <[email protected]>
Committed: Wed Jan 22 12:01:33 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/views.js    | 33 +++++++++++++++++++++++
 src/fauxton/app/addons/fauxton/components.js |  4 +++
 2 files changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/4f180704/src/fauxton/app/addons/documents/views.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/documents/views.js 
b/src/fauxton/app/addons/documents/views.js
index d5319f3..702beee 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 () {

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4f180704/src/fauxton/app/addons/fauxton/components.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/fauxton/components.js 
b/src/fauxton/app/addons/fauxton/components.js
index cda61c5..6ff0ce3 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);

Reply via email to