You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/02/12 07:20:25 UTC

[31/52] [abbrv] fauxton commit: updated refs/heads/import-master to d11b90b

Fauxton: block user from deleting _id and _rev from doc


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

Branch: refs/heads/import-master
Commit: c9ad37bc0f5bc7aa3cf16b04ddffb6baf7c5ba29
Parents: 77ce5d0
Author: Garren Smith <ga...@gmail.com>
Authored: Tue Jan 21 21:57:23 2014 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Tue Jan 28 09:02:05 2014 +0200

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


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/c9ad37bc/app/addons/documents/views.js
----------------------------------------------------------------------
diff --git a/app/addons/documents/views.js b/app/addons/documents/views.js
index bd89527..e2f5578 100644
--- a/app/addons/documents/views.js
+++ b/app/addons/documents/views.js
@@ -961,6 +961,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'},
@@ -972,6 +973,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-fauxton/blob/c9ad37bc/app/addons/fauxton/components.js
----------------------------------------------------------------------
diff --git a/app/addons/fauxton/components.js b/app/addons/fauxton/components.js
index 8b8aa5b..edde428 100644
--- a/app/addons/fauxton/components.js
+++ b/app/addons/fauxton/components.js
@@ -317,6 +317,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);