fix(webhooks): Fix userIdFiltering for webhooks calls

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/46076/head
Côme Chilliet 5 months ago
parent 44d707c97b
commit 8abf62715c
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
  1. 15
      apps/webhook_listeners/lib/Db/WebhookListenerMapper.php
  2. 2
      apps/webhook_listeners/lib/Listener/WebhooksEventListener.php
  3. 45
      apps/webhook_listeners/tests/Db/WebhookListenerMapperTest.php

@ -168,7 +168,10 @@ class WebhookListenerMapper extends QBMapper {
$qb->selectDistinct('event')
->from($this->getTableName())
->where($qb->expr()->in('user_id_filter', $qb->createNamedParameter(['',$userId], IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR));
->where($qb->expr()->in(
'user_id_filter',
$qb->createNamedParameter(array_unique(['',$userId]), IQueryBuilder::PARAM_STR_ARRAY),
));
$result = $qb->executeQuery();
@ -201,12 +204,18 @@ class WebhookListenerMapper extends QBMapper {
/**
* @throws Exception
*/
public function getByEvent(string $event): array {
public function getByEvent(string $event, ?string $userId = null): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('event', $qb->createNamedParameter($event, IQueryBuilder::PARAM_STR)));
->where($qb->expr()->eq('event', $qb->createNamedParameter($event, IQueryBuilder::PARAM_STR)))
->andWhere(
$qb->expr()->in(
'user_id_filter',
$qb->createNamedParameter(array_unique(['',$userId ?? '']), IQueryBuilder::PARAM_STR_ARRAY),
)
);
return $this->findEntities($qb);
}

@ -34,8 +34,8 @@ class WebhooksEventListener implements IEventListener {
}
public function handle(Event $event): void {
$webhookListeners = $this->mapper->getByEvent($event::class);
$user = $this->userSession->getUser();
$webhookListeners = $this->mapper->getByEvent($event::class, $user?->getUID());
foreach ($webhookListeners as $webhookListener) {
// TODO add group membership to be able to filter on it

@ -124,4 +124,49 @@ class WebhookListenerMapperTest extends TestCase {
$listener1->resetUpdatedFields();
$this->assertEquals($listener1, $listener2);
}
public function testInsertListenerAndGetItByEventAndUser() {
$listener1 = $this->mapper->addWebhookListener(
null,
'bob',
'POST',
'https://webhook.example.com/endpoint',
NodeWrittenEvent::class,
null,
'alice',
null,
AuthMethod::None,
null,
);
$listener1->resetUpdatedFields();
$this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice'));
$this->assertEquals([], $this->mapper->getAllConfiguredEvents(''));
$this->assertEquals([], $this->mapper->getAllConfiguredEvents('otherUser'));
$this->assertEquals([$listener1], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice'));
$this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, ''));
$this->assertEquals([], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser'));
/* Add a second listener with no user filter */
$listener2 = $this->mapper->addWebhookListener(
null,
'bob',
'POST',
'https://webhook.example.com/endpoint',
NodeWrittenEvent::class,
null,
'',
null,
AuthMethod::None,
null,
);
$listener2->resetUpdatedFields();
$this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents('alice'));
$this->assertEquals([NodeWrittenEvent::class], $this->mapper->getAllConfiguredEvents(''));
$this->assertEquals([$listener1, $listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'alice'));
$this->assertEquals([$listener2], $this->mapper->getByEvent(NodeWrittenEvent::class, 'otherUser'));
}
}

Loading…
Cancel
Save