Skip to content

Commit

Permalink
Merge pull request #2815 from LibreSign/backport/2811/stable28
Browse files Browse the repository at this point in the history
[stable28] fix: save and retrieve file to guest account
  • Loading branch information
vitormattos authored Apr 19, 2024
2 parents 83c65ee + 7ae99e7 commit 39fe89a
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 98 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/behat-mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ jobs:
./occ app:enable --force notifications
git clone --depth 1 -b ${{ matrix.server-versions }} https://github.com/nextcloud/activity apps/activity
./occ app:enable --force activity
./occ app:enable --force guests
./occ config:system:set mail_smtpport --value 1025 --type integer
./occ config:system:set mail_smtphost --value mailhog
./occ config:system:set allow_local_remote_servers --value true --type boolean
./occ config:system:set auth.bruteforce.protection.enabled --value false --type boolean
- name: Run behat
working-directory: apps/${{ env.APP_NAME }}/tests/integration
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/behat-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ jobs:
./occ app:enable --force notifications
git clone --depth 1 -b ${{ matrix.server-versions }} https://github.com/nextcloud/activity apps/activity
./occ app:enable --force activity
./occ app:enable --force guests
./occ config:system:set mail_smtpport --value 1025 --type integer
./occ config:system:set mail_smtphost --value mailhog
./occ config:system:set allow_local_remote_servers --value true --type boolean
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/behat-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
./occ app:enable --force notifications
git clone --depth 1 -b ${{ matrix.server-versions }} https://github.com/nextcloud/activity apps/activity
./occ app:enable --force activity
./occ app:enable --force guests
./occ config:system:set mail_smtpport --value 1025 --type integer
./occ config:system:set mail_smtphost --value mailhog
./occ config:system:set allow_local_remote_servers --value true --type boolean
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/behat-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ jobs:
./occ app:enable --force notifications
git clone --depth 1 -b ${{ matrix.server-versions }} https://github.com/nextcloud/activity apps/activity
./occ app:enable --force activity
./occ app:enable --force guests
./occ config:system:set mail_smtpport --value 1025 --type integer
./occ config:system:set mail_smtphost --value mailhog
./occ config:system:set allow_local_remote_servers --value true --type boolean
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ public function createSignatureElement(array $elements): JSONResponse {

#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
public function getSignatureElements(): JSONResponse {
$userId = $this->userSession->getUser()?->getUID();
try {
Expand Down
25 changes: 14 additions & 11 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IGroupManager;
Expand Down Expand Up @@ -308,14 +309,17 @@ public function getPdfByUuid(string $uuid): File {
public function getFileByNodeIdAndSessionId(int $nodeId, string $sessionId): File {
$rootSignatureFolder = $this->folderService->getFolder();
if (!$rootSignatureFolder->nodeExists($sessionId)) {
throw new DoesNotExistException('Not found');
try {
return $this->folderService->getFileById($nodeId);
} catch (NotFoundException $th) {
throw new DoesNotExistException('Not found');
}
}
$nodes = $rootSignatureFolder->getById($nodeId);
if (empty($nodes)) {
try {
return $this->folderService->getFileById($nodeId);
} catch (NotFoundException $th) {
throw new DoesNotExistException('Not found');
}
$file = current($nodes);
return $file;
}

public function canRequestSign(?IUser $user = null): bool {
Expand Down Expand Up @@ -380,9 +384,7 @@ private function updateFileOfVisibleElement(array $data): void {
return;
}
$userElement = $this->userElementMapper->findOne(['id' => $data['elementId']]);
$userFolder = $this->folderService->getFolder($userElement->getFileId());
/** @var \OCP\Files\File */
$file = $userFolder->getById($userElement->getFileId())[0];
$file = $this->folderService->getFileById($userElement->getFileId());
$file->putContent($this->getFileRaw($data));
}

Expand Down Expand Up @@ -475,9 +477,10 @@ public function deleteSignatureElement(?IUser $user, string $sessionId, int $nod
'user_id' => $user->getUID(),
]);
$this->userElementMapper->delete($element);
$file = $this->root->getById($element->getFileId());
if ($file) {
current($file)->delete();
try {
$file = $this->folderService->getFileById($element->getFileId());
$file->delete();
} catch (NotFoundException $e) {
}
} else {
$rootSignatureFolder = $this->folderService->getFolder();
Expand Down
59 changes: 25 additions & 34 deletions lib/Service/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -62,37 +63,37 @@ public function getUserId(): ?string {
}

/**
* Get folder for user
* Get folder for user and creates it if non-existent
*
* @psalm-suppress MixedReturnStatement
* @psalm-suppress InvalidReturnStatement
* @psalm-suppress MixedMethodCall
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getFolder(int $nodeId = null): Folder {
if ($nodeId) {
$mountsContainingFile = $this->userMountCache->getMountsForFileId($nodeId);
foreach ($mountsContainingFile as $fileInfo) {
$this->root->getByIdInPath($nodeId, $fileInfo->getMountPoint());
}
$node = $this->root->getById($nodeId);
if (!$node) {
throw new \Exception('Invalid node');
}
return $node[0]->getParent();
public function getFolder(): Folder {
$path = $this->getLibreSignDefaultPath();
$containerFolder = $this->getContainerFolder();
if (!$containerFolder->nodeExists($path)) {
return $containerFolder->newFolder($path);
}

return $this->getOrCreateFolder();
return $containerFolder->get($path);
}

/**
* Finds a folder and creates it if non-existent
*
* @psalm-suppress MixedReturnStatement
* @throws NotFoundException
* @throws NotPermittedException
*/
private function getOrCreateFolder(): Folder {
public function getFileById(int $nodeId = null): File {
$path = $this->getLibreSignDefaultPath();
$containerFolder = $this->getContainerFolder();
if (!$containerFolder->nodeExists($path)) {
throw new NotFoundException('Invalid node');
}
/** @var Folder $folder */
$folder = $containerFolder->get($path);
$file = $folder->getById($nodeId);
return current($file);
}

private function getContainerFolder(): Folder {
$withoutPermission = false;
if ($this->getUserId()) {
$containerFolder = $this->root->getUserFolder($this->getUserId());
Expand All @@ -110,22 +111,12 @@ private function getOrCreateFolder(): Folder {
$reflection = new \ReflectionClass($containerFolder);
$reflectionProperty = $reflection->getProperty('folder');
$reflectionProperty->setAccessible(true);
$containerFolder = $reflectionProperty->getValue($containerFolder);
} else {
$containerFolder = $this->root->getUserFolder($this->getUserId());
return $reflectionProperty->getValue($containerFolder);
}
if ($containerFolder->nodeExists($path)) {
$folder = $containerFolder->get($path);
} else {
$folder = $containerFolder->newFolder($path);
}
return $folder;
return $this->root->getUserFolder($this->getUserId());
}

/**
* @psalm-suppress MixedReturnStatement
*/
public function getLibreSignDefaultPath(): string {
private function getLibreSignDefaultPath(): string {
if (!$this->userId) {
return 'unauthenticated';
}
Expand Down
9 changes: 2 additions & 7 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,15 @@ public function setVisibleElements(array $list): self {
}
try {
if ($this->user instanceof IUser) {
$mountsContainingFile = $this->userMountCache->getMountsForFileId($nodeId);
foreach ($mountsContainingFile as $fileInfo) {
$this->root->getByIdInPath($nodeId, $fileInfo->getMountPoint());
}
/** @var \OCP\Files\File[] */
$node = $this->root->getById($nodeId);
$node = $this->folderService->getFileById($nodeId);
} else {
$filesOfElementes = $this->signerElementsService->getElementsFromSession();
$node = array_filter($filesOfElementes, fn ($file) => $file->getId() === $nodeId);
$node = current($node);
}
if (!$node) {
throw new \Exception('empty');
}
$node = current($node);
} catch (\Throwable $th) {
throw new LibresignException($this->l10n->t('You need to define a visible signature or initials to sign this document.'));
}
Expand Down
12 changes: 9 additions & 3 deletions lib/Service/SignerElementsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function getUserElementByNodeId(string $userId, $nodeId): array {
'id' => $element->getId(),
'type' => $element->getType(),
'file' => [
'url' => $this->urlGenerator->linkToRoute('core.Preview.getPreviewByFileId', ['fileId' => $element->getFileId(), 'x' => self::ELEMENT_SIGN_WIDTH, 'y' => self::ELEMENT_SIGN_HEIGHT]),
'url' => $this->urlGenerator->linkToRoute('ocs.libresign.account.getSignatureElementPreview', [
'apiVersion' => 'v1',
'nodeId' => $element->getFileId(),
]),
'nodeId' => $element->getFileId()
],
'uid' => $element->getUserId(),
Expand All @@ -73,7 +76,10 @@ public function getUserElements(string $userId): array {
'id' => $element->getId(),
'type' => $element->getType(),
'file' => [
'url' => $this->urlGenerator->linkToRoute('core.Preview.getPreviewByFileId', ['fileId' => $element->getFileId(), 'x' => self::ELEMENT_SIGN_WIDTH, 'y' => self::ELEMENT_SIGN_HEIGHT]),
'url' => $this->urlGenerator->linkToRoute('ocs.libresign.account.getSignatureElementPreview', [
'apiVersion' => 'v1',
'nodeId' => $element->getFileId(),
]),
'nodeId' => $element->getFileId()
],
'starred' => $element->getStarred() ? 1 : 0,
Expand All @@ -85,7 +91,7 @@ public function getUserElements(string $userId): array {

private function signatureFileExists(UserElement $userElement): bool {
try {
$this->folderService->getFolder($userElement->getFileId());
$this->folderService->getFileById($userElement->getFileId());
} catch (\Exception $e) {
$this->userElementMapper->delete($userElement);
return false;
Expand Down
3 changes: 1 addition & 2 deletions lib/Service/TFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function getNodeFromData(array $data): Node {
return $data['file']['fileNode'];
}
if (isset($data['file']['fileId'])) {
$userFolder = $this->folderService->getFolder($data['file']['fileId']);
return $userFolder->getById($data['file']['fileId'])[0];
return $this->folderService->getFileById($data['file']['fileId']);
}
if (isset($data['file']['path'])) {
return $this->folderService->getFileByPath($data['file']['path']);
Expand Down
Loading

0 comments on commit 39fe89a

Please sign in to comment.