delete existing record
This commit is contained in:
parent
656c803720
commit
9dfa8ed323
35
lib/httpd.js
35
lib/httpd.js
|
@ -256,8 +256,12 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
|
||||||
|
|
||||||
if ('SOA' === record.type) {
|
if ('SOA' === record.type) {
|
||||||
// TODO be strict about what can be edited
|
// 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 });
|
res.send({ success: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.send({ error: { message: err.message } });
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
engine.records.save(record, function (err, record) {
|
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')));
|
app.use('/', express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@
|
||||||
<span class="js-record-refresh">{{ refresh }}</span>
|
<span class="js-record-refresh">{{ refresh }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-a js-record-aaaa">
|
<li class="js-record-a js-record-aaaa">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -56,6 +57,7 @@
|
||||||
<span class="js-record-address">{{ addr }}</span>
|
<span class="js-record-address">{{ addr }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-aname js-record-cname js-record-ns">
|
<li class="js-record-aname js-record-cname js-record-ns">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -64,6 +66,7 @@
|
||||||
<span class="js-record-data">{{ target }}</span>
|
<span class="js-record-data">{{ target }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-caa">
|
<li class="js-record-caa">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -73,6 +76,7 @@
|
||||||
<span class="js-record-value">{{ value }}</span>
|
<span class="js-record-value">{{ value }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-mx">
|
<li class="js-record-mx">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -82,6 +86,7 @@
|
||||||
<span class="js-record-priority">{{ priority }}</span>
|
<span class="js-record-priority">{{ priority }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-ptr">
|
<li class="js-record-ptr">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -89,6 +94,7 @@
|
||||||
<span class="js-record-name">example.com</span>
|
<span class="js-record-name">example.com</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-srv">
|
<li class="js-record-srv">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -98,6 +104,7 @@
|
||||||
<span class="js-record-priority">{{ priority }}</span>
|
<span class="js-record-priority">{{ priority }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-txt">
|
<li class="js-record-txt">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -106,6 +113,7 @@
|
||||||
<span class="js-record-data">{{ text data }}</span>
|
<span class="js-record-data">{{ text data }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
<li class="js-record-typex">
|
<li class="js-record-typex">
|
||||||
<input type="hidden" class="js-record-id" />
|
<input type="hidden" class="js-record-id" />
|
||||||
|
@ -114,6 +122,7 @@
|
||||||
<span class="js-record-rr">{{ hex }}</span>
|
<span class="js-record-rr">{{ hex }}</span>
|
||||||
<span class="js-record-ttl">{{ ttl }}</span>
|
<span class="js-record-ttl">{{ ttl }}</span>
|
||||||
<button type="button" class="js-record-edit">Edit</button>
|
<button type="button" class="js-record-edit">Edit</button>
|
||||||
|
<button type="button" class="js-record-destroy">X</button>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
|
@ -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 = '';
|
$qs('select.js-record-form-type').value = '';
|
||||||
// Create a new 'change' event and dispatch it.
|
// Create a new 'change' event and dispatch it.
|
||||||
|
|
|
@ -4,6 +4,10 @@ module.exports.create = function (opts) {
|
||||||
// opts = { filepath };
|
// opts = { filepath };
|
||||||
var engine = { db: null };
|
var engine = { db: null };
|
||||||
|
|
||||||
|
function notDeleted(r) {
|
||||||
|
return !r.revokedAt && !r.deletedAt;
|
||||||
|
}
|
||||||
|
|
||||||
var db = require(opts.filepath);
|
var db = require(opts.filepath);
|
||||||
var stat = require('fs').statSync(opts.filepath);
|
var stat = require('fs').statSync(opts.filepath);
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
|
@ -105,7 +109,7 @@ module.exports.create = function (opts) {
|
||||||
engine.zones = {
|
engine.zones = {
|
||||||
all: function (cb) {
|
all: function (cb) {
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
cb(null, db.zones.slice(0));
|
cb(null, db.zones.slice(0).filter(notDeleted));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
, get: function (queries, cb) {
|
, get: function (queries, cb) {
|
||||||
|
@ -116,7 +120,7 @@ module.exports.create = function (opts) {
|
||||||
}
|
}
|
||||||
var myDomains = db.zones.filter(function (d) {
|
var myDomains = db.zones.filter(function (d) {
|
||||||
return queries.some(function (q) {
|
return queries.some(function (q) {
|
||||||
return d.name.toLowerCase() === q.name;
|
return (d.name.toLowerCase() === q.name) && notDeleted(d);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
|
@ -191,7 +195,22 @@ module.exports.create = function (opts) {
|
||||||
engine.records = {
|
engine.records = {
|
||||||
all: function (cb) {
|
all: function (cb) {
|
||||||
process.nextTick(function () {
|
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) {
|
, get: function (query, cb) {
|
||||||
|
@ -204,8 +223,10 @@ module.exports.create = function (opts) {
|
||||||
// TODO use IN in masterquest (or implement OR)
|
// TODO use IN in masterquest (or implement OR)
|
||||||
// Only return single-level wildcard?
|
// Only return single-level wildcard?
|
||||||
if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) {
|
if (query.name === r.name || ('*.' + query.name.split('.').slice(1).join('.')) === r.name) {
|
||||||
|
if (notDeleted(r)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
cb(null, myRecords);
|
cb(null, myRecords);
|
||||||
|
@ -290,6 +311,22 @@ module.exports.create = function (opts) {
|
||||||
cb(err, record);
|
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;
|
return engine;
|
||||||
|
|
Loading…
Reference in New Issue