Skip to content

Commit

Permalink
feat: allow to use webhook_listeners without user context
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Jul 18, 2024
1 parent 9cd2e5b commit e15acd6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => $baseDir . '/../lib/Db/WebhookListenerMapper.php',
'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => $baseDir . '/../lib/Listener/WebhooksEventListener.php',
'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => $baseDir . '/../lib/Migration/Version1000Date20240527153425.php',
'OCA\\WebhookListeners\\Migration\\Version1000Date20240716184935' => $baseDir . '/../lib/Migration/Version1000Date20240716184935.php',
'OCA\\WebhookListeners\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => $baseDir . '/../lib/Service/PHPMongoQuery.php',
'OCA\\WebhookListeners\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ComposerStaticInitWebhookListeners
'OCA\\WebhookListeners\\Db\\WebhookListenerMapper' => __DIR__ . '/..' . '/../lib/Db/WebhookListenerMapper.php',
'OCA\\WebhookListeners\\Listener\\WebhooksEventListener' => __DIR__ . '/..' . '/../lib/Listener/WebhooksEventListener.php',
'OCA\\WebhookListeners\\Migration\\Version1000Date20240527153425' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20240527153425.php',
'OCA\\WebhookListeners\\Migration\\Version1000Date20240716184935' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20240716184935.php',
'OCA\\WebhookListeners\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php',
'OCA\\WebhookListeners\\Service\\PHPMongoQuery' => __DIR__ . '/..' . '/../lib/Service/PHPMongoQuery.php',
'OCA\\WebhookListeners\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
Expand Down
10 changes: 6 additions & 4 deletions apps/webhook_listeners/lib/Controller/WebhooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\AppApiAdminAccessWithoutUser;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\DataResponse;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function __construct(
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function index(?string $uri = null): DataResponse {
try {
if ($uri !== null) {
Expand Down Expand Up @@ -89,6 +91,7 @@ public function index(?string $uri = null): DataResponse {
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function show(int $id): DataResponse {
try {
return new DataResponse($this->mapper->getById($id)->jsonSerialize());
Expand Down Expand Up @@ -122,6 +125,7 @@ public function show(int $id): DataResponse {
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function create(
string $httpMethod,
string $uri,
Expand All @@ -143,8 +147,6 @@ public function create(
throw new OCSBadRequestException('This auth method does not exist');
}
try {
/* We can never reach here without a user in session */
assert(is_string($this->userId));
$webhookListener = $this->mapper->addWebhookListener(
$appId,
$this->userId,
Expand Down Expand Up @@ -191,6 +193,7 @@ public function create(
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function update(
int $id,
string $httpMethod,
Expand All @@ -213,8 +216,6 @@ public function update(
throw new OCSBadRequestException('This auth method does not exist');
}
try {
/* We can never reach here without a user in session */
assert(is_string($this->userId));
$webhookListener = $this->mapper->updateWebhookListener(
$id,
$appId,
Expand Down Expand Up @@ -254,6 +255,7 @@ public function update(
*/
#[ApiRoute(verb: 'DELETE', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function destroy(int $id): DataResponse {
try {
$deleted = $this->mapper->deleteById($id);
Expand Down
8 changes: 4 additions & 4 deletions apps/webhook_listeners/lib/Db/WebhookListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use OCP\Security\ICrypto;

/**
* @method void setUserId(string $userId)
* @method void setUserId(?string $userId)
* @method ?string getAppId()
* @method string getUserId()
* @method ?string getUserId()
* @method string getHttpMethod()
* @method string getUri()
* @method ?array getHeaders()
Expand All @@ -31,10 +31,10 @@ class WebhookListener extends Entity implements \JsonSerializable {
protected $appId = null;

/**
* @var string id of the user who added the webhook listener
* @var ?string id of the user who added the webhook listener
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $userId;
protected $userId = null;

/**
* @var string
Expand Down
4 changes: 2 additions & 2 deletions apps/webhook_listeners/lib/Db/WebhookListenerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getAll(): array {
*/
public function addWebhookListener(
?string $appId,
string $userId,
?string $userId,
string $httpMethod,
string $uri,
string $event,
Expand Down Expand Up @@ -112,7 +112,7 @@ public function addWebhookListener(
public function updateWebhookListener(
int $id,
?string $appId,
string $userId,
?string $userId,
string $httpMethod,
string $uri,
string $event,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\WebhookListeners\Migration;

use Closure;
use OCA\WebhookListeners\Db\WebhookListenerMapper;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version1000Date20240716184935 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

if ($schema->hasTable(WebhookListenerMapper::TABLE_NAME)) {
$table = $schema->getTable(WebhookListenerMapper::TABLE_NAME);
$table->getColumn('user_id')->setNotnull(false)->setDefault(null);
return $schema;
}
return null;
}
}

0 comments on commit e15acd6

Please sign in to comment.