Skip to content

Commit a8ccb65

Browse files
authored
Merge pull request #394 from nextcloud/backport/392/stable28
[stable28] Improve testing
2 parents 81349cc + 565180d commit a8ccb65

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

lib/Service/ApprovalService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ public function request(int $fileId, int $ruleId, ?string $userId, bool $createS
426426
return ['error' => $this->l10n->t('You do not have access to this file')];
427427
}
428428

429+
if ($createShares && !$this->utilsService->userCanShareFile($fileId, $userId)) {
430+
return ['error' => $this->l10n->t('You can not share this file')];
431+
}
432+
429433
$rule = $this->ruleService->getRule($ruleId);
430434
if (is_null($rule)) {
431435
return ['error' => $this->l10n->t('Rule does not exist')];

lib/Service/UtilsService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ public function userHasAccessTo(int $fileId, ?string $userId): bool {
170170
return false;
171171
}
172172

173+
/**
174+
* Check if user can share a given file
175+
*
176+
* @param int $fileId
177+
* @param string|null $userId
178+
* @return bool
179+
*/
180+
public function userCanShareFile(int $fileId, ?string $userId): bool {
181+
$user = $this->userManager->get($userId);
182+
if ($user instanceof IUser) {
183+
$userFolder = $this->root->getUserFolder($userId);
184+
$node = $userFolder->getById($fileId);
185+
return sizeof($node) > 0 && $node[0]->isShareable();
186+
}
187+
return false;
188+
}
189+
173190
/**
174191
* @param string $name of the new tag
175192
* @return array

tests/unit/Service/ApprovalServiceTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
use OCP\Security\ICrypto;
4040
use OCP\Share\IManager as IShareManager;
4141

42+
use OCP\Share\IShare;
43+
4244
use OCP\SystemTag\ISystemTagManager;
4345
use OCP\SystemTag\ISystemTagObjectMapper;
4446

@@ -429,4 +431,41 @@ public function testApproval() {
429431
$stateForUser1 = $this->approvalService->getApprovalState($fileToReject->getId(), 'user1');
430432
$this->assertEquals(Application::STATE_REJECTED, $stateForUser1['state']);
431433
}
434+
435+
public function testRequestWithCreateSharesWhenUserCannotShareReturnsError(): void {
436+
// Share a file from user1 to user2 with read-only (no share permission).
437+
// user2 has access but cannot share -> request with createShares true must return error.
438+
$uf1 = $this->root->getUserFolder('user1');
439+
$file = $uf1->newFile('file_no_share.txt', 'content');
440+
$shared = $this->utilsService->createShare(
441+
$file,
442+
IShare::TYPE_USER,
443+
'user2',
444+
'user1',
445+
'label'
446+
);
447+
$this->assertTrue($shared);
448+
449+
// Add some tags
450+
$r = $this->utilsService->createTag('pending4');
451+
$idTagPending4 = $r['id'];
452+
$r = $this->utilsService->createTag('approved4');
453+
$idTagApproved4 = $r['id'];
454+
$r = $this->utilsService->createTag('rejected4');
455+
$idTagRejected4 = $r['id'];
456+
457+
$r = $this->ruleService->createRule(
458+
$idTagPending4, $idTagApproved4, $idTagRejected4,
459+
[['entityId' => 'user3', 'type' => 'user']],
460+
[['entityId' => 'user2', 'type' => 'user']],
461+
'user 2 request, 3 approves',
462+
false
463+
);
464+
$ruleId = $r['id'];
465+
466+
$result = $this->approvalService->request($file->getId(), $ruleId, 'user2', true);
467+
$this->assertArrayHasKey('error', $result);
468+
469+
$this->ruleService->deleteRule($ruleId);
470+
}
432471
}

0 commit comments

Comments
 (0)