diff --git a/backend/src/controllers/Domains.php b/backend/src/controllers/Domains.php index ccbe2cc..2d5fe0d 100644 --- a/backend/src/controllers/Domains.php +++ b/backend/src/controllers/Domains.php @@ -70,11 +70,33 @@ class Domains try { $result = $domains->addDomain($name, $type, $master); - $this->logger->debug('Domain was added', $result); + $this->logger->info('Created domain', $result); return $res->withJson($result, 201); } catch (\Exceptions\AlreadyExistentException $e) { $this->logger->debug('Zone with name ' . $name . ' already exists.'); return $res->withJson(['error' => 'Zone with name ' . $name . ' already exists.'], 409); } } + + public function delete(Request $req, Response $res, array $args) + { + $ac = new \Operations\AccessControl($this->c); + if (!$ac->isAdmin($req->getAttribute('userId'))) { + $this->logger->info('Non admin user tries to delete domain'); + return $res->withJson(['error' => 'You must be admin to use this feature'], 403); + } + + $domains = new \Operations\Domains($this->c); + + $domainId = intval($args['domainId']); + + try { + $domains->deleteDomain($domainId); + + $this->logger->info('Deleted domain', ['id' => $domainId]); + return $res->withStatus(204); + } catch (\Exceptions\NotFoundException $e) { + return $res->withJson(['error' => 'No domain found for id ' + $domainId], 404); + } + } } diff --git a/backend/src/exceptions/NotFoundException.php b/backend/src/exceptions/NotFoundException.php new file mode 100644 index 0000000..6a05fac --- /dev/null +++ b/backend/src/exceptions/NotFoundException.php @@ -0,0 +1,9 @@ +db->beginTransaction(); @@ -158,4 +160,31 @@ class Domains return $record; } + + /** + * Add new domain + * + * @param $id Id of the domain to delete + * + * @return void + * + * @throws NotFoundException if domain does not exist + */ + public function deleteDomain(int $id) : void + { + $this->db->beginTransaction(); + + $query = $this->db->prepare('SELECT id FROM domains WHERE id=:id'); + $query->bindValue(':id', $id, \PDO::PARAM_INT); + $query->execute(); + + if ($query->fetch() === false) { //Domain does not exist + $this->db->rollBack(); + throw new \Exceptions\NotFoundException(); + } + + $query = $this->db->prepare('DELETE FROM domains WHERE id=:id'); + $query->bindValue(':id', $id, \PDO::PARAM_INT); + $query->execute(); + } } diff --git a/backend/src/public/index.php b/backend/src/public/index.php index 6dbcec8..e5b824e 100644 --- a/backend/src/public/index.php +++ b/backend/src/public/index.php @@ -29,6 +29,7 @@ $app->group('/v1', function () { $this->get('/domains', '\Controllers\Domains:getList'); $this->post('/domains', '\Controllers\Domains:postNew'); + $this->delete('/domains/{domainId}', '\Controllers\Domains:delete'); })->add('\Middlewares\Authentication'); }); diff --git a/backend/test/tests/domains-crud.js b/backend/test/tests/domains-crud.js index e84aed3..70ee621 100644 --- a/backend/test/tests/domains-crud.js +++ b/backend/test/tests/domains-crud.js @@ -87,10 +87,26 @@ const cartesianProduct = require('cartesian-product'); type: 'SLAVE', master: '1.2.3.4' }, 'Creation result fail.') + + //Delete not existing domain + var res = await req({ + url: '/domains/100', + method: 'delete' + }); + + assert.equal(res.status, 404, 'Non existing domain deletion should be 404.'); + + //Delete existing domain + var res = await req({ + url: '/domains/1', + method: 'delete' + }); + + assert.equal(res.status, 204, 'Deletion of domain 1 should be successfull.'); }); await require('../testlib')('user', async function (assert, req) { - //Test insufficient privileges + //Test insufficient privileges for add var res = await req({ url: '/domains', method: 'post', @@ -100,6 +116,14 @@ const cartesianProduct = require('cartesian-product'); }); assert.equal(res.status, 403, 'Domain creation should be forbidden for users.') + + //Test insufficient privileges for delete + var res = await req({ + url: '/domains/1', + method: 'delete' + }); + + assert.equal(res.status, 403, 'Domain deletion should be forbidden for users.'); }); })(); \ No newline at end of file