Skip to content

Commit b01719d

Browse files
authored
Merge pull request #403 from nextcloud/change-api-signature
Change api signature to require etag
2 parents 52e12a3 + 6a57ab1 commit b01719d

3 files changed

Lines changed: 14 additions & 18 deletions

File tree

lib/Controller/ApprovalController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public function getPendingNodes(?int $since = null): DataResponse {
7878
* Approve a file
7979
*
8080
* @param int $fileId
81+
* @param string $etag
8182
* @param string|null $message
82-
* @param string|null $etag
8383
* @return DataResponse
8484
*/
8585
#[NoAdminRequired]
86-
public function approve(int $fileId, ?string $message = '', ?string $etag = ''): DataResponse {
86+
public function approve(int $fileId, string $etag, ?string $message = ''): DataResponse {
8787
try {
8888
if ($this->approvalService->approve($fileId, $this->userId, $message, $etag)) {
8989
return new DataResponse([]);
@@ -98,12 +98,12 @@ public function approve(int $fileId, ?string $message = '', ?string $etag = ''):
9898
* Reject a file
9999
*
100100
* @param int $fileId
101+
* @param string $etag
101102
* @param string|null $message
102-
* @param string|null $etag
103103
* @return DataResponse
104104
*/
105105
#[NoAdminRequired]
106-
public function reject(int $fileId, ?string $message = '', ?string $etag = ''): DataResponse {
106+
public function reject(int $fileId, string $etag, ?string $message = ''): DataResponse {
107107
try {
108108
if ($this->approvalService->reject($fileId, $this->userId, $message, $etag)) {
109109
return new DataResponse([]);

lib/Service/ApprovalService.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ public function getApprovalState(int $fileId, ?string $userId, bool $userHasAcce
353353
*
354354
* @param int $fileId
355355
* @param string|null $userId
356+
* @param string $etag
356357
* @param string $message
357-
* @param string $etag optional etag of the file to check if it has changed since approval was requested
358358
* @return bool success
359359
* @throws OutdatedEtagException
360360
*/
361-
public function approve(int $fileId, ?string $userId, string $message = '', string $etag = ''): bool {
361+
public function approve(int $fileId, ?string $userId, string $etag, string $message = ''): bool {
362362
$fileState = $this->getApprovalState($fileId, $userId);
363-
if ($etag !== '' && $etag !== $this->getEtag($fileId)) {
363+
if ($etag !== $this->getEtag($fileId)) {
364364
throw new OutdatedEtagException();
365365
}
366366
// if file has pending tag and user is authorized to approve it
@@ -396,14 +396,14 @@ public function approve(int $fileId, ?string $userId, string $message = '', stri
396396
*
397397
* @param int $fileId
398398
* @param string|null $userId
399+
* @param string $etag
399400
* @param string $message
400-
* @param string $etag optional etag of the file to check if it has changed since approval was requested
401401
* @return bool success
402402
* @throws OutdatedEtagException
403403
*/
404-
public function reject(int $fileId, ?string $userId, string $message = '', string $etag = ''): bool {
404+
public function reject(int $fileId, ?string $userId, string $etag, string $message = ''): bool {
405405
$fileState = $this->getApprovalState($fileId, $userId);
406-
if ($etag !== '' && $etag !== $this->getEtag($fileId)) {
406+
if ($etag !== $this->getEtag($fileId)) {
407407
throw new OutdatedEtagException();
408408
}
409409
// if file has pending tag and user is authorized to approve it

tests/unit/Service/ApprovalServiceTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020
use OCP\IL10N;
2121
use OCP\IUserManager;
2222
use OCP\Notification\IManager as INotificationManager;
23-
2423
use OCP\Share\IManager as IShareManager;
25-
2624
use OCP\Share\IShare;
27-
2825
use OCP\SystemTag\ISystemTagManager;
2926
use OCP\SystemTag\ISystemTagObjectMapper;
30-
3127
use Psr\Log\LoggerInterface;
3228

3329
class ApprovalServiceTest extends TestCase {
@@ -365,24 +361,24 @@ public function testApproval() {
365361
// approve failures
366362
// tag does not exist
367363
$this->ruleService->saveRule($idRule3, $idTagPending3, -1, $idTagRejected3, $approvers, $requesters, $description, $unapproveWhenModified);
368-
$result = $this->approvalService->approve($fileToReject->getId(), 'user1');
364+
$result = $this->approvalService->approve($fileToReject->getId(), 'user1', $fileToReject->getEtag());
369365
$this->assertFalse($result);
370366
$this->ruleService->saveRule($idRule3, $idTagPending3, $idTagApproved3, $idTagRejected3, $approvers, $requesters, $description, $unapproveWhenModified);
371367

372368
// approve
373-
$this->approvalService->approve($fileToApprove->getId(), 'user1');
369+
$this->approvalService->approve($fileToApprove->getId(), 'user1', $fileToApprove->getEtag());
374370
$stateForUser1 = $this->approvalService->getApprovalState($fileToApprove->getId(), 'user1');
375371
$this->assertEquals(Application::STATE_APPROVED, $stateForUser1['state']);
376372

377373
// reject failures
378374
// tag does not exist
379375
$this->ruleService->saveRule($idRule3, $idTagPending3, $idTagApproved3, -1, $approvers, $requesters, $description, $unapproveWhenModified);
380-
$result = $this->approvalService->reject($fileToReject->getId(), 'user1');
376+
$result = $this->approvalService->reject($fileToReject->getId(), 'user1', $fileToReject->getEtag());
381377
$this->assertFalse($result);
382378
$this->ruleService->saveRule($idRule3, $idTagPending3, $idTagApproved3, $idTagRejected3, $approvers, $requesters, $description, $unapproveWhenModified);
383379

384380
// reject
385-
$this->approvalService->reject($fileToReject->getId(), 'user1');
381+
$this->approvalService->reject($fileToReject->getId(), 'user1', $fileToReject->getEtag());
386382
$stateForUser1 = $this->approvalService->getApprovalState($fileToReject->getId(), 'user1');
387383
$this->assertEquals(Application::STATE_REJECTED, $stateForUser1['state']);
388384
}

0 commit comments

Comments
 (0)