From 9dfa8ed3232a593b55b5fea70a5fe3fde2dae578 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 29 Jan 2018 14:54:03 -0700 Subject: [PATCH] delete existing record --- lib/httpd.js | 37 +++++++++++++++++++++++++++++++++-- lib/public/index.html | 9 +++++++++ lib/public/js/app.js | 35 +++++++++++++++++++++++++++++++++ lib/store.json.js | 45 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/lib/httpd.js b/lib/httpd.js index d87b9a2..4856c22 100644 --- a/lib/httpd.js +++ b/lib/httpd.js @@ -256,8 +256,12 @@ module.exports.create = function (cli, engine/*, dnsd*/) { if ('SOA' === record.type) { // TODO be strict about what can be edited - engine.zones.save(record, function (err, record) { - res.send({ success: true }); + engine.zones.save(record, function (err/*, record*/) { + if (!err) { + res.send({ success: true }); + return; + } + res.send({ error: { message: err.message } }); }); } else { engine.records.save(record, function (err, record) { @@ -265,6 +269,35 @@ module.exports.create = function (cli, engine/*, dnsd*/) { }); } }); + app.delete('/api/records/:id', jsonParser, hasClaim('+rw@adns.org'), function (req, res) { + var id = req.params.id; + + engine.records.one(id, function (err, record) { + if (err) { + res.send({ error: { message: err.message } }); + return; + } + + if ('SOA' === record.type) { + // TODO be strict about what can be edited + engine.zones.destroy(id, function (err, record) { + if (!err) { + res.send(record); + return; + } + res.send({ error: { message: err.message } }); + }); + } else { + engine.records.destroy(id, function (err, record) { + if (!err) { + res.send(record || { error: { message: "no record with id '" + id + "' found" } }); + return; + } + res.send({ error: { message: err.message } }); + }); + } + }); + }); app.use('/', express.static(path.join(__dirname, 'public'))); diff --git a/lib/public/index.html b/lib/public/index.html index db59bf3..d9806d0 100644 --- a/lib/public/index.html +++ b/lib/public/index.html @@ -48,6 +48,7 @@ {{ refresh }} {{ ttl }} +
  • @@ -56,6 +57,7 @@ {{ addr }} {{ ttl }} +
  • @@ -64,6 +66,7 @@ {{ target }} {{ ttl }} +
  • @@ -73,6 +76,7 @@ {{ value }} {{ ttl }} +
  • @@ -82,6 +86,7 @@ {{ priority }} {{ ttl }} +
  • @@ -89,6 +94,7 @@ example.com {{ ttl }} +
  • @@ -98,6 +104,7 @@ {{ priority }} {{ ttl }} +
  • @@ -106,6 +113,7 @@ {{ text data }} {{ ttl }} +
  • @@ -114,6 +122,7 @@ {{ hex }} {{ ttl }} +
  • diff --git a/lib/public/js/app.js b/lib/public/js/app.js index 1a49681..3c6d1a2 100644 --- a/lib/public/js/app.js +++ b/lib/public/js/app.js @@ -391,6 +391,41 @@ }); }); }); + $on('button.js-record-destroy', 'click', function (ev) { + console.log('destroy'); + var $pel = ev.target.parentElement; + var id = $qs('.js-record-id', $pel).value; + var existingRecord = cache.recordsMap[id]; + + delete cache.recordsMap[id]; + cache.records.some(function (r, i) { + if (r === existingRecord) { + cache.records.splice(i, 1); + return true; + } + }); + + renderRecords(); + + return window.fetch( + '/api/records/' + id + , { method: 'DELETE' + , headers: new window.Headers({ + 'Authorization': 'Bearer ' + auth + , 'Content-Type': 'application/json;charset=UTF-8' + }) + } + ).then(function (resp) { + return resp.json().then(function (data) { + if (data.error) { + console.error(data); + window.alert(data.error.message); + return; + } + console.log('result:', data); + }); + }); + }); $qs('select.js-record-form-type').value = ''; // Create a new 'change' event and dispatch it. diff --git a/lib/store.json.js b/lib/store.json.js index bf421e7..5c33496 100644 --- a/lib/store.json.js +++ b/lib/store.json.js @@ -4,6 +4,10 @@ module.exports.create = function (opts) { // opts = { filepath }; var engine = { db: null }; + function notDeleted(r) { + return !r.revokedAt && !r.deletedAt; + } + var db = require(opts.filepath); var stat = require('fs').statSync(opts.filepath); var crypto = require('crypto'); @@ -105,7 +109,7 @@ module.exports.create = function (opts) { engine.zones = { all: function (cb) { process.nextTick(function () { - cb(null, db.zones.slice(0)); + cb(null, db.zones.slice(0).filter(notDeleted)); }); } , get: function (queries, cb) { @@ -116,7 +120,7 @@ module.exports.create = function (opts) { } var myDomains = db.zones.filter(function (d) { return queries.some(function (q) { - return d.name.toLowerCase() === q.name; + return (d.name.toLowerCase() === q.name) && notDeleted(d); }); }); process.nextTick(function () { @@ -191,7 +195,22 @@ module.exports.create = function (opts) { engine.records = { all: function (cb) { process.nextTick(function () { - cb(null, db.records.slice(0)); + cb(null, db.records.slice(0).filter(notDeleted)); + }); + } + , one: function (id, cb) { + var myRecord; + db.records.slice(0).some(function (r) { + if (id && id === r.id) { + if (notDeleted(r)) { + myRecord = r; + return true; + } + return false; + } + }); + process.nextTick(function () { + cb(null, myRecord); }); } , get: function (query, cb) { @@ -204,7 +223,9 @@ module.exports.create = function (opts) { // TODO use IN in masterquest (or implement OR) // Only return single-level wildcard? if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) { - return true; + if (notDeleted(r)) { + return true; + } } }); process.nextTick(function () { @@ -290,6 +311,22 @@ module.exports.create = function (opts) { cb(err, record); }); } + , destroy: function (id, cb) { + var record; + db.records.some(function (r/*, i*/) { + if (id === r.id) { + record = r; + r.deletedAt = Date.now(); + //record = db.records.splice(i, 1); + return true; + } + }); + process.nextTick(function () { + db.save(function (err) { + cb(err, record); + }); + }); + } }; return engine;