Skip to content

Commit a828838

Browse files
committed
fix: always return the owner of the file to be signed
To don't throw exceptions at the app file_version, we need to always return the owner of the file .signed.pdf. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent c367e3a commit a828838

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

lib/Service/SignFileService.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Exception;
1414
use InvalidArgumentException;
1515
use OC\AppFramework\Http as AppFrameworkHttp;
16+
use OC\User\NoUserException;
1617
use OCA\Libresign\AppInfo\Application;
1718
use OCA\Libresign\DataObjects\VisibleElementAssoc;
1819
use OCA\Libresign\Db\AccountFile;
@@ -674,15 +675,10 @@ public function getSignRequestToSign(FileEntity $libresignFile, ?string $signReq
674675
return $signRequest;
675676
}
676677

677-
private function getPdfToSign(File $originalFile): File {
678-
if ($this->libreSignFile->getSignedNodeId()) {
679-
$nodeId = $this->libreSignFile->getSignedNodeId();
680-
681-
$fileToSign = $this->root->getUserFolder($this->libreSignFile->getUserId())->getFirstNodeById($nodeId);
682-
if (!$fileToSign instanceof File) {
683-
throw new LibresignException($this->l10n->t('File not found'));
684-
}
685-
return $fileToSign;
678+
protected function getPdfToSign(File $originalFile): File {
679+
$file = $this->getSignedFile();
680+
if ($file instanceof File) {
681+
return $file;
686682
}
687683
$footer = $this->footerHandler
688684
->setTemplateVar('signers', array_map(fn (SignRequestEntity $signer) => [
@@ -710,6 +706,34 @@ private function getPdfToSign(File $originalFile): File {
710706
return $this->createSignedFile($originalFile, $pdfContent);
711707
}
712708

709+
protected function getSignedFile(): ?File {
710+
$nodeId = $this->libreSignFile->getSignedNodeId();
711+
if (!$nodeId) {
712+
return null;
713+
}
714+
715+
$fileToSign = $this->getNodeByIdUsingUid($this->libreSignFile->getUserId(), $nodeId);
716+
717+
if ($fileToSign->getOwner()->getUID() !== $this->libreSignFile->getUserId()) {
718+
$fileToSign = $this->getNodeByIdUsingUid($fileToSign->getOwner()->getUID(), $nodeId);
719+
}
720+
return $fileToSign;
721+
}
722+
723+
protected function getNodeByIdUsingUid(string $uid, int $nodeId): File {
724+
try {
725+
$fileToSign = $this->root->getUserFolder($uid)->getFirstNodeById($nodeId);
726+
} catch (NoUserException) {
727+
throw new LibresignException($this->l10n->t('User not found.'));
728+
} catch (NotPermittedException) {
729+
throw new LibresignException($this->l10n->t('You do not have permission for this action.'));
730+
}
731+
if (!$fileToSign instanceof File) {
732+
throw new LibresignException($this->l10n->t('File not found'));
733+
}
734+
return $fileToSign;
735+
}
736+
713737
private function createSignedFile(File $originalFile, string $content): File {
714738
$filename = preg_replace(
715739
'/' . $originalFile->getExtension() . '$/',

tests/php/Unit/Service/SignFileServiceTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
use bovigo\vfs\vfsStream;
10+
use OC\User\NoUserException;
1011
use OCA\Libresign\Db\AccountFileMapper;
1112
use OCA\Libresign\Db\File;
1213
use OCA\Libresign\Db\FileElement;
@@ -38,6 +39,7 @@
3839
use OCP\EventDispatcher\IEventDispatcher;
3940
use OCP\Files\IRootFolder;
4041
use OCP\Files\NotFoundException;
42+
use OCP\Files\NotPermittedException;
4143
use OCP\Http\Client\IClientService;
4244
use OCP\IAppConfig;
4345
use OCP\IDateTimeZone;
@@ -1076,4 +1078,75 @@ private static function createScenarioSetVisibleElements(
10761078
$isAuthenticatedSigner,
10771079
];
10781080
}
1081+
1082+
#[DataProvider('providerGetSignedFile')]
1083+
public function testGetSignedFile(
1084+
int $timesCalled,
1085+
string $managerUid,
1086+
?string $ownerUid = null,
1087+
?int $nodeId = null,
1088+
): void {
1089+
$service = $this->getService(['getNodeByIdUsingUid']);
1090+
1091+
$libreSignFile = new \OCA\Libresign\Db\File();
1092+
$libreSignFile->setSignedNodeId($nodeId);
1093+
$libreSignFile->setUserId($managerUid);
1094+
$service->setLibreSignFile($libreSignFile);
1095+
1096+
$fileToSign = $this->createMock(\OCP\Files\File::class);
1097+
$user = $this->createMock(\OCP\IUser::class);
1098+
$user->method('getUID')->willReturn($ownerUid);
1099+
$fileToSign->method('getOwner')->willReturn($user);
1100+
$service
1101+
->expects($this->exactly($timesCalled))
1102+
->method('getNodeByIdUsingUid')
1103+
->willReturn($fileToSign);
1104+
1105+
$this->invokePrivate($service, 'getSignedFile');
1106+
}
1107+
1108+
public static function providerGetSignedFile(): array {
1109+
return [
1110+
[0, 'managerUid', '', null],
1111+
[1, 'managerUid', 'managerUid', 1],
1112+
[2, 'managerUid', 'johndoe', 1],
1113+
];
1114+
}
1115+
1116+
#[DataProvider('providerGetNodeByIdUsingUid')]
1117+
public function testGetNodeByIdUsingUid(
1118+
string $typeOfNode,
1119+
string $exceptionMessage,
1120+
): void {
1121+
$service = $this->getService();
1122+
if ($exceptionMessage) {
1123+
$this->expectExceptionMessageMatches($exceptionMessage);
1124+
}
1125+
$leaf = $this->createMock($typeOfNode);
1126+
$userFolder = $this->createMock(\OCP\Files\Folder::class);
1127+
$userFolder->method('getFirstNodeById')->willReturn($leaf);
1128+
$this->root->method('getUserFolder')->willReturnCallback(function () use ($userFolder, $exceptionMessage) {
1129+
switch ($exceptionMessage) {
1130+
case '/User not found/':
1131+
throw new NoUserException();
1132+
case '/not have permission/':
1133+
throw new NotPermittedException();
1134+
case '/File not found/':
1135+
return $userFolder;
1136+
default:
1137+
return $userFolder;
1138+
}
1139+
});
1140+
$actual = $this->invokePrivate($service, 'getNodeByIdUsingUid', ['', 1]);
1141+
$this->assertEquals($leaf, $actual);
1142+
}
1143+
1144+
public static function providerGetNodeByIdUsingUid(): array {
1145+
return [
1146+
[\OCP\Files\Folder::class, '/User not found/'],
1147+
[\OCP\Files\Folder::class, '/not have permission/'],
1148+
[\OCP\Files\Folder::class, '/File not found/'],
1149+
[\OCP\Files\File::class, ''],
1150+
];
1151+
}
10791152
}

0 commit comments

Comments
 (0)