feat: Add app_api app id to saved information about webhook

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2024-06-03 16:10:55 +02:00 committed by Côme Chilliet
parent 75b2ed4c79
commit 261f08e631
6 changed files with 50 additions and 3 deletions

View file

@ -20,6 +20,7 @@ use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\ISession;
use Psr\Log\LoggerInterface;
/**
@ -33,6 +34,7 @@ class WebhooksController extends OCSController {
private LoggerInterface $logger,
private WebhookListenerMapper $mapper,
private ?string $userId,
private ISession $session,
) {
parent::__construct($appName, $request);
}
@ -97,8 +99,13 @@ class WebhooksController extends OCSController {
?string $authMethod,
?array $authData,
): DataResponse {
$appId = null;
if ($this->session->get('app_api') === true) {
$appId = $this->request->getHeader('EX-APP-ID');
}
try {
$webhookListener = $this->mapper->addWebhookListener(
$appId,
$this->userId,
$httpMethod,
$uri,
@ -151,9 +158,14 @@ class WebhooksController extends OCSController {
?string $authMethod,
?array $authData,
): DataResponse {
$appId = null;
if ($this->session->get('app_api') === true) {
$appId = $this->request->getHeader('EX-APP-ID');
}
try {
$webhookListener = $this->mapper->updateWebhookListener(
$id,
$appId,
$this->userId,
$httpMethod,
$uri,

View file

@ -16,7 +16,10 @@ use OCP\AppFramework\Db\Entity;
* @method string getUserId()
*/
class WebhookListener extends Entity implements \JsonSerializable {
/** @var string id of the user who added the webhook listener */
/** @var ?string id of the app_api application who added the webhook listener */
protected $appId;
/** @var string id of the user who added the webhook listener */
protected $userId;
/** @var string */
@ -41,6 +44,7 @@ class WebhookListener extends Entity implements \JsonSerializable {
protected $authData;
public function __construct() {
$this->addType('appId', 'string');
$this->addType('userId', 'string');
$this->addType('httpMethod', 'string');
$this->addType('uri', 'string');

View file

@ -57,6 +57,7 @@ class WebhookListenerMapper extends QBMapper {
}
public function addWebhookListener(
?string $appId,
string $userId,
string $httpMethod,
string $uri,
@ -68,6 +69,7 @@ class WebhookListenerMapper extends QBMapper {
) {
$webhookListener = WebhookListener::fromParams(
[
'appId' => $appId,
'userId' => $userId,
'httpMethod' => $httpMethod,
'uri' => $uri,
@ -83,6 +85,7 @@ class WebhookListenerMapper extends QBMapper {
public function updateWebhookListener(
int $id,
?string $appId,
string $userId,
string $httpMethod,
string $uri,
@ -95,6 +98,7 @@ class WebhookListenerMapper extends QBMapper {
$webhookListener = WebhookListener::fromParams(
[
'id' => $id,
'appId' => $appId,
'userId' => $userId,
'httpMethod' => $httpMethod,
'uri' => $uri,

View file

@ -31,6 +31,10 @@ class Version1000Date20240527153425 extends SimpleMigrationStep {
'notnull' => true,
'length' => 4,
]);
$table->addColumn('app_id', Types::STRING, [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('user_id', Types::STRING, [
'notnull' => true,
'length' => 64,

View file

@ -26,7 +26,8 @@
"id",
"userId",
"httpMethod",
"uri"
"uri",
"authMethod"
],
"properties": {
"id": {
@ -43,6 +44,27 @@
},
"event": {
"type": "string"
},
"eventFilter": {
"type": "array",
"items": {
"type": "object"
}
},
"headers": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"authMethod": {
"type": "string"
},
"authData": {
"type": "object",
"additionalProperties": {
"type": "object"
}
}
}
},
@ -709,4 +731,4 @@
}
},
"tags": []
}
}

View file

@ -44,6 +44,7 @@ class WebhookListenerMapperTest extends TestCase {
public function testInsertListenerAndGetIt() {
$listener1 = $this->mapper->addWebhookListener(
null,
'bob',
'POST',
'https://webhook.example.com/endpoint',