delete existing record

This commit is contained in:
AJ ONeal 2018-01-29 14:54:03 -07:00
parent 656c803720
commit 9dfa8ed323
4 changed files with 120 additions and 6 deletions

View File

@ -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) {
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')));

View File

@ -48,6 +48,7 @@
<span class="js-record-refresh">{{ refresh }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-a js-record-aaaa">
<input type="hidden" class="js-record-id" />
@ -56,6 +57,7 @@
<span class="js-record-address">{{ addr }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-aname js-record-cname js-record-ns">
<input type="hidden" class="js-record-id" />
@ -64,6 +66,7 @@
<span class="js-record-data">{{ target }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-caa">
<input type="hidden" class="js-record-id" />
@ -73,6 +76,7 @@
<span class="js-record-value">{{ value }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-mx">
<input type="hidden" class="js-record-id" />
@ -82,6 +86,7 @@
<span class="js-record-priority">{{ priority }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-ptr">
<input type="hidden" class="js-record-id" />
@ -89,6 +94,7 @@
<span class="js-record-name">example.com</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-srv">
<input type="hidden" class="js-record-id" />
@ -98,6 +104,7 @@
<span class="js-record-priority">{{ priority }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-txt">
<input type="hidden" class="js-record-id" />
@ -106,6 +113,7 @@
<span class="js-record-data">{{ text data }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
<li class="js-record-typex">
<input type="hidden" class="js-record-id" />
@ -114,6 +122,7 @@
<span class="js-record-rr">{{ hex }}</span>
<span class="js-record-ttl">{{ ttl }}</span>
<button type="button" class="js-record-edit">Edit</button>
<button type="button" class="js-record-destroy">X</button>
</li>
</ul>

View File

@ -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.

View File

@ -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,8 +223,10 @@ 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) {
if (notDeleted(r)) {
return true;
}
}
});
process.nextTick(function () {
cb(null, myRecords);
@ -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;