SelfService: provide ticket

refs #486
This commit is contained in:
Thomas Gelf 2017-07-06 20:29:35 +02:00
parent 7a1dbd5334
commit 427cb2c050
1 changed files with 34 additions and 0 deletions

View File

@ -9,6 +9,7 @@ use Icinga\Module\Director\Forms\IcingaHostSelfServiceForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Settings;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Controller\ActionController;
use ipl\Html\Html;
@ -86,6 +87,39 @@ class SelfServiceController extends ActionController
->add($form);
}
public function ticketAction()
{
if (!$this->getRequest()->isApiRequest()) {
throw new NotFoundError('Not found');
}
try {
$key = $this->params->getRequired('key');
$host = IcingaHost::loadWithApiKey($key, $this->db());
if ($host->isTemplate()) {
throw new NotFoundError('Got invalid API key "%s"', $key);
}
$name = $host->getObjectName();
if ($host->getResolvedProperty('has_agent') !== 'y') {
throw new NotFoundError('The host "%s" is not an agent', $name);
}
$this->sendPowerShellResponse(
Util::getIcingaTicket(
$name,
$this->api()->getTicketSalt()
)
);
} catch (Exception $e) {
if ($e instanceof NotFoundError) {
$this->sendPowerShellError($e->getMessage(), 404);
} else {
$this->sendPowerShellError($e->getMessage(), 500);
}
}
}
protected function sendPowerShellResponse($response)
{
if ($this->getRequest()->getHeader('X-Director-Accept') === 'text/plain') {