Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions application/controllers/ApiV1ChannelsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Controllers;

use Exception;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Util\Environment;
use Icinga\Util\Json;
use ipl\Sql\Compat\FilterProcessor;
use ipl\Sql\Select;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use Ramsey\Uuid\Uuid;
use stdClass;

class ApiV1ChannelsController extends CompatController
{
public function indexAction(): void
{
$this->assertPermission('notifications/api/v1');

$request = $this->getRequest();
if (! $request->isApiRequest()) {
$this->httpBadRequest('No API request');
}

$method = $request->getMethod();
if ($method !== 'GET') {
$this->httpBadRequest('Only GET method supported');
}

$db = Database::get();

/** @var ?string $identifier */
$identifier = $request->getParam('identifier');

if ($identifier && ! Uuid::isValid($identifier)) {
$this->httpBadRequest('The given identifier is not a valid UUID');
}

try {
$filterRule = QueryString::fromString(Url::fromRequest()->getQueryString())
->on(
QueryString::ON_CONDITION,
function (Filter\Condition $condition) {
$column = $condition->getColumn();
if (! in_array($column, ['id', 'name', 'type'])) {
$this->httpBadRequest(sprintf(
'Invalid filter column %s given, only id, name and type are allowed',
$column
));
}

if ($column === 'id') {
if (! Uuid::isValid($condition->getValue())) {

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.

Check failure on line 59 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.4 on ubuntu-latest

Parameter #1 $uuid of static method Ramsey\Uuid\Uuid::isValid() expects string, mixed given.
$this->httpBadRequest('The given filter id is not a valid UUID');
}

$condition->setColumn('external_uuid');
}
}
)->parse();

$filter = FilterProcessor::assembleFilter($filterRule);
} catch (Exception $e) {
$this->httpBadRequest($e->getMessage());
}

$stmt = (new Select())
->distinct()
->from('channel ch')
->columns([
'channel_id' => 'ch.id',
'id' => 'ch.external_uuid',
'name',
'type',
'config'
]);

if ($identifier !== null) {
$stmt->where(['external_uuid = ?' => $identifier]);

/** @var stdClass|false $result */
$result = $db->fetchOne($stmt);

if ($result === false) {
$this->httpNotFound('Channel not found');
}

unset($result->channel_id);

$this->getResponse()
->setHttpResponseCode(200)
->json()
->setSuccessData((array) $result)
->sendResponse();
} else {
if ($filter !== null) {
$stmt->where($filter);

Check failure on line 103 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $condition of method ipl\Sql\Select::where() expects array|ipl\Sql\ExpressionInterface|ipl\Sql\Select|string, mixed given.

Check failure on line 103 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $condition of method ipl\Sql\Select::where() expects array|ipl\Sql\ExpressionInterface|ipl\Sql\Select|string, mixed given.

Check failure on line 103 in application/controllers/ApiV1ChannelsController.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.4 on ubuntu-latest

Parameter #1 $condition of method ipl\Sql\Select::where() expects array|ipl\Sql\ExpressionInterface|ipl\Sql\Select|string, mixed given.
}

$stmt->limit(500);
$offset = 0;

ob_end_clean();
Environment::raiseExecutionTime();

$this->getResponse()
->setHeader('Content-Type', 'application/json')
->setHeader('Cache-Control', 'no-store')
->sendResponse();

echo '[';

$res = $db->select($stmt->offset($offset));
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
if ($i > 0 || $offset !== 0) {
echo ",\n";
}

unset($row->channel_id);

echo Json::sanitize($row);
}

$offset += 500;
$res = $db->select($stmt->offset($offset));
} while ($res->rowCount());

echo ']';
}

exit;
}
}
Loading
Loading