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
67 changes: 67 additions & 0 deletions lib/Events/ApprovalStateChangedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

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

namespace OCA\Approval\Events;

use OCP\EventDispatcher\Event;

/**
* Emitted when the approval state of a file changes:
* requested, approved or rejected.
*
* It is dispatched right before the corresponding system tag is
* (un)assigned, so listeners that react to tag assignment events
* (e.g. workflow engines) already have the context available.
*
* Next to the user who performed the action, it carries the ID of the
* user who requested the approval, so listeners can act on behalf of
* the requester and not only of the user who performed the latest action.
*/
class ApprovalStateChangedEvent extends Event {

public function __construct(
private int $fileId,
private int $ruleId,
private int $newState,
private ?string $actorUserId,
private ?string $requesterUserId,
) {
parent::__construct();
}

public function getFileId(): int {
return $this->fileId;
}

public function getRuleId(): int {
return $this->ruleId;
}

/**
* One of Application::STATE_PENDING, Application::STATE_APPROVED or Application::STATE_REJECTED
*/
public function getNewState(): int {
return $this->newState;
}

/**
* User who performed the action:
* the requester on request, the approver on approve/reject
*/
public function getActorUserId(): ?string {
return $this->actorUserId;
}

/**
* User who requested the approval, if known
*/
public function getRequesterUserId(): ?string {
return $this->requesterUserId;
}
}
29 changes: 29 additions & 0 deletions lib/Service/ApprovalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use DateTime;
use OCA\Approval\Activity\ActivityManager;
use OCA\Approval\AppInfo\Application;
use OCA\Approval\Events\ApprovalStateChangedEvent;
use OCA\Approval\Exceptions\OutdatedEtagException;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\FileInfo;
Expand Down Expand Up @@ -50,6 +52,7 @@ public function __construct(
private LoggerInterface $logger,
private ?string $userId,
private IUserMountCache $userMountCache,
private IEventDispatcher $eventDispatcher,
) {
}

Expand Down Expand Up @@ -381,8 +384,17 @@ public function approve(int $fileId, ?string $userId, string $etag, string $mess
try {
if ($this->tagObjectMapper->haveTag((string)$fileId, 'files', $rule['tagPending'])
&& $this->userIsAuthorizedByRule($userId, $rule, 'approvers')) {
// get the requester before storeAction() replaces the pending activity row
$pendingAction = $this->ruleService->getLastAction($fileId, $ruleId, Application::STATE_PENDING);

// store activity in our tables
$this->ruleService->storeAction($fileId, $ruleId, $userId, Application::STATE_APPROVED, $message);

// dispatch before the tags change so listeners reacting to the tag events have the context
$this->eventDispatcher->dispatchTyped(
new ApprovalStateChangedEvent($fileId, $ruleId, Application::STATE_APPROVED, $userId, $pendingAction['userId'] ?? null)
);

// Change tags
$this->tagObjectMapper->assignTags((string)$fileId, 'files', $rule['tagApproved']);
$this->tagObjectMapper->unassignTags((string)$fileId, 'files', $rule['tagPending']);
Expand Down Expand Up @@ -424,9 +436,17 @@ public function reject(int $fileId, ?string $userId, string $etag, string $messa
try {
if ($this->tagObjectMapper->haveTag((string)$fileId, 'files', $rule['tagPending'])
&& $this->userIsAuthorizedByRule($userId, $rule, 'approvers')) {
// get the requester before storeAction() replaces the pending activity row
$pendingAction = $this->ruleService->getLastAction($fileId, $ruleId, Application::STATE_PENDING);

// store activity in our tables
$this->ruleService->storeAction($fileId, $ruleId, $userId, Application::STATE_REJECTED, $message);

// dispatch before the tags change so listeners reacting to the tag events have the context
$this->eventDispatcher->dispatchTyped(
new ApprovalStateChangedEvent($fileId, $ruleId, Application::STATE_REJECTED, $userId, $pendingAction['userId'] ?? null)
);

// Change tags
$this->tagObjectMapper->assignTags((string)$fileId, 'files', $rule['tagRejected']);
$this->tagObjectMapper->unassignTags((string)$fileId, 'files', $rule['tagPending']);
Expand Down Expand Up @@ -485,6 +505,11 @@ public function request(int $fileId, int $ruleId, ?string $requesterUserId, bool
// store activity in our tables
$this->ruleService->storeAction($fileId, $ruleId, $requesterUserId, Application::STATE_PENDING);

// dispatch before the tag is assigned so listeners reacting to the tag event have the context
$this->eventDispatcher->dispatchTyped(
new ApprovalStateChangedEvent($fileId, $ruleId, Application::STATE_PENDING, $requesterUserId, $requesterUserId)
);

$this->tagObjectMapper->assignTags((string)$fileId, 'files', $rule['tagPending']);

// still produce an activity entry for the user who requests
Expand Down Expand Up @@ -530,6 +555,10 @@ public function requestViaTagAssignment(int $fileId, int $ruleId, string $reques
// store activity in our tables
$this->ruleService->storeAction($fileId, $ruleId, $requesterUserId, Application::STATE_PENDING);

$this->eventDispatcher->dispatchTyped(
new ApprovalStateChangedEvent($fileId, $ruleId, Application::STATE_PENDING, $requesterUserId, $requesterUserId)
);

// still produce an activity entry for the user who requests
$this->activityManager->triggerEvent(
ActivityManager::APPROVAL_OBJECT_NODE, $fileId,
Expand Down
39 changes: 38 additions & 1 deletion tests/unit/Service/ApprovalServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Approval\Activity\ActivityManager;
use OCA\Approval\AppInfo\Application;
use OCA\Approval\Events\ApprovalStateChangedEvent;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -97,7 +99,8 @@ protected function setUp(): void {
$c->get(IL10N::class),
$c->get(LoggerInterface::class),
'user1',
$c->get(IUserMountCache::class)
$c->get(IUserMountCache::class),
$c->get(IEventDispatcher::class)
);

// add some tags
Expand Down Expand Up @@ -427,4 +430,38 @@ public function testRequestWithCreateSharesWhenUserCannotShareReturnsError(): vo

$this->ruleService->deleteRule($ruleId);
}

public function testApprovalStateChangedEventIsEmitted(): void {
$app = new Application();
$eventDispatcher = $app->getContainer()->get(IEventDispatcher::class);

$emittedEvents = [];
$eventDispatcher->addListener(ApprovalStateChangedEvent::class, function (ApprovalStateChangedEvent $event) use (&$emittedEvents) {
$emittedEvents[] = $event;
});

// create a file
$uf1 = $this->root->getUserFolder('user1');
$file = $uf1->newFile('eventFile.txt', 'content');

// requesting approval emits an event carrying the requester
$this->approvalService->request($file->getId(), $this->idRule1, 'user1', false);
$this->assertCount(1, $emittedEvents);
$event = $emittedEvents[0];
$this->assertSame($file->getId(), $event->getFileId());
$this->assertEquals($this->idRule1, $event->getRuleId());
$this->assertSame(Application::STATE_PENDING, $event->getNewState());
$this->assertSame('user1', $event->getActorUserId());
$this->assertSame('user1', $event->getRequesterUserId());

// approving emits an event still carrying the requester
$this->approvalService->approve($file->getId(), 'user1', $file->getEtag());
$this->assertCount(2, $emittedEvents);
$event = $emittedEvents[1];
$this->assertSame($file->getId(), $event->getFileId());
$this->assertEquals($this->idRule1, $event->getRuleId());
$this->assertSame(Application::STATE_APPROVED, $event->getNewState());
$this->assertSame('user1', $event->getActorUserId());
$this->assertSame('user1', $event->getRequesterUserId());
}
}