Added PUT /domains/{domainId}
This commit is contained in:
parent
f4b06ae910
commit
827d4d8280
|
@ -122,4 +122,38 @@ class Domains
|
|||
return $res->withJson(['error' => 'No domain found for id ' . $domainId], 404);
|
||||
}
|
||||
}
|
||||
|
||||
public function put(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);
|
||||
}
|
||||
|
||||
$body = $req->getParsedBody();
|
||||
|
||||
if (!array_key_exists('master', $body)) {
|
||||
$this->logger->debug('One of the required fields is missing');
|
||||
return $res->withJson(['error' => 'One of the required fields is missing'], 422);
|
||||
}
|
||||
|
||||
$domainId = $args['domainId'];
|
||||
$master = $body['master'];
|
||||
|
||||
$domains = new \Operations\Domains($this->c);
|
||||
|
||||
try {
|
||||
$result = $domains->updateSlave($domainId, $master);
|
||||
|
||||
$this->logger->debug('Update master', ['id' => $domainId]);
|
||||
return $res->withStatus(204);
|
||||
} catch (\Exceptions\NotFoundException $e) {
|
||||
$this->logger->debug('Trying to update non existing slave zone', ['id' => $domainId]);
|
||||
return $res->withJson(['error' => 'No domain found for id ' . $domainId], 404);
|
||||
} catch (\Exceptions\SemanticException $e) {
|
||||
$this->logger->debug('Trying to update non slave zone', ['id' => $domainId]);
|
||||
return $res->withJson(['error' => 'Domain is not a slave zone'], 405);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
9
backend/src/exceptions/SemanticException.php
Normal file
9
backend/src/exceptions/SemanticException.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Exceptions;
|
||||
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
class SemanticException extends \Exception
|
||||
{
|
||||
}
|
|
@ -222,4 +222,50 @@ class Domains
|
|||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type of given domain
|
||||
*
|
||||
* @param int Domain id
|
||||
*
|
||||
* @return string Domain type
|
||||
*
|
||||
* @throws NotFoundException if domain does not exist
|
||||
*/
|
||||
public function getDomainType(int $id) : string
|
||||
{
|
||||
$query = $this->db->prepare('SELECT type FROM domains WHERE id=:id');
|
||||
$query->bindValue(':id', $id);
|
||||
$query->execute();
|
||||
$record = $query->fetch();
|
||||
|
||||
if ($record === false) {
|
||||
throw new \Exceptions\NotFoundException();
|
||||
}
|
||||
|
||||
return $record['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update master for slave zone
|
||||
*
|
||||
* @param int Domain id
|
||||
* @param string New master
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws NotFoundException if domain does not exist
|
||||
* @throws SemanticException if domain is no slave zone
|
||||
*/
|
||||
public function updateSlave(int $id, string $master)
|
||||
{
|
||||
if ($this->getDomainType($id) !== 'SLAVE') {
|
||||
throw new \Exceptions\SemanticException();
|
||||
}
|
||||
|
||||
$query = $this->db->prepare('UPDATE domains SET master=:master WHERE id=:id');
|
||||
$query->bindValue(':id', $id);
|
||||
$query->bindValue(':master', $master);
|
||||
$query->execute();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ $app->group('/v1', function () {
|
|||
$this->post('/domains', '\Controllers\Domains:postNew');
|
||||
$this->delete('/domains/{domainId}', '\Controllers\Domains:delete');
|
||||
$this->get('/domains/{domainId}', '\Controllers\Domains:getSingle');
|
||||
$this->put('/domains/{domainId}', '\Controllers\Domains:put');
|
||||
})->add('\Middlewares\Authentication');
|
||||
});
|
||||
|
||||
|
|
|
@ -131,6 +131,59 @@ test.run(async function () {
|
|||
master: '1.2.3.4'
|
||||
}, 'Slave domain data mismatch');
|
||||
|
||||
//Update slave domain
|
||||
var res = await req({
|
||||
url: '/domains/8',
|
||||
method: 'put',
|
||||
data: {
|
||||
master: '9.8.7.6'
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(res.status, 204, 'Slave update should return no content');
|
||||
|
||||
//Check if update succeded
|
||||
var res = await req({
|
||||
url: '/domains/8',
|
||||
method: 'get'
|
||||
});
|
||||
|
||||
assert.equal(res.status, 200, 'Slave domain should be accessible after update.');
|
||||
assert.equal(res.data.master, '9.8.7.6', 'Slave update had no effect');
|
||||
|
||||
//Check if update fails for non existing domain
|
||||
var res = await req({
|
||||
url: '/domains/100',
|
||||
method: 'put',
|
||||
data: {
|
||||
master: '9.8.7.6'
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(res.status, 404, 'Update on not existing domain should fail.');
|
||||
|
||||
//Check if update fails for master zone
|
||||
var res = await req({
|
||||
url: '/domains/1',
|
||||
method: 'put',
|
||||
data: {
|
||||
master: '9.8.7.6'
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(res.status, 405, 'Update on master zone should fail.');
|
||||
|
||||
//Check if update fails for missing field
|
||||
var res = await req({
|
||||
url: '/domains/100',
|
||||
method: 'put',
|
||||
data: {
|
||||
foo: 'bar'
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(res.status, 422, 'Update with missing master field should fail.');
|
||||
|
||||
//Delete not existing domain
|
||||
var res = await req({
|
||||
url: '/domains/100',
|
||||
|
@ -168,6 +221,17 @@ test.run(async function () {
|
|||
|
||||
assert.equal(res.status, 403, 'Domain deletion should be forbidden for users.');
|
||||
|
||||
//Test insufficient permissions
|
||||
var res = await req({
|
||||
url: '/domains/2',
|
||||
method: 'put',
|
||||
data: {
|
||||
master: '9.8.7.6'
|
||||
}
|
||||
});
|
||||
|
||||
assert.equal(res.status, 403, 'Update of slave zone should be forbidden for non admins.');
|
||||
|
||||
//Test insufficient privileges for get
|
||||
var res = await req({
|
||||
url: '/domains/3',
|
||||
|
|
Loading…
Reference in a new issue