Skip to content
Open
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
42 changes: 38 additions & 4 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldFactory;
use OCP\Files\Template\FieldType;
Expand All @@ -37,18 +39,45 @@ public function __construct(
) {
}

/**
* Resolve a fileId strictly within the acting user's scope so that a
* global id cannot be used to reach another user's file.
*
* @throws NotFoundException
*/
private function getUserFile(int $fileId): File {
if ($this->userId === null) {
throw new NotFoundException();
}

$userFolder = $this->rootFolder->getUserFolder($this->userId);
$node = $userFolder->getFirstNodeById($fileId);

if (!$node instanceof File) {
throw new NotFoundException();
}

// Defensive: the node is reachable in the user's scope, which already
// implies read access, but assert it explicitly.
if (!($node->getPermissions() & Constants::PERMISSION_READ)) {
throw new NotFoundException();
}

return $node;
}

/**
* @param Node|int $file
* @return Field[]
* @throws NotFoundException
*/
public function extractFields(Node|int $file): array {
if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
$file = $this->getUserFile($file);
}

try {
if (!$file || !$file instanceof File) {
if (!$file instanceof File) {
throw new NotFoundException();
}

Expand Down Expand Up @@ -124,17 +153,22 @@ public function extractFields(Node|int $file): array {
*/
public function fillFields(Node|int $file, array $fields = [], ?string $destination = null, ?string $format = null) {
if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
$file = $this->getUserFile($file);
}

if (!$file || !$file instanceof File) {
if (!$file instanceof File) {
$e = new NotFoundException();
$this->logger->error($e->getMessage());

throw $e;
}

if ($file->getMimeType() === 'application/pdf') {
// Filling a PDF overwrites the source in place, so require write access.
if (!($file->getPermissions() & Constants::PERMISSION_UPDATE)) {
throw new NotPermittedException('No update permission for file ' . $file->getId());
}

$content = $this->pdfService->fillFields($file, $fields);
if ($destination !== null) {
$this->writeToDestination($destination, $content);
Expand Down
Loading