feat: allow to use webhook_listeners without user context

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
This commit is contained in:
Alexander Piskun 2024-07-18 18:12:00 +03:00 committed by Alexander Piskun
parent 9cd2e5bed9
commit 14e07a337c
No known key found for this signature in database
GPG key ID: 1F0BF0EC3CF22721
7 changed files with 51 additions and 11 deletions

View file

@ -9,7 +9,7 @@
<name>Nextcloud webhook support</name>
<summary>Nextcloud webhook support</summary>
<description>Nextcloud webhook support</description>
<version>1.0.0-dev</version>
<version>1.1.0-dev</version>
<licence>agpl</licence>
<author>Côme Chilliet</author>
<namespace>WebhookListeners</namespace>

View file

@ -16,6 +16,7 @@ return array(
'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\\Version1001Date20240716184935' => $baseDir . '/../lib/Migration/Version1001Date20240716184935.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',

View file

@ -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\\Version1001Date20240716184935' => __DIR__ . '/..' . '/../lib/Migration/Version1001Date20240716184935.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',

View file

@ -17,6 +17,7 @@ use OCA\WebhookListeners\Settings\Admin;
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;
@ -56,6 +57,7 @@ class WebhooksController extends OCSController {
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function index(?string $uri = null): DataResponse {
try {
if ($uri !== null) {
@ -89,6 +91,7 @@ class WebhooksController extends OCSController {
*/
#[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());
@ -122,6 +125,7 @@ class WebhooksController extends OCSController {
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function create(
string $httpMethod,
string $uri,
@ -143,8 +147,6 @@ class WebhooksController extends OCSController {
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,
@ -191,6 +193,7 @@ class WebhooksController extends OCSController {
*/
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/{id}')]
#[AuthorizedAdminSetting(settings:Admin::class)]
#[AppApiAdminAccessWithoutUser]
public function update(
int $id,
string $httpMethod,
@ -213,8 +216,6 @@ class WebhooksController extends OCSController {
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,
@ -254,6 +255,7 @@ class WebhooksController extends OCSController {
*/
#[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);

View file

@ -13,9 +13,9 @@ use OCP\AppFramework\Db\Entity;
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()
@ -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

View file

@ -72,7 +72,7 @@ class WebhookListenerMapper extends QBMapper {
*/
public function addWebhookListener(
?string $appId,
string $userId,
?string $userId,
string $httpMethod,
string $uri,
string $event,
@ -112,7 +112,7 @@ class WebhookListenerMapper extends QBMapper {
public function updateWebhookListener(
int $id,
?string $appId,
string $userId,
?string $userId,
string $httpMethod,
string $uri,
string $event,

View file

@ -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 Version1001Date20240716184935 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;
}
}