Added DELETE /domains/{domainId}
This commit is contained in:
parent
8d24d6e6e1
commit
29aa6e87f8
|
@ -70,11 +70,33 @@ class Domains
|
||||||
try {
|
try {
|
||||||
$result = $domains->addDomain($name, $type, $master);
|
$result = $domains->addDomain($name, $type, $master);
|
||||||
|
|
||||||
$this->logger->debug('Domain was added', $result);
|
$this->logger->info('Created domain', $result);
|
||||||
return $res->withJson($result, 201);
|
return $res->withJson($result, 201);
|
||||||
} catch (\Exceptions\AlreadyExistentException $e) {
|
} catch (\Exceptions\AlreadyExistentException $e) {
|
||||||
$this->logger->debug('Zone with name ' . $name . ' already exists.');
|
$this->logger->debug('Zone with name ' . $name . ' already exists.');
|
||||||
return $res->withJson(['error' => 'Zone with name ' . $name . ' already exists.'], 409);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
9
backend/src/exceptions/NotFoundException.php
Normal file
9
backend/src/exceptions/NotFoundException.php
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Exceptions;
|
||||||
|
|
||||||
|
require '../vendor/autoload.php';
|
||||||
|
|
||||||
|
class NotFoundException extends \Exception
|
||||||
|
{
|
||||||
|
}
|
|
@ -110,15 +110,17 @@ class Domains
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of domains according to filter criteria
|
* Add new domain
|
||||||
*
|
*
|
||||||
* @param $name Name of the new zone
|
* @param $name Name of the new zone
|
||||||
* @param $type Type of the new zone
|
* @param $type Type of the new zone
|
||||||
* @param $master Master for slave zones, otherwise null
|
* @param $master Master for slave zones, otherwise null
|
||||||
*
|
*
|
||||||
* @return array New domain entry
|
* @return array New domain entry
|
||||||
|
*
|
||||||
|
* @throws AlreadyExistenException it the domain exists already
|
||||||
*/
|
*/
|
||||||
public function addDomain(string $name, string $type, ? string $master)
|
public function addDomain(string $name, string $type, ? string $master) : array
|
||||||
{
|
{
|
||||||
$this->db->beginTransaction();
|
$this->db->beginTransaction();
|
||||||
|
|
||||||
|
@ -158,4 +160,31 @@ class Domains
|
||||||
|
|
||||||
return $record;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ $app->group('/v1', function () {
|
||||||
|
|
||||||
$this->get('/domains', '\Controllers\Domains:getList');
|
$this->get('/domains', '\Controllers\Domains:getList');
|
||||||
$this->post('/domains', '\Controllers\Domains:postNew');
|
$this->post('/domains', '\Controllers\Domains:postNew');
|
||||||
|
$this->delete('/domains/{domainId}', '\Controllers\Domains:delete');
|
||||||
})->add('\Middlewares\Authentication');
|
})->add('\Middlewares\Authentication');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -87,10 +87,26 @@ const cartesianProduct = require('cartesian-product');
|
||||||
type: 'SLAVE',
|
type: 'SLAVE',
|
||||||
master: '1.2.3.4'
|
master: '1.2.3.4'
|
||||||
}, 'Creation result fail.')
|
}, '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) {
|
await require('../testlib')('user', async function (assert, req) {
|
||||||
//Test insufficient privileges
|
//Test insufficient privileges for add
|
||||||
var res = await req({
|
var res = await req({
|
||||||
url: '/domains',
|
url: '/domains',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
|
@ -100,6 +116,14 @@ const cartesianProduct = require('cartesian-product');
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(res.status, 403, 'Domain creation should be forbidden for users.')
|
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.');
|
||||||
});
|
});
|
||||||
|
|
||||||
})();
|
})();
|
Loading…
Reference in a new issue