2017-06-14 15:04:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Controller\Extension;
|
|
|
|
|
|
|
|
use Icinga\Exception\AuthenticationException;
|
|
|
|
use Icinga\Exception\NotFoundError;
|
2018-06-14 08:33:11 +02:00
|
|
|
use Icinga\Module\Director\Exception\JsonException;
|
2017-06-14 15:04:39 +02:00
|
|
|
use Icinga\Web\Response;
|
2018-06-14 08:33:11 +02:00
|
|
|
use InvalidArgumentException;
|
|
|
|
use Zend_Controller_Response_Exception;
|
2017-06-14 15:04:39 +02:00
|
|
|
|
|
|
|
trait RestApi
|
|
|
|
{
|
|
|
|
protected function isApified()
|
|
|
|
{
|
|
|
|
if (property_exists($this, 'isApified')) {
|
|
|
|
return $this->isApified;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-08-25 12:33:36 +02:00
|
|
|
protected function sendNotFoundForRestApi()
|
|
|
|
{
|
2018-06-14 08:33:11 +02:00
|
|
|
/** @var \Icinga\Web\Request $request */
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isApiRequest()) {
|
2017-08-25 12:33:36 +02:00
|
|
|
$this->sendJsonError($this->getResponse(), 'Not found', 404);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 09:25:03 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function sendNotFoundUnlessRestApi()
|
|
|
|
{
|
|
|
|
/** @var \Icinga\Web\Request $request */
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isApiRequest()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$this->sendJsonError($this->getResponse(), 'Not found', 404);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @throws AuthenticationException
|
|
|
|
*/
|
2017-07-06 15:09:18 +02:00
|
|
|
protected function assertApiPermission()
|
|
|
|
{
|
|
|
|
if (! $this->hasPermission('director/api')) {
|
|
|
|
throw new AuthenticationException('You are not allowed to access this API');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @throws AuthenticationException
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2017-06-14 15:04:39 +02:00
|
|
|
protected function checkForRestApiRequest()
|
|
|
|
{
|
2018-06-14 08:33:11 +02:00
|
|
|
/** @var \Icinga\Web\Request $request */
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isApiRequest()) {
|
2017-07-06 15:09:18 +02:00
|
|
|
$this->assertApiPermission();
|
2017-06-14 15:04:39 +02:00
|
|
|
if (! $this->isApified()) {
|
|
|
|
throw new NotFoundError('No such API endpoint found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @param Response $response
|
|
|
|
* @param $object
|
|
|
|
*/
|
2017-06-14 15:04:39 +02:00
|
|
|
protected function sendJson(Response $response, $object)
|
|
|
|
{
|
|
|
|
$response->setHeader('Content-Type', 'application/json', true);
|
2020-12-02 18:06:34 +01:00
|
|
|
echo json_encode($object, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
|
2017-06-14 15:04:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @param Response $response
|
|
|
|
* @param string $message
|
|
|
|
* @param int|null $code
|
|
|
|
*/
|
2017-06-14 15:04:39 +02:00
|
|
|
protected function sendJsonError(Response $response, $message, $code = null)
|
|
|
|
{
|
|
|
|
if ($code !== null) {
|
2018-06-14 08:33:11 +02:00
|
|
|
try {
|
|
|
|
$response->setHttpResponseCode((int) $code);
|
|
|
|
} catch (Zend_Controller_Response_Exception $e) {
|
|
|
|
throw new InvalidArgumentException($e->getMessage(), 0, $e);
|
|
|
|
}
|
2017-06-14 15:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->sendJson($response, (object) ['error' => $message]);
|
|
|
|
}
|
|
|
|
|
2018-06-14 08:33:11 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-14 15:04:39 +02:00
|
|
|
protected function getLastJsonError()
|
|
|
|
{
|
2018-06-14 08:33:11 +02:00
|
|
|
return JsonException::getJsonErrorMessage(json_last_error());
|
2017-06-14 15:04:39 +02:00
|
|
|
}
|
|
|
|
}
|